![]() |
|
Morse Decoder - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Coding (https://sinister.ly/Forum-Coding) +--- Forum: Python (https://sinister.ly/Forum-Python) +--- Thread: Morse Decoder (/Thread-Morse-Decoder) Pages:
1
2
|
Morse Decoder - Ex094 - 04-18-2013 ![]() This is a very simple python program that uses a dictionary containing Morse code key like '-' for T. The condition to decode via key present in the dictionary is: Code: store = ''
for items in code:
if the key is in the dictionary:
then store the value of the key in the variable Store
if else the key is not found in the dictionary:
print that the key was not found
print value of the variable storeSource Code: Code: morse = {'.-' : '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 = ''
# We need to store the code with the spaces in between as intact so we use split() to keep the spaces between the codes and make a list code.
code = input('Input your Morse Code: ').split()
#For Error management
try:
#For each item in the list 'code'
for item in code:
#Get the value of the key and store it in the variable 'store', if the key exists in the dictionary, if not morse[item]
#will be false and it will raise the KeyError that key not found in the dictionary which will be handled
#as a normal error at the end of the code.
store = store + morse[item]
#Prints the decoded code stored in the variable 'store'
print('Your Decoded Message is: %s' % store)
# In case of the above code returns false due to morse[item]
except KeyError as error:
#Display error
print('Your code contains invalid morse code character', error)Compiled Source Download: http://www.mediafire.com/?nujm7sgcxcliu97 Everything is explained in the code comments, Have fun! If you like, you can make it encode the normal text into a morse, you just need to reverse the dictionary values and then ask for normal text
Morse Decoder - Ex094 - 04-18-2013 ![]() This is a very simple python program that uses a dictionary containing Morse code key like '-' for T. The condition to decode via key present in the dictionary is: Code: store = ''
for items in code:
if the key is in the dictionary:
then store the value of the key in the variable Store
if else the key is not found in the dictionary:
print that the key was not found
print value of the variable storeSource Code: Code: morse = {'.-' : '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 = ''
# We need to store the code with the spaces in between as intact so we use split() to keep the spaces between the codes and make a list code.
code = input('Input your Morse Code: ').split()
#For Error management
try:
#For each item in the list 'code'
for item in code:
#Get the value of the key and store it in the variable 'store', if the key exists in the dictionary, if not morse[item]
#will be false and it will raise the KeyError that key not found in the dictionary which will be handled
#as a normal error at the end of the code.
store = store + morse[item]
#Prints the decoded code stored in the variable 'store'
print('Your Decoded Message is: %s' % store)
# In case of the above code returns false due to morse[item]
except KeyError as error:
#Display error
print('Your code contains invalid morse code character', error)Compiled Source Download: http://www.mediafire.com/?nujm7sgcxcliu97 Everything is explained in the code comments, Have fun! If you like, you can make it encode the normal text into a morse, you just need to reverse the dictionary values and then ask for normal text
RE: Morse Decoder - Deque - 04-18-2013 Well done, @Ex094. I thought lately that we could make a multi encoder for HC that includes morse code as well. RE: Morse Decoder - Deque - 04-18-2013 Well done, @Ex094. I thought lately that we could make a multi encoder for HC that includes morse code as well. RE: Morse Decoder - Psycho_Coder - 04-18-2013 Wow that looks good , I think I am going to make one in C++ or in VB.NET too . Seeing this I remembered Coder-sans contest in which we were given a morse code for decoding .. RE: Morse Decoder - Psycho_Coder - 04-18-2013 Wow that looks good , I think I am going to make one in C++ or in VB.NET too . Seeing this I remembered Coder-sans contest in which we were given a morse code for decoding .. RE: Morse Decoder - Anima Templi - 04-18-2013 Really great job! I like that you are still making tools in Python. And you did a very great job at explaining that code also. RE: Morse Decoder - Anima Templi - 04-18-2013 Really great job! I like that you are still making tools in Python. And you did a very great job at explaining that code also. RE: Morse Decoder - Coder-san - 04-18-2013 Nice. A job well done. RE: Morse Decoder - Coder-san - 04-18-2013 Nice. A job well done. |