Morse Encode/Decode Class 04-19-2013, 11:53 AM
#2
![[Image: FY7L401.png]](http://i.imgur.com/FY7L401.png)
Here's a class that I made for Morse Encode/Decode functions for Deque's Multi-Encoder Collection

Code:
class ex094morse:
def encode(self, text):
morse_encode = {'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' : '----.',' ' : ' '}
store = ''
for item in text.upper():
store += morse_encode[item] + ' '
return store
def decode(self, text):
morse_decode = {'.-' : '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'}
store = ''
for item in text.split():
store += ' ' + morse_decode[item]
return storeArkPhase's Modified Code:
Code:
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 storeNo explanation because I've already done it in this thread http://www.hackcommunity.com/Thread-Mors...#pid134332
Have fun


![[+]](https://sinister.ly/images/modern/collapse_collapsed.png)