1

I'm trying to create a function that takes mores code as an input in string format and returns the message decoded also as a string.

I've identified that i need to split the string where there is a space to ascertain each individual character in morse. and a loop to return a value if matched in dictionary key. I'm a beginner and going really wrong somewhere. Thanks in advance.

code_dict =  {'.-...': '&', '--..--': ',', '....-': '4', '.....': '5',
     '...---...': 'SOS', '-...': 'B', '-..-': 'X', '.-.': 'R',
     '.--': 'W', '..---': '2', '.-': 'A', '..': 'I', '..-.': 'F',
     '.': 'E', '.-..': 'L', '...': 'S', '..-': 'U', '..--..': '?',
     '.----': '1', '-.-': 'K', '-..': 'D', '-....': '6', '-...-': '=',
     '---': 'O', '.--.': 'P', '.-.-.-': '.', '--': 'M', '-.': 'N',
     '....': 'H', '.----.': "'", '...-': 'V', '--...': '7', '-.-.-.': ';',
     '-....-': '-', '..--.-': '_', '-.--.-': ')', '-.-.--': '!', '--.': 'G',
     '--.-': 'Q', '--..': 'Z', '-..-.': '/', '.-.-.': '+', '-.-.': 'C', '---...': ':',
     '-.--': 'Y', '-': 'T', '.--.-.': '@', '...-..-': '$', '.---': 'J', '-----': '0',
     '----.': '9', '.-..-.': '"', '-.--.': '(', '---..': '8', '...--': '3'
     }

def decodeMorse(morseCode):
    for item in morseCode.split(' '):
        return code_dict.get(item)

my problem is it only decodes the first character of the string entered in morse

  • _I'm a beginner and going really wrong somewhere._ What is your issue? Getting any errors? – B001ᛦ Jul 25 '16 at 11:47
  • it only returns the first character of the string entered –  Jul 25 '16 at 11:47
  • 1
    @Anayatc: yes, because you use `return` in the loop. `return` exits a function *immediately*. What did you want to return instead? – Martijn Pieters Jul 25 '16 at 11:48
  • That's because youare `return`ing inside the loop, which terminates the loop. Try: `return [code_dict.get(item) for item in morseCode.split(' ')]` – Bakuriu Jul 25 '16 at 11:48

1 Answers1

4

return something instantly ends the function. You stop stop processing input after first character.

In other languages, you could instead create list (array) with results, and return that:

def decodeMorse(morseCode):
    results = []
    for item in morseCode.split(' '):
        results.append(code_dict.get(item))
    return results

Or, as @Bakuriu suggested:

def decodeMorse(morseCode):
    for item in morseCode.split(' '):
        return [code_dict.get(item) for item in morseCode.split(' ')]

There is however simple flaw with this approach -- it decodes whole string at once, even if you only need first few characters.

We can do better in Python.

Use yield instead of return:

def decodeMorse(morseCode):
    for item in morseCode.split(' '):
        yield code_dict.get(item)

Now, the function instead of returning whole list at once, returns generator which yields one character at once. If you don't need whole translation, it's likely to be faster. It'll also use less memory (you don't need to construct and keep in memory list of all the characters).

You can convert the generator into list (list(decodeMorse('... --- ...'))) or into string (''.join(decodeMorse('... --- ...'))) if you need to. You can also just iterate over it like over a sequence:

>>> decoded = decodeMorse('... --- ...')
>>> for char in decoded:
...     print(char)
...
S
O
S
>>>

...except, you can only do it once:

>>> for char in decoded:
...     print(char)
...
>>>

...because generators are disposable.

If you need to iterate over it another time, store it in list, or create another generator by calling decodeMorse again.

GingerPlusPlus
  • 5,336
  • 1
  • 29
  • 52