Python/ciphers/morse_code_implementation.py
Dhruv Manilawala 60895366c0
fix(mypy): type annotations for cipher algorithms (#4306)
* fix(mypy): type annotations for cipher algorithms

* Update mypy workflow to include cipher directory

* fix: mypy errors in hill_cipher.py

* fix build errors
2021-04-04 07:22:12 +02:00

98 lines
1.8 KiB
Python

# Python program to implement Morse Code Translator
# Dictionary representing the morse code chart
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": "-----",
"&": ".-...",
"@": ".--.-.",
":": "---...",
",": "--..--",
".": ".-.-.-",
"'": ".----.",
'"': ".-..-.",
"?": "..--..",
"/": "-..-.",
"=": "-...-",
"+": ".-.-.",
"-": "-....-",
"(": "-.--.",
")": "-.--.-",
# Exclamation mark is not in ITU-R recommendation
"!": "-.-.--",
}
def encrypt(message: str) -> str:
cipher = ""
for letter in message:
if letter != " ":
cipher += MORSE_CODE_DICT[letter] + " "
else:
cipher += "/ "
# Remove trailing space added on line 64
return cipher[:-1]
def decrypt(message: str) -> str:
decipher = ""
letters = message.split(" ")
for letter in letters:
if letter != "/":
decipher += list(MORSE_CODE_DICT.keys())[
list(MORSE_CODE_DICT.values()).index(letter)
]
else:
decipher += " "
return decipher
def main() -> None:
message = "Morse code here"
result = encrypt(message.upper())
print(result)
message = result
result = decrypt(message)
print(result)
if __name__ == "__main__":
main()