That's awesome! Use Python to implement Morse code translator, netizen: "How to knock I love you?"

Morse code is a method of transmitting text information as a series of on and off tones, lights or clicks, and can be understood directly by skilled listeners or observers without special equipment. It is named after Samuel FB Morse, the inventor of the telegraph.

algorithm

The algorithm is very simple. Every character in English is replaced by a series of "dots" and "dashes", sometimes just singular "dots" or "dashes", and vice versa.

encryption

  1. In the case of encryption, we extract each character (if not a space) from a word at a time and match it with the corresponding Morse code stored in any data structure of our choice (if you use python to encode, The dictionary can prove to be very useful in this case)
  2. Store the Morse code in a variable that will contain our encoded string, and then add a space to the string, which will contain the result.
  3. When encoding with Morse code, we need to add 1 space between each character and 2 consecutive spaces between each word.
  4. If the character is a space, add another space to the variable containing the result. We repeat this process until we traverse the entire string

Decryption method

  1. In the case of decryption, we first add a space at the end of the string to be decoded (this will be explained later).
  2. Now, we continue to extract characters from the string until there are no spaces.
  3. Once we get the space, we look for the corresponding English character in the extracted character sequence (or Morse code) and add it to the variable where the result will be stored.
  4. Remember that tracking space is the most important part of this decryption process. Once we get 2 consecutive spaces, we will add another space to the variable containing the decoded string.
  5. The last space at the end of the string will help us identify the last sequence of Morse code characters (because the space can be used as a check to extract characters and start decoding them).

carried out

Python provides a data structure called a dictionary, which stores information in the form of key-value pairs, which is very convenient for implementing passwords (such as Morse code). We can save the Morse code table in the dictionary, where (key-value pair) => (English character-Morse code) . The plain text (English characters) replaces the key, and the cipher text (Morse code) forms the value of the corresponding key. You can access the value of the key from the dictionary, just as we access the value of the array through their index, and vice versa.


''' 
VARIABLE KEY 
'cipher' -> 'stores the morse translated form of the english string' 
'decipher' -> 'stores the english translated form of the morse string' 
'citext' -> 'stores morse code of a single character' 
'i' -> 'keeps count of the spaces between morse characters' 
'message' -> 'stores the string to be encoded or decoded' 
'''

MORSE_CODE_DICT = { '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':'--..', 
					'1':'.----', '2':'..---', '3':'...--', 
					'4':'....-', '5':'.....', '6':'-....', 
					'7':'--...', '8':'---..', '9':'----.', 
					'0':'-----', ', ':'--..--', '.':'.-.-.-', 
					'?':'..--..', '/':'-..-.', '-':'-....-', 
					'(':'-.--.', ')':'-.--.-'} 

def encrypt(message): 
	cipher = '' 
	for letter in message: 
		if letter != ' ': 

			cipher += MORSE_CODE_DICT[letter] + ' '
		else: 
			cipher += ' '

	return cipher 

def decrypt(message): 

	message += ' '

	decipher = '' 
	citext = '' 
	for letter in message: 

		if (letter != ' '): 

			i = 0

			citext += letter 

		else: 
			i += 1

			if i == 2 : 

				decipher += ' '
			else: 

				decipher += list(MORSE_CODE_DICT.keys())[list(MORSE_CODE_DICT 
				.values()).index(citext)] 
				citext = '' 

	return decipher 

def main(): 
	message = "GEEKS-FOR-GEEKS"
	result = encrypt(message.upper()) 
	print (result) 

	message = "--. . . -.- ... -....- ..-. --- .-. -....- --. . . -.- ... "
	result = decrypt(message) 
	print (result) 

if __name__ == '__main__': 
	main() 

Output:

-。。。-.- ... -....- ..- -.. -....--。。。-.- ...
GEEKS-FOR-GEEKS

I still want to recommend the Python learning group I built myself : 705933274. The group is all learning Python. If you want to learn or are learning Python, you are welcome to join. Everyone is a software development party and share dry goods from time to time (only Python software development related), including a copy of the latest Python advanced materials and zero-based teaching compiled by myself in 2021. Welcome friends who are in advanced and interested in Python to join!

Guess you like

Origin blog.csdn.net/pyjishu/article/details/114746372