Is there a way to do this more efficiently? Spylife | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Is there a way to do this more efficiently? Spylife

https://code.sololearn.com/c6471EpWb3Tv/?ref=app

5th Apr 2021, 5:33 PM
Tarun Kumar Aggarwal
Tarun Kumar Aggarwal - avatar
6 Answers
+ 3
Tarun Kumar Aggarwal Instead of making a list of alphabets, you can simply use str.isalpha() to check if a character is an alphabet. https://code.sololearn.com/cys855C11tXa/?ref=app There is also a one-line way to do the using the filter() function, but I'm guessing you're not familiar with it currently.
5th Apr 2021, 5:58 PM
XXX
XXX - avatar
+ 1
XXX You're right. But for this special code coach use case, you have also to consider the "blank" if char.isalpha() or char==' ':
5th Apr 2021, 6:09 PM
Coding Cat
Coding Cat - avatar
0
What are you trying to do here? Are you simply trying to reverse a string. Then the for-loop is unnecessary. Only `codedtext[::-1]` is enough.
5th Apr 2021, 5:40 PM
XXX
XXX - avatar
0
This is an exercise in code coach. The input string will have special characters and numbers which the program needs to remove and then reverse the string
5th Apr 2021, 5:42 PM
Tarun Kumar Aggarwal
Tarun Kumar Aggarwal - avatar
0
Thanks XXX
5th Apr 2021, 6:04 PM
Tarun Kumar Aggarwal
Tarun Kumar Aggarwal - avatar
0
Here is a streaming approach that does not retain the whole result. It uses fewer lines of code and fewer variables: buf = input() for ch in buf[::-1]: if (ch.isalpha() or ch==' '): print(ch, end="")
5th Apr 2021, 11:29 PM
Brian
Brian - avatar