Login Register


Morse Encode/Decode Class filter_list
Author
Message
RE: Morse Encode/Decode Class #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 Smile
My Blog: http://www.procurity.wordpress.com
Donations: 1HLjiSbnWMpeQU46eUVCrYdbkrtduX7snG

Reply





Messages In This Thread
Morse Encode/Decode Class - by Ex094 - 04-19-2013, 11:53 AM
Morse Encode/Decode Class - by Ex094 - 04-19-2013, 11:53 AM



Users browsing this thread: 1 Guest(s)