mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-03-20 13:39:48 +00:00
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:
parent
b81fcef66b
commit
e92cd9d5c5
@ -1,6 +1,5 @@
|
|||||||
# Python program to implement Morse Code Translator
|
# Python program to implement Morse Code Translator
|
||||||
|
|
||||||
|
|
||||||
# Dictionary representing the morse code chart
|
# Dictionary representing the morse code chart
|
||||||
MORSE_CODE_DICT = {
|
MORSE_CODE_DICT = {
|
||||||
"A": ".-",
|
"A": ".-",
|
||||||
@ -39,13 +38,22 @@ MORSE_CODE_DICT = {
|
|||||||
"8": "---..",
|
"8": "---..",
|
||||||
"9": "----.",
|
"9": "----.",
|
||||||
"0": "-----",
|
"0": "-----",
|
||||||
|
"&": ".-...",
|
||||||
|
"@": ".--.-.",
|
||||||
|
":": "---...",
|
||||||
",": "--..--",
|
",": "--..--",
|
||||||
".": ".-.-.-",
|
".": ".-.-.-",
|
||||||
|
"'": ".----.",
|
||||||
|
'"': ".-..-.",
|
||||||
"?": "..--..",
|
"?": "..--..",
|
||||||
"/": "-..-.",
|
"/": "-..-.",
|
||||||
|
"=": "-...-",
|
||||||
|
"+": ".-.-.",
|
||||||
"-": "-....-",
|
"-": "-....-",
|
||||||
"(": "-.--.",
|
"(": "-.--.",
|
||||||
")": "-.--.-",
|
")": "-.--.-",
|
||||||
|
# Exclamation mark is not in ITU-R recommendation
|
||||||
|
"!": "-.-.--",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -53,42 +61,24 @@ def encrypt(message):
|
|||||||
cipher = ""
|
cipher = ""
|
||||||
for letter in message:
|
for letter in message:
|
||||||
if letter != " ":
|
if letter != " ":
|
||||||
|
|
||||||
cipher += MORSE_CODE_DICT[letter] + " "
|
cipher += MORSE_CODE_DICT[letter] + " "
|
||||||
else:
|
else:
|
||||||
|
cipher += "/ "
|
||||||
|
|
||||||
cipher += " "
|
# Remove trailing space added on line 64
|
||||||
|
return cipher[:-1]
|
||||||
return cipher
|
|
||||||
|
|
||||||
|
|
||||||
def decrypt(message):
|
def decrypt(message):
|
||||||
|
|
||||||
message += " "
|
|
||||||
|
|
||||||
decipher = ""
|
decipher = ""
|
||||||
citext = ""
|
letters = message.split(" ")
|
||||||
for letter in message:
|
for letter in letters:
|
||||||
|
if letter != "/":
|
||||||
if letter != " ":
|
|
||||||
|
|
||||||
i = 0
|
|
||||||
|
|
||||||
citext += letter
|
|
||||||
|
|
||||||
else:
|
|
||||||
|
|
||||||
i += 1
|
|
||||||
|
|
||||||
if i == 2:
|
|
||||||
|
|
||||||
decipher += " "
|
|
||||||
else:
|
|
||||||
|
|
||||||
decipher += list(MORSE_CODE_DICT.keys())[
|
decipher += list(MORSE_CODE_DICT.keys())[
|
||||||
list(MORSE_CODE_DICT.values()).index(citext)
|
list(MORSE_CODE_DICT.values()).index(letter)
|
||||||
]
|
]
|
||||||
citext = ""
|
else:
|
||||||
|
decipher += " "
|
||||||
|
|
||||||
return decipher
|
return decipher
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user