Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Morse Decoder by COLDPLAY
MORSE = {'.-': 'a', '-...': 'b', '-.-.': 'c',
'-..': 'd', '.': 'e', '..-.': 'f',
'--.': 'g', '....': 'h', '..': 'i',
'.---': 'j', '-.-': 'k', '.-..': 'l',
'--': 'm', '-.': 'n', '---': 'o',
'.--.': 'p', '--.-': 'q', '.-.': 'r',
'...': 's', '-': 't', '..-': 'u',
'...-': 'v', '.--': 'w', '-..-': 'x',
'-.--': 'y', '--..': 'z', '-----': '0',
'.----': '1', '..---': '2', '...--': '3',
'....-': '4', '.....': '5', '-....': '6',
'--...': '7', '---..': '8', '----.': '9'
}
def morse_decoder(code):
"""I create a new variable message, with split i separate each word
by 3 spaces between and 1 space between each letter of each word
stocking the resultat in decoded massage variable.
After that with append and join ,i can stock the result of decoded message
in the message variable and i return the message capitalizing the first letter
"""
message = []
for m in code.split(' '):
decoded_message = []
for w in m.split(' '):
if w in MORSE:
decoded_message.append(MORSE[w])
message.append(''.join(decoded_message))
return ' '.join(message).capitalize()
if __name__ == '__main__':
print("Example:")
print(morse_decoder('... --- ...'))
#These "asserts" using only for self-checking and not necessary for auto-testing
assert morse_decoder("... --- -- . - . -..- -") == "Some text"
assert morse_decoder("..--- ----- .---- ---..") == "2018"
assert morse_decoder(".. - .-- .- ... .- --. --- --- -.. -.. .- -.--") == "It was a good day"
print("Coding complete? Click 'Check' to earn cool rewards!")
June 11, 2021