-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmorse_code.py
More file actions
84 lines (76 loc) · 3.14 KB
/
morse_code.py
File metadata and controls
84 lines (76 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# !/usr/bin/env python3
class MorseCode:
"""Python program to implement Morse Code Translator
VARIABLE KEY
"cipher" -> "stores the morse translated form of the english string"
"decipher" -> "stores the english translated form of the morse string"
"citext" -> "stores morse code of a single character"
"i" -> "keeps count of the spaces between morse characters"
"message" -> "stores the string to be encoded or decoded"
"""
# Dictionary representing the morse code chart
MORSE_CODE_DICT = {
"A":".-", "B":"-...", "C":"-.-.", "D":"-..", "E":".",
"F":"..-.", "G":"--.", "H":"....", "I":"..", "J":".---",
"K":"-.-", "L":".-..", "M":"--", "N":"-.", "O":"---",
"P":".--.", "Q":"--.-", "R":".-.", "S":"...", "T":"-",
"U":"..-", "V":"...-", "W":".--", "X":"-..-", "Y":"-.--",
"Z":"--..", "1":".----", "2":"..---", "3":"...--",
"4":"....-", "5":".....", "6":"-....", "7":"--...", "8":"---..",
"9":"----.", "0":"-----", ", ":"--..--", ".":".-.-.-",
"?":"..--..", "/":"-..-.", "-":"-....-", "(":"-.--.", ")":"-.--.-",
"=":"-...-"
}
def encode_morse_code(self, input_string: str) -> str:
"""Conversion from Morse Code value to ASCII
Args:
str: ASCII encoded string
One (1) space indicates different character
Two (2) spaces indicates different word
Returns:
str: Morse Code string to convert
"""
cipher = ""
input_string = input_string.upper()
for c in input_string:
if c != " ":
# Looks up the dictionary and adds the corresponding morse code
# along with a space to separate morse codes for different
# characters
cipher += f"""{MorseCode.MORSE_CODE_DICT[c] + " "}"""
else:
cipher += " "
return cipher
def decode_morse_code(self, input_string: str) -> str:
"""Conversion from Morse Code value to ASCII.
Args:
str: Morse Code string to convert
Returns:
str: ASCII encoded string
"""
# Extra space added at the end to access the last morse code
input_string += " "
decipher = ""
citext = ""
for c in input_string:
# Checks for space
if (c != " "):
# Counter to keep track of space
i = 0
# Storing morse code of a single character
citext += c
# In case of space
else:
# If i = 1 that indicates a new character
i += 1
# If i = 2 that indicates a new word
if i == 2 :
# Adding space to separate words
decipher += " "
else:
# Accessing the keys using their values (reverse of encryption)
decipher += list(
MorseCode.MORSE_CODE_DICT.keys())[list(
MorseCode.MORSE_CODE_DICT.values()).index(citext)]
citext = ""
return decipher