Last active
December 17, 2019 04:51
-
-
Save victorazzam/5da783bd56c790cf538347d8ce84d4ab to your computer and use it in GitHub Desktop.
Morse code translator.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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