2019-05-26 16:37:40 +00:00
|
|
|
# Python program to implement Morse Code Translator
|
|
|
|
|
|
|
|
|
|
|
|
# Dictionary representing the morse code chart
|
2019-10-05 05:14:13 +00:00
|
|
|
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": "-----",
|
|
|
|
", ": "--..--",
|
|
|
|
".": ".-.-.-",
|
|
|
|
"?": "..--..",
|
|
|
|
"/": "-..-.",
|
|
|
|
"-": "-....-",
|
|
|
|
"(": "-.--.",
|
|
|
|
")": "-.--.-",
|
|
|
|
}
|
2019-05-26 16:37:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
def encrypt(message):
|
2019-10-05 05:14:13 +00:00
|
|
|
cipher = ""
|
2019-05-26 16:37:40 +00:00
|
|
|
for letter in message:
|
2019-10-05 05:14:13 +00:00
|
|
|
if letter != " ":
|
2019-05-26 16:37:40 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
cipher += MORSE_CODE_DICT[letter] + " "
|
2019-05-26 16:37:40 +00:00
|
|
|
else:
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
cipher += " "
|
2019-05-26 16:37:40 +00:00
|
|
|
|
|
|
|
return cipher
|
|
|
|
|
|
|
|
|
|
|
|
def decrypt(message):
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
message += " "
|
2019-05-26 16:37:40 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
decipher = ""
|
|
|
|
citext = ""
|
2019-05-26 16:37:40 +00:00
|
|
|
for letter in message:
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
if letter != " ":
|
2019-05-26 16:37:40 +00:00
|
|
|
|
|
|
|
i = 0
|
|
|
|
|
|
|
|
citext += letter
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
i += 1
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
if i == 2:
|
2019-05-26 16:37:40 +00:00
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
decipher += " "
|
2019-05-26 16:37:40 +00:00
|
|
|
else:
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
decipher += list(MORSE_CODE_DICT.keys())[
|
|
|
|
list(MORSE_CODE_DICT.values()).index(citext)
|
|
|
|
]
|
|
|
|
citext = ""
|
2019-05-26 16:37:40 +00:00
|
|
|
|
|
|
|
return decipher
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
message = "Morse code here"
|
|
|
|
result = encrypt(message.upper())
|
2019-08-06 10:14:23 +00:00
|
|
|
print(result)
|
2019-05-26 16:37:40 +00:00
|
|
|
|
|
|
|
message = result
|
|
|
|
result = decrypt(message)
|
2019-08-06 10:14:23 +00:00
|
|
|
print(result)
|
2019-05-26 16:37:40 +00:00
|
|
|
|
|
|
|
|
2019-10-05 05:14:13 +00:00
|
|
|
if __name__ == "__main__":
|
2019-05-26 16:37:40 +00:00
|
|
|
main()
|