diff --git a/scripts/Morse Code Translator/MorseCodeTranslator.py b/scripts/Morse Code Translator/MorseCodeTranslator.py new file mode 100644 index 0000000..42993e6 --- /dev/null +++ b/scripts/Morse Code Translator/MorseCodeTranslator.py @@ -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() diff --git a/scripts/Morse Code Translator/README.md b/scripts/Morse Code Translator/README.md new file mode 100644 index 0000000..8fcae86 --- /dev/null +++ b/scripts/Morse Code Translator/README.md @@ -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 +> -- . - .- ..-. -.-- ... --- -.-. .. .- .-.. +```