Skip to content

Commit e92cd9d

Browse files
authored
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
1 parent b81fcef commit e92cd9d

File tree

1 file changed

+20
-30
lines changed

1 file changed

+20
-30
lines changed

Diff for: ciphers/morse_code_implementation.py

+20-30
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# Python program to implement Morse Code Translator
22

3-
43
# Dictionary representing the morse code chart
54
MORSE_CODE_DICT = {
65
"A": ".-",
@@ -39,56 +38,47 @@
3938
"8": "---..",
4039
"9": "----.",
4140
"0": "-----",
42-
", ": "--..--",
41+
"&": ".-...",
42+
"@": ".--.-.",
43+
":": "---...",
44+
",": "--..--",
4345
".": ".-.-.-",
46+
"'": ".----.",
47+
'"': ".-..-.",
4448
"?": "..--..",
4549
"/": "-..-.",
50+
"=": "-...-",
51+
"+": ".-.-.",
4652
"-": "-....-",
4753
"(": "-.--.",
4854
")": "-.--.-",
55+
# Exclamation mark is not in ITU-R recommendation
56+
"!": "-.-.--",
4957
}
5058

5159

5260
def encrypt(message):
5361
cipher = ""
5462
for letter in message:
5563
if letter != " ":
56-
5764
cipher += MORSE_CODE_DICT[letter] + " "
5865
else:
66+
cipher += "/ "
5967

60-
cipher += " "
61-
62-
return cipher
68+
# Remove trailing space added on line 64
69+
return cipher[:-1]
6370

6471

6572
def decrypt(message):
66-
67-
message += " "
68-
6973
decipher = ""
70-
citext = ""
71-
for letter in message:
72-
73-
if letter != " ":
74-
75-
i = 0
76-
77-
citext += letter
78-
74+
letters = message.split(" ")
75+
for letter in letters:
76+
if letter != "/":
77+
decipher += list(MORSE_CODE_DICT.keys())[
78+
list(MORSE_CODE_DICT.values()).index(letter)
79+
]
7980
else:
80-
81-
i += 1
82-
83-
if i == 2:
84-
85-
decipher += " "
86-
else:
87-
88-
decipher += list(MORSE_CODE_DICT.keys())[
89-
list(MORSE_CODE_DICT.values()).index(citext)
90-
]
91-
citext = ""
81+
decipher += " "
9282

9383
return decipher
9484

0 commit comments

Comments
 (0)