Skip to content

Instantly share code, notes, and snippets.

@udoprog
Created May 7, 2011 19:28
Show Gist options
  • Save udoprog/960758 to your computer and use it in GitHub Desktop.
Save udoprog/960758 to your computer and use it in GitHub Desktop.
A morse code transscriber
import sys
lookup_map={
'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': "--..",
' ': " ",
'1': ".----",
'2': "..---",
'3': "...--",
'4': "....-",
'5': ".....",
'6': "-....",
'7': "--...",
'8': "---..",
'9': "----."
}
reverse_map={}
for k in lookup_map.keys():
v = lookup_map[k]
reverse_map[v] = k
inp = raw_input("sentence:")
if sys.argv[1] == "-r":
sys.stdout.write(" ".join(["".join([reverse_map.get(c.strip(), "") for c in w.split(" ")]) for w in inp.split(" ")]))
sys.exit(0)
sys.stdout.write(" ".join([" ".join([lookup_map.get(c.upper(), "") for c in w]) for w in inp.split(" ")]))
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment