Created
November 20, 2013 15:55
-
-
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
This file contains hidden or 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
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