Last active
October 7, 2016 17:06
-
-
Save MGF15/e5bfb3789e851d1238626e1f74d6f06b to your computer and use it in GitHub Desktop.
Morse Code Encode & Decode
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/python | |
# Morse Code | |
# Encode & Decode | |
# http://stackoverflow.com/questions/32094525/morse-code-to-english-python3?answertab=votes#tab-top | |
# direction from ^ LOL i'm too lazy | |
# add . to @ from wiki ;) https://en.wikipedia.org/wiki/Morse_code | |
CODE = {'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': '----.' , | |
' ':' ', '.':'.-.-.-', ',':'--..--', | |
'?':'.--..', "'":'.----.', '!':'-.-.--', | |
'/':'-..-.', '[':'-.--.', ']':'-.--.-', | |
'&':'.-...', ':':'---...', ';':'-.-.-.', | |
'=':'-...-', '+':'.-.-.', '-':'-....-', | |
'_':'..--.-', '"':'.-..-.', '$':'...-..-', | |
'@':'.--.-.' | |
} | |
CODE_ = {value:key for key,value in CODE.items()} | |
CODE_["<>"] = ' ' #to fix space i know it's ugly :\ | |
def en_morse(msg): | |
a = '' | |
for i in msg: | |
a += CODE[i.upper()] + ' ' | |
return a | |
def de_morse(msg): | |
b = '' | |
w = msg.replace(' ',' <> ') #Lovewins :P | |
for i in w.split(): | |
b += CODE_[i] | |
return b |
Author
MGF15
commented
Oct 7, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment