Skip to content

Instantly share code, notes, and snippets.

@victorazzam
Last active December 17, 2019 04:51
Show Gist options
  • Save victorazzam/5da783bd56c790cf538347d8ce84d4ab to your computer and use it in GitHub Desktop.
Save victorazzam/5da783bd56c790cf538347d8ce84d4ab to your computer and use it in GitHub Desktop.
Morse code translator.
#!/usr/bin/env python3
from sys import argv, exit
u = "Usage: morsecode <from> <text>\nTypes: morse (m), english (e)"
e = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
m = ".- -... -.-. -.. . ..-. --. .... .. .--- -.- .-.. -- -. --- .--. --.- .-. ... - ..- ...- .-- -..- -.-- --.. ----- .---- ..--- ...-- ....- ..... -.... --... ---.. ----.".split()
english = dict(zip(e, m))
morse = dict(zip(m, e))
try:
if len(argv) < 3:
exit(u)
elif argv[1].lower() in ("english", "e"):
pre, dic, arg = " ", english, "".join(argv[2:]).upper()
elif argv[1].lower() in ("morse", "m"):
pre, dic, arg = "", morse, argv[2:]
else:
exit(u)
print(pre.join(dic.get(x, "?") for x in arg))
except (KeyboardInterrupt, EOFError):
print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment