12/27/2023 0 Comments Morse decoder 01Image by author - Python output of Morse Code Decoder with ‘abc’ input ") # append input_msg (string) to ori_msg (string) ori_msg.append(input_msg) # split each morse code by '*' new_msg = input_msg.split("*") # printing out the original message for line in ori_msg: # to print original message without the print("Original message: " + line + "\n") # loop through each morse code representation for j in range (0,len(new_msg)): # get the alphanumeric alphabet based on the dict keys and append to dec_msg if new_msg in rev_morse_dict.keys(): dec_msg.append(rev_morse_dict]) # printing out the decoded message print ("Decoded Message is: " + ''.join(dec_msg) + "\n") # end the infinite while loop break # reverse the previous dict as it's easier to access the keys zipped_code_char = zip(code,character) rev_morse_dict = dict(list(zipped_code_char)) # initiating a while loop while True: # empty list to store original message ori_msg = # empty list to store decoded message dec_msg = # prompt the user to input morse code input_msg = input ("Please enter any Morse Code sequence for decoding. Once the user pressed ‘enter’, the program will decode the Morse Code and displays it in alphanumeric form. in 0s and 1s), with each alphabet or number separated by a *. The way the Morse Decoder will be built is that we will prompt the user to key in the Morse Code representation (i.e. Image by author - Python output of dictionary containing the Morse Code Representation Building the Morse Code Decoder # Define an empty dictionary 'morse_dict' morse_dict = # Convert the 2 lists into a dictionary using a tuple zipped_char_code = zip(character, code) morse_dict = dict(zipped_char_code) # Print the dictionary 'morse_dict' on the terminal line by line for key, value in morse_ems(): print(key, value) You can refer to my previous post on building a dictionary data structure in Python here. an alphabet or number) corresponds to a series of 0s and 1s, we will use a dictionary structure to store them in Python. character = code = Īs each character (i.e. We can represent them as such in Python, but for better clarity, let’s translate them to ‘0’ and ‘1’ instead, where ‘0’ represents a dot, and ‘1’ represents a dash. Morse Code Representation in PythonĪs seen in the image above, each alphabet and number is represented by a series of dots and dashes. This article will describe the process to build a simple Morse Code decoder in Python. It was first invented by Samuel Morse in 1930s and it has been heavily used in the navy industry. Morse code is a method used in telecommunication where each alphabet, number and punctuation is represented by a series of dots/dashes/spaces. Camp, 1922 - Image:Intcode.png and Image:International Morse Code.PNG, Public Domain,
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |