42 lines
1.5 KiB
Python
42 lines
1.5 KiB
Python
|
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: str) -> str:
|
||
|
sentence = ""
|
||
|
while len(code) > 0:
|
||
|
if code[0] == " ":
|
||
|
sentence += " "
|
||
|
code = code[2:]
|
||
|
next_space = code.find(" ")
|
||
|
if next_space != -1:
|
||
|
letter = code[0:next_space]
|
||
|
code = code[next_space+1:]
|
||
|
sentence += MORSE[letter]
|
||
|
else:
|
||
|
sentence += MORSE[code]
|
||
|
code = ""
|
||
|
return sentence[0].upper() + sentence[1:]
|
||
|
|
||
|
|
||
|
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!")
|