Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
morse_encoder solution in Clear category for Morse Encoder by dannedved
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_encoder(text):
# Iterate through each character of the input text
# If the characer (in lowercase) is encoded, encrypt it, oterwise (i. e. if character is a white space), don't change it.
# Join the list consisting of encrypted characters and whitespaces into a string, the characters being separated by " ".
return " ".join([MORSE[char.lower()] if char.lower() in MORSE else char for char in text])
if __name__ == '__main__':
print("Example:")
print(morse_encoder('some text'))
#These "asserts" using only for self-checking and not necessary for auto-testing
assert morse_encoder("some text") == "... --- -- . - . -..- -"
assert morse_encoder("2018") == "..--- ----- .---- ---.."
assert morse_encoder("It was a good day") == ".. - .-- .- ... .- --. --- --- -.. -.. .- -.--"
print("Coding complete? Click 'Check' to earn cool rewards!")
May 4, 2019
Comments: