Skip to content

Instantly share code, notes, and snippets.

@112buddyd
Last active August 29, 2015 14:27
Show Gist options
  • Save 112buddyd/f359ab57110789679783 to your computer and use it in GitHub Desktop.
Save 112buddyd/f359ab57110789679783 to your computer and use it in GitHub Desktop.
Easy Challenge #7 from reddit.com/r/dailyprogrammer
# EasyChallenge 7 - reddit.com/r/dailyprogrammer
# Convert morse code
# 10 AUG 2015
# Created a dictionary of Letter : Morse
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' : '--..'
}
inv_morse = {v:k for k, v in morse.items()} # This inverts the morse dictionary.
code = ".... . .-.. .-.. --- / -.. .- .. .-.. -.-- / .--. .-. --- --. .-. .- -- -- . .-. / --. --- --- -.. / .-.. ..- -.-. -.- / --- -. / - .... . / -.-. .... .- .-.. .-.. . -. --. . ... / - --- -.. .- -.--"
#Begin Logic
split_code = code.split(' ') # split morse string into list of letters by spaces
translation = [] # not sure I have to define this before using the append in line 23
for word in split_code: # begin translation
translation.append(inv_morse.get(word, ' ')) # .get() method searches given dictionary for keys and returns values. Invalid keys return ' ' which turns the / in ' '.
print("".join(x for x in translation)) # Prints translation list joined into a string.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment