RE: Morse Encode/Decode Class 04-25-2013, 09:52 AM
#18
(04-25-2013, 09:46 AM)ArkPhaze Wrote: Here is what I meant:
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 store
Example usage:
Code:x = ex094morse() print(x.encode('test')) print(x.decode('- . ... - '))
Not sure if I got the encoded values and the decoded values matched up properly, but that's the way I would do it. Then you can reference the variables without having to redeclare them in every function in the class.
Oh, Yeah that definitely was a valuable edit, Thanks! I've added your coded under my thread as a modified one by you


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