![]() Symbols = encoded.replace(" ", " SPACE "). " ": "SPACE", # Special "sentinel" value to simplify decoder.ĭecode_table = Įnc = " ".join(encode_table for x in s) Thanks in advance.Īlways turn code into data whenever possible. ')) #returns HEY JUDEĪnyone with a solution that makes this more pythonic? For this exercise, we are not allowed to use regexp library. So here is my very ugly but working approach: def decode_morse(morse_code): After replacing a character, the next replace function won't be able to find a character, because it needs the spaces to determine the start and the end of a character (else. Leaving out the spaces won't do much good. This returns H E Y J U D E, rather than HEY JUDE. I have tried to do it with the replace method first, but I ran into trouble because replace doesn't have the flexibility needed to decode morse code. I can not create a new string within the for loop either, since the variable will be lost upon leaving the for loop scope. I can't create an empty string, go into a for loop, and append to the string. The reason I work with lists is because strings are immutable in python. Finally, the lists within the list are joined, and the resulting list is joined as well so it can be returned as a string value. In my code below I create an empty list, which get filled with a list in which each item represents a single morse code character, that is then replaced with the actual character.
0 Comments
Leave a Reply. |