Skip to content

Instantly share code, notes, and snippets.

@navidanindya
Created June 10, 2018 10:52
Show Gist options
  • Save navidanindya/ff0f22eec0d0c35a30b40b7987a43423 to your computer and use it in GitHub Desktop.
Save navidanindya/ff0f22eec0d0c35a30b40b7987a43423 to your computer and use it in GitHub Desktop.
Decode Morse code using Python. [ Link: https://repl.it/@nealtheguitaris/AcceptableWellinformedDifference ]
import re
MORSE_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',
'.-.-.-': '.', '--..--': ',', '..--..': '?', '.----.': "'", '-.-.--': '!',
'-..-.': '/', '-.--.': '(', '-.--.-': ')', '.-...': '&', '---...': ':',
'-.-.-.': ';', '-...-': '=', '.-.-.': '+', '-....-': '-', '..--.-': '_',
'.-..-.': '"', '...-..-': '$', '.--.-.': '@', '...---...': 'SOS'
}
def decodeMorse(morse_code):
return re.sub(' +',' ',''.join([MORSE_CODE[i] if i else ' ' for i in morse_code.split(' ')])).strip()
print(decodeMorse('.-- .. - .... --- .-. .-- .. - .... --- ..- - -.-- --- ..-'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment