From e92cd9d5c5099cdcf8c80e7a7df2c1ff3190eacc Mon Sep 17 00:00:00 2001 From: Thomas Voss <57815710+Mango0x45@users.noreply.github.com> Date: Fri, 25 Sep 2020 19:03:15 +0200 Subject: [PATCH] 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 --- ciphers/morse_code_implementation.py | 50 +++++++++++----------------- 1 file changed, 20 insertions(+), 30 deletions(-) diff --git a/ciphers/morse_code_implementation.py b/ciphers/morse_code_implementation.py index 6df4632af..04af8fcf6 100644 --- a/ciphers/morse_code_implementation.py +++ b/ciphers/morse_code_implementation.py @@ -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