Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Speedy category for Morse Decoder by DEADPOOL
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):
result = ""
for i in range(len(split(code))):
if split(code)[i] in MORSE:
if i == 0:
result += MORSE[(split(code)[i])].upper()
else:
result += MORSE[(split(code)[i])]
else:
result += " "
return result
def split(code):
letter = ""
space = 0
len_code = 0
word = []
for i in code:
len_code += 1
letter += i
if i == " ":
space += 1
word.append(letter[:-1])
letter = ""
if space == 3:
space = 0
word = word[:-2]
word.append("W")
elif len_code == len(code):
word.append(letter)
else:
space = 0
return word
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!")
Dec. 24, 2018