Skip to content

Instantly share code, notes, and snippets.

@Ex094
Created November 20, 2013 15:55
Show Gist options
  • Save Ex094/7565537 to your computer and use it in GitHub Desktop.
Save Ex094/7565537 to your computer and use it in GitHub Desktop.
A Small Python Class I made for encoding and decoding Morse code Thanks to Arkphaze for code improvements
class ex094morse:
morse_alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
morse_encode = ['.-', '-...', '-.-.', '-.', '.', '..-.', '--.', '....', '..', '.---', '-.-', '.-..', '--', '-.', '---', '.--.', '--.-', '.-.','...' ,'-' ,'..-' ,'...-' ,'.--' ,'-..-' ,'-.--' ,'--..' ,'-----' ,'.----','..---','...--','....-','.....','-....','--...','---..','----.']
def encode(self, text):
store = ''
for item in text.upper():
store += self.morse_encode[self.morse_alpha.find(item)] + ' '
return store
def decode(self, text):
store = ''
for item in text.split():
store += ' ' + self.morse_alpha[self.morse_encode.index(item)]
return store
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment