Skip to content

Add Morse Code Translator #430

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions scripts/Morse Code Translator/MorseCodeTranslator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
MORSE_CODES = {
"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_CODES[letter] + " "
else:
cipher += " "
return cipher


def decrypt(message):
message += " "
decipher = ""
citext = "" # stores morse code of a single character
for letter in message:
if letter != " ":
i = 0 # keeps count of the spaces between morse characters
citext += letter
else:
i += 1
if i == 2:
decipher += " "
else:
decipher += list(MORSE_CODES.keys())[
list(MORSE_CODES.values()).index(citext)
]
citext = ""
return decipher


def main():
while True:
print('''
-----------------------------
Morse Code Translator
1. Encrypt
2. Decrypt

Enter 'q' to quit.
-----------------------------
''')

choice = input("Enter your choice: ")
if choice == "1":
message = input("Enter message to encrypt: ")
result = encrypt(message.upper())
print("> ", result)
elif choice == "2":
message = input("Enter message to decrypt: ")
result = decrypt(message)
print("> ", result)
elif choice == "q":
break
else:
print("Invalid choice. Try again.")


if __name__ == "__main__":
main()
25 changes: 25 additions & 0 deletions scripts/Morse Code Translator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Morse Code Translator

This simple script translates texts into morse code and vise versa.

## Usage

It has two options:-
1). Encrypt a text into a morse code.
2). Decrypt a morse code into a text.

Just run it and follow the instructions. Here is a simple example.

```shell
-----------------------------
Morse Code Translator
1. Encrypt
2. Decrypt

Enter 'q' to quit.
-----------------------------

Enter your choice: 1
Enter message to encrypt: Metafy social
> -- . - .- ..-. -.-- ... --- -.-. .. .- .-..
```