Update morse_code_implementation.py (#2386)

* Update morse_code_implementation.py

Added more characters to MORSE_CODE_DICT for a more complete dictionary.
Split words with "/" instead of a space as is standard.
Fixed bug when encrypting a message with a comma.

* Fixed comment typo
This commit is contained in:
Thomas Voss 2020-09-25 19:03:15 +02:00 committed by GitHub
parent b81fcef66b
commit e92cd9d5c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,5 @@
# Python program to implement Morse Code Translator
# Dictionary representing the morse code chart
MORSE_CODE_DICT = {
"A": ".-",
@ -39,13 +38,22 @@ MORSE_CODE_DICT = {
"8": "---..",
"9": "----.",
"0": "-----",
", ": "--..--",
"&": ".-...",
"@": ".--.-.",
":": "---...",
",": "--..--",
".": ".-.-.-",
"'": ".----.",
'"': ".-..-.",
"?": "..--..",
"/": "-..-.",
"=": "-...-",
"+": ".-.-.",
"-": "-....-",
"(": "-.--.",
")": "-.--.-",
# Exclamation mark is not in ITU-R recommendation
"!": "-.-.--",
}
@ -53,42 +61,24 @@ def encrypt(message):
cipher = ""
for letter in message:
if letter != " ":
cipher += MORSE_CODE_DICT[letter] + " "
else:
cipher += "/ "
cipher += " "
return cipher
# Remove trailing space added on line 64
return cipher[:-1]
def decrypt(message):
message += " "
decipher = ""
citext = ""
for letter in message:
if letter != " ":
i = 0
citext += letter
letters = message.split(" ")
for letter in letters:
if letter != "/":
decipher += list(MORSE_CODE_DICT.keys())[
list(MORSE_CODE_DICT.values()).index(letter)
]
else:
i += 1
if i == 2:
decipher += " "
else:
decipher += list(MORSE_CODE_DICT.keys())[
list(MORSE_CODE_DICT.values()).index(citext)
]
citext = ""
decipher += " "
return decipher