![]() |
|
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
|
RE: Morse Encode/Decode Class - Deque - 04-23-2013 (04-23-2013, 12:31 PM)Ex094 Wrote: 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! It's perfect now. RE: Morse Encode/Decode Class - ArkPhaze - 04-24-2013 This: Code: store = store + morse_encode[item] + ' 'Code: store = store + ' ' + morse_decode[item]Could be shortened with += though. Code: store += morse_encode[item] + ' 'Code: store += ' ' + morse_decode[item]RE: Morse Encode/Decode Class - Ex094 - 04-24-2013 (04-24-2013, 01:27 AM)ArkPhaze Wrote: This: Good Suggestion, Thanks alot! Fixed that in the main code
RE: Morse Encode/Decode Class - ArkPhaze - 04-25-2013 I'm still a novice to intermediate in Python, but I'm also wondering if you could make that morse_decode variable a constant in that class? Or at least a static value? It never changes, but is used as reference, so declaring it in both functions within the class is about the last thing I could see about this code that could be improved. Perhaps a global? I'll take a look in a bit, I have to go in about 3 minutes, so i'm rushing my thought process here... RE: Morse Encode/Decode Class - Ex094 - 04-25-2013 (04-25-2013, 12:28 AM)ArkPhaze Wrote: I'm still a novice to intermediate in Python, but I'm also wondering if you could make that morse_decode variable a constant in that class? Or at least a static value? It never changes, but is used as reference, so declaring it in both functions within the class is about the last thing I could see about this code that could be improved. Perhaps a global? I'll take a look in a bit, I have to go in about 3 minutes, so i'm rushing my thought process here...I'm not that experienced either So you are saying that to declare a variable in the encode function which will take the encoded value and decode it at the same moment hence presenting both results, is this what you are saying?
RE: Morse Encode/Decode Class - ArkPhaze - 04-25-2013 (04-25-2013, 05:32 AM)Ex094 Wrote:(04-25-2013, 12:28 AM)ArkPhaze Wrote: I'm still a novice to intermediate in Python, but I'm also wondering if you could make that morse_decode variable a constant in that class? Or at least a static value? It never changes, but is used as reference, so declaring it in both functions within the class is about the last thing I could see about this code that could be improved. Perhaps a global? I'll take a look in a bit, I have to go in about 3 minutes, so i'm rushing my thought process here...I'm not that experienced either What? no.. What i'm saying is that you're re-declaring the variable regardless of the function being called though. edit: I didn't notice before that things were reversed in your variables. RE: Morse Encode/Decode Class - ArkPhaze - 04-25-2013 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 storeExample 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. RE: Morse Encode/Decode Class - Ex094 - 04-25-2013 (04-25-2013, 09:46 AM)ArkPhaze Wrote: Here is what I meant: Oh, Yeah that definitely was a valuable edit, Thanks! I've added your coded under my thread as a modified one by you
|