Morse code translator in Python

Counters | File handling | Python Methods and Functions

Morse Code — it is a method of transmitting textual information in the form of a sequence of tones, lights or clicks that can be understood by an experienced listener or observer without special equipment. It is named after Samuel FB Morse, the inventor of the telegraph.

The algorithm

The algorithm is very simple. Every character in English is replaced by a series of "dots" and "dashes", and sometimes just single "dots" or "dashes" and vice versa.
Please refer to this Wikipedia image for details.

encryption

  1. In the case of encryption, we extract each character (if not a space) from a word one at a time and match it with the corresponding Morse code stored in any the data structure we have chosen (if you are coding in python, dictionaries can be very useful in this case)
  2. Store the Morse code in a variable that will contain our encoded string, and then we will add to our string a space that will contain the result.
  3. When encoding in 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 have traversed the entire line

Decryption

  1. In case of decryption, we start by adding a space at the end of the line to decode (this will be explained later).
  2. Now we continue to extract characters from the string until we have a space.
  3. Once we have a space, we look for the appropriate English character for the extracted character sequence (or our Morse code) and add it to a variable that will hold the result.
  4. Remember that space tracking is the most important part of this decryption process. As soon as we have 2 consecutive spaces, we add another space to our variable containing the decoded string.
  5. The last space at the end of the line will help us determine the last sequence of Morse code characters (since the space acts as a check for extracting characters and start decoding).

Implementation

Python provides a data structure called dictionary, which stores information as key-value pairs, which is very handy for implementing a cipher like Morse code. We can store the Morse code in a dictionary where (key-value pairs) = & gt; (English characters — Morse code) . The plaintext (English characters) replaces the keys, and the ciphertext (Morse code) forms the values ​​of the corresponding keys. Key values ​​can be accessed from a dictionary in the same way we access array values ​​through their index and vice versa.

  Output:  -... -.- ... -....- ..-. --- .-. -....- -... -.- ... GEEKS-FOR-GEEKS 

This article courtesy of Palash Nigam . If you are as Python.Engineering and would like to contribute alright, you can also write an article using contribute.python.engineering or mail the article to [email protected] See your article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong.or would you like to share more information on the topic discussed above.





Get Solution for free from DataCamp guru

# Python program for implementing Morse code

 
"" "
VARIABLE KEY
& # 39; cipher & # 39; - & gt; & # 39; stores the morse-translated form of the English string & # 39;
& # 39; decipher & # 39; - & gt; & # 39; stores the morse form translated into English & # 39;
& # 39; citext & # 39; - & gt; & # 39; stores the Morse code of one character & # 39;
& # 39; i & # 39; - & gt; & # 39; counts the spaces between Morse code & # 39;
& # 39; message & # 39; - & gt; & # 39; saves a string to encode or decode & # 39;
"" "

  
# Dictionary representing Morse code

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` : `-----` , `,` : `--..--` , `.` : `.-.-.- ` ,

  `? ` : `..-- ..` , `/` : `-..-.` , `-` : ` -....- ` ,

`(` : `-.--.` , `) ` : ` -.--. -` }

 
# Function for string encryption
# according to Morse diagram

def encrypt (message):

cipher = ``

for letter in message:

if letter! = `` :

 

# Searches a dictionary and adds

# relevant code Morse code

# along with a space to separate

# Morse code for different characters

  cipher + = MORSE_CODE_DICT [ letter] + ``

else :

# 1 space means different characters

  # and 2 denote different words

  cipher + = ``

 

return cipher

 
# Function for line decoding
# from Morse to English

def decrypt (message):

 

# added extra space at the end to access

# latest Morse code

  message + = ``

 

decipher = ``

citext = ``

for letter in message:

 

  # checks location

if (letter! = `` ):

  

# counter to track space

i = 0

  

  # storing one character Morse code

citext + = letter

 

  # if there is a space

else :

# if i = 1, which indicates a new character

  i + = 1

 

# if i = 2, indicating a new word

if i = = 2 :

 

# add space to individual words

decipher + = ` `

  else :

  

# access to keys using their values ​​(reverse encryption )

decipher + = list (MORSE_CODE_DICT.keys ()) [ list (MORSE_CODE_DICT

.values ​​()). index (citext)]

  citext = ``

 

return decipher

 
# Hard-coded function I am a driver to run the program

def main ():

message = " GEEKS-FOR-GEEKS "

  result = encrypt (message.upper ())

print (result)

 

message = "- -. ... ... -.- ... -....- ..-. --- .-. -....- -. ... ... -.- ... "

  result = decrypt (message)

  print (result)

 
# Performs the main function

if __ name__ = = ` __main__` :

main ()