+ 3
Summarize code
How can we summarize this code it is for decode a massage https://sololearn.com/compiler-playground/cU3C8wpeMx6d/?ref=app
16 Réponses
+ 5
I want to know the task description.
Is your code works fine for the task? So you only want it to look nicer?
+ 4
it looks like this is the task `the spy life` from code coach. the output should only contain letters and spaces, and the string has to be reversed.
it ca be done like:
create a new empty string as output_ string
use a loop and iterate over the characters of the input string
if character is a letter (use function .isalpha()) or a space (use .isspace()):
ad the character to the output_string (use concatenation with + operator)
otherwise continue
reverse the string and output it
+ 3
Mohammed Hassan ,
I misunderstood your question, and I was wondering why you left empty quotes!
From which course and lesson did you get/made this code for?
+ 3
You can put all the old characters in one string, and loop through it
+ 3
Lothar ,
Ah, I see it.
Community > Code Coach >
The Spy Life (medium)
https://www.sololearn.com/coach/55?ref=app
Mohammed Hassan ,
Will you add that to the opening discussion post, so people know what it is right away?
+ 2
You can use a dictionary data structure, or nested list.
+ 2
Ajaiy P
Just put your idea if we are not trust people off course will not Evan put a quastion .
+ 1
Mohammed Hassan ,
Is that from a Community > Code Coach test? It sounds familiar but I can't find it. If I understand, you're supposed to, for example, input "e+re&<ht 7i9h🎃" and output "hi there".
So you want to reverse the input then keep certain characters and throw away the rest.
The problem with your code, besides being too repetitious, is that there are way more bad characters than good characters, and you're trying to specify the bad characters one by one, but you have thousands more to go (because of unicode).
It would be much easier to make a master string listing the characters you want to keep. Then you could do a simple loop to only print each character in the reversed input string if it's in that master string, ignoring the potential thousands of bad characters. Do you know about the in operator? To print a character without adding newline, do this.
print(my_character, end="")
There's your hint. I won't post the code, to give you a chance to try it, unless you get desperate.
+ 1
Rain
This is plan from beginning but
If i will do a string character list it is also it will take much more because i must edit any symbol in world 😂😅 in this list
+ 1
Mr Mafdi
replace() function work like this:-
replace ("charc or word which replaceed","An alternative to a letter or word"(
+ 1
Mohammed Hassan ,
I think you misread my message backwards. I didn't say the master string would contain all the bad characters in the world. I said it would contain only the few good characters you want to print. You can ignore the rest of the characters in the world, even if you don't know what they are.
And you don't need to create a new string object each time you remove a character, as you did (remember strings are immutable). You only need the reversed input string. Then print each good character from it with end="" so it stays on the same line.
0
One liner code: (be honest and don't copy the solution)
print("".join([i for i in input() if i.isalpha() or i == ' '])[::-1])
0
You can interpret it like this:
[::-1] when added to the end of any sequence data type will reverse it (you can find out how if you are curious)
The <string>.join([<list of strings>]) function joins all the list of strings, keeping the string mentioned outside between each string.
Eg: "(".join(["I", "am", "Ravi"]) returns:
I(am(Ravi
The for loop can also be compressed to filter an array to have only specific elements like this:
[i for i in <string> if <condition the letter has to match to be in the arrays>]
So, the meaning of the line is:
reverse the following word: [join all the strings of the following array and return a string: [return all the characters in the input who is an alphabet or space] ]
0
text=[i for i in input().lower() if i in "qwertyuiopasdfghjklzxcvbnm "]
text=list(text)
text.reverse()
print(''.join(text))