Skip to content

Update morse_code_implementation.py #2386

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 25, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 20 additions & 30 deletions ciphers/morse_code_implementation.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Python program to implement Morse Code Translator


# Dictionary representing the morse code chart
MORSE_CODE_DICT = {
"A": ".-",
Expand Down Expand Up @@ -39,56 +38,47 @@
"8": "---..",
"9": "----.",
"0": "-----",
", ": "--..--",
"&": ".-...",
"@": ".--.-.",
":": "---...",
",": "--..--",
".": ".-.-.-",
"'": ".----.",
'"': ".-..-.",
"?": "..--..",
"/": "-..-.",
"=": "-...-",
"+": ".-.-.",
"-": "-....-",
"(": "-.--.",
")": "-.--.-",
# Exclamation mark is not in ITU-R recommendation
"!": "-.-.--",
}


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

Expand Down