Skip to content

Instantly share code, notes, and snippets.

@MGF15
Last active October 7, 2016 17:06
Show Gist options
  • Save MGF15/e5bfb3789e851d1238626e1f74d6f06b to your computer and use it in GitHub Desktop.
Save MGF15/e5bfb3789e851d1238626e1f74d6f06b to your computer and use it in GitHub Desktop.
Morse Code Encode & Decode
#!/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
@MGF15
Copy link
Author

MGF15 commented Oct 7, 2016

m@mgf-desktop:~$ python morse.py    #encode
-- --. ..-. .---- ..... 
m@mgf-desktop:~$ python morse.py    #decode
MGF15

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment