+ 3

Summarize code

How can we summarize this code it is for decode a massage https://sololearn.com/compiler-playground/cU3C8wpeMx6d/?ref=app

22nd Dec 2023, 12:06 PM
Mohammed Hassan
Mohammed Hassan - avatar
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?
23rd Dec 2023, 7:30 AM
Mafdi
Mafdi - avatar
+ 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
23rd Dec 2023, 11:44 AM
Lothar
Lothar - avatar
+ 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?
23rd Dec 2023, 6:21 AM
Mafdi
Mafdi - avatar
+ 3
You can put all the old characters in one string, and loop through it
23rd Dec 2023, 7:41 AM
Mafdi
Mafdi - avatar
+ 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?
23rd Dec 2023, 11:50 AM
Rain
Rain - avatar
+ 2
You can use a dictionary data structure, or nested list.
22nd Dec 2023, 2:30 PM
Mafdi
Mafdi - avatar
+ 2
Ajaiy P Just put your idea if we are not trust people off course will not Evan put a quastion .
23rd Dec 2023, 1:20 PM
Mohammed Hassan
Mohammed Hassan - avatar
+ 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.
22nd Dec 2023, 10:38 PM
Rain
Rain - avatar
+ 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
23rd Dec 2023, 5:26 AM
Mohammed Hassan
Mohammed Hassan - avatar
+ 1
Mr Mafdi replace() function work like this:- replace ("charc or word which replaceed","An alternative to a letter or word"(
23rd Dec 2023, 7:20 AM
Mohammed Hassan
Mohammed Hassan - avatar
+ 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.
23rd Dec 2023, 8:36 AM
Rain
Rain - avatar
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])
23rd Dec 2023, 12:50 PM
Ajaiy P
Ajaiy P - avatar
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] ]
23rd Dec 2023, 1:34 PM
Ajaiy P
Ajaiy P - avatar
0
text=[i for i in input().lower() if i in "qwertyuiopasdfghjklzxcvbnm "] text=list(text) text.reverse() print(''.join(text))
23rd Dec 2023, 10:47 PM
Annihilate
Annihilate - avatar