![]() |
|
Morse Encode/Decode Class - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Coding (https://sinister.ly/Forum-Coding) +--- Forum: Python (https://sinister.ly/Forum-Python) +--- Thread: Morse Encode/Decode Class (/Thread-Morse-Encode-Decode-Class) Pages:
1
2
|
Morse Encode/Decode Class - Ex094 - 04-19-2013 ![]() 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-Morse-Decoder?pid=134332#pid134332 Have fun
Morse Encode/Decode Class - Ex094 - 04-19-2013 ![]() 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-Morse-Decoder?pid=134332#pid134332 Have fun
RE: Morse Encode/Decode Class - Deque - 04-19-2013 Hi Ex094, thanks for your entry. There are some things I would like to change though. You are doing the input and output yourself. But this is my part. The Multi-Encoder program will get the user input and use your class (and the class of other submissions) to encode or decode it. Because of that I just need the encoding/decoding from you, not any kind of user interface that I will have to remove. Please make sure that your program adheres the specification. Look at the example and the description Do this: Code: def encode(self, text):
def decode(self, code):Not this: Code: def encode():
def decode():Otherwise I can't use your code. Last but not least: The dictionaries (morse_decode, morse_encode) belong into the class, not outside of it. RE: Morse Encode/Decode Class - Deque - 04-19-2013 Hi Ex094, thanks for your entry. There are some things I would like to change though. You are doing the input and output yourself. But this is my part. The Multi-Encoder program will get the user input and use your class (and the class of other submissions) to encode or decode it. Because of that I just need the encoding/decoding from you, not any kind of user interface that I will have to remove. Please make sure that your program adheres the specification. Look at the example and the description Do this: Code: def encode(self, text):
def decode(self, code):Not this: Code: def encode():
def decode():Otherwise I can't use your code. Last but not least: The dictionaries (morse_decode, morse_encode) belong into the class, not outside of it. RE: Morse Encode/Decode Class - Ex094 - 04-19-2013 (04-19-2013, 07:04 PM)Deque Wrote: Hi Ex094,Jeez, Thanks alot, I'll correct those stuff right away and for the dictionary I kept getting confused whether to put em inside or outside but now you've cleared it @Deque I've done the adjustments in the code, do check it and alert me if it needs further edit
RE: Morse Encode/Decode Class - Ex094 - 04-19-2013 (04-19-2013, 07:04 PM)Deque Wrote: Hi Ex094,Jeez, Thanks alot, I'll correct those stuff right away and for the dictionary I kept getting confused whether to put em inside or outside but now you've cleared it @Deque I've done the adjustments in the code, do check it and alert me if it needs further edit
RE: Morse Encode/Decode Class - ArkPhaze - 04-22-2013 It could be fun to create this in C# (or VB.NET) with a call to the Beep function from Kernel32.dll as sound lol. Maybe i'll write it up quick tomorrow and release it. I have other projects on my plate but it's nice to have something else to distract you for a day or two just to regain a clear mind. RE: Morse Encode/Decode Class - Deque - 04-22-2013 Code: def encode(self):
for item in self.upper():What are you doing here? self is not the text. It is the reference to the instance of the class. You shouldn't use it at all. You should do: Code: def encode(self, text):And Code: def decode(self, text):Please have a look at my example in the contest thread and you will understand. If you do Code: encoder = new MorseEncoder()
encoder.encode("my text")it is a bit like doing this instead: Code: encode(encoder, "my text")encoder is the self, the object that you pass to the function. You shouldn't handle it as a string. Code: return 'Your Decoded Message is: %s' % storeHere you are doing part of the user interface ("Your Decoded Message is" is a note from the UI). I only want the decoded/encoded message, not any description or I end up getting stuff like this in the tool: Code: Encoded message: You encoded message is: Hello WorldRE: Morse Encode/Decode Class - Ex094 - 04-23-2013 Quote:Code: Understood! Sorry Deque for putting such a mess! I didn't had the time to properly correct as I was a little busy of my own, I've done the work now and I think it meets your needs now! RE: Morse Encode/Decode Class - Ex094 - 04-23-2013 Quote:Code: Understood! Sorry Deque for putting such a mess! I didn't had the time to properly correct as I was a little busy of my own, I've done the work now and I think it meets your needs now! |