in

Decodificador de código Morse simple en Python

18VvpVJUPjj8C i7i1d8 tQ

Decodificador de código Morse simple en Python

Kieran Tan Kah Wang

14 de oct de 2020·5 min de lectura

1*8VvpVJUPjj8C i7i1d8 tQ
Por Rhey T. Snodgrass & amp; Victor F. Camp, 1922 – Imagen: Intcode.png e Imagen: Código Morse internacional.PNG, dominio público, https://commons.wikimedia.org/w/index.php?curid=3902977

Representación del código Morse en Python

character = ['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']code = ['01','1000','1010','100','0','0010','110','0000','00','0111','101','0100','11','10','111','0110','1101','010','000','1','001','0001','011','1001','1011','1100','11111','01111','00111','00011','00001','00000','10000','11000','11100','11110']
# 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_dict.items():
print(key, value)
1*SSf74sRbFH1POvyjlsY1JQ

Imagen del autor: salida de Python del diccionario que contiene la representación del código Morse

Construyendo el decodificador de código Morse

# 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. Leave blank to quit. ")
# 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[j] in rev_morse_dict.keys():
dec_msg.append(rev_morse_dict[new_msg[j]])

# printing out the decoded message
print ("Decoded Message is: " + ''.join(dec_msg) + "n")
# end the infinite while loop
break
1*vtObQmFpO6XVk68DXTBXmg

Imagen del autor – Salida de Python del decodificador de código Morse

Comprobación de errores

Imagen del autor: salida de Python del decodificador de código Morse con entrada ‘abc’
# counter to do error check
i = 0
# proceed if user enters something
if (len(input_msg)!=0):
# while loop to check if only 0, 1 or * has been keyed in
while (i<len(input_msg)):
if input_msg[i] == "0" or input_msg[i] == "1" or input_msg[i] == "*":
i+=1
else:
# if invalid chars found, print error message
print ("Error input, Morse Code sequence should only consists of 0 and 1, with a * between subsequence")
# break the error check once one error has been found
break
1*KOkjC0v aLsuP02p6tOBfQ

Imagen del autor: salida de Python del nuevo decodificador de código Morse con entrada ‘abc’
# 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[j] in rev_morse_dict.keys():
dec_msg.append(rev_morse_dict[new_msg[j]])
# if morse code representation not found in original list, append input to err_msg
else:
err_msg.append(new_msg[j])

# to print error message only when necessary
if len(err_msg) != 0:
for line in err_msg:
print(line + " has no known morse translation")
1*Qx cDB

Imagen del autor: salida de Python del nuevo decodificador de código Morse con entrada de código Morse no válida
1*eAN9u 0 l5b

Imagen del autor – Salida de Python del nuevo decodificador de código Morse

Limitaciones

Resumen

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *

644c160b2320c5d75fb9ad0967db9287 1200 80

Trucos de GTA 5: códigos de trucos y números de teléfono para PS4, Xbox One y PC

WebLogic Server 12c (12.2.1), WebLogic Server 11g (10.3.6) y versiones anteriores