-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_morse.py
More file actions
80 lines (76 loc) · 3.5 KB
/
basic_morse.py
File metadata and controls
80 lines (76 loc) · 3.5 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
charDict = { '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':'-----', ', ':'--..--', '.':'.-.-.-',
'?':'..--..', '/':'-..-.', '-':'-....-',
'(':'-.--.', ')':'-.--.-'}
rawText = list(charDict.keys())
rawMorse = list(charDict.values())
class Encoder:
def def_Encode(inp):
encodedMorse = []
for char in inp:
if char.isalpha():
encodedMorse.append(charDict[char.upper()])
elif char in charDict:
encodedMorse.append(charDict[char])
elif char == " ":
encodedMorse.append(" ")
else:
print(f"Unrecognized character {char} in input. Kindly try again with valid characters.")
print("For a list of valid characters, input 'EncodingScheme' or 'EncSch'.")
return " ".join(encodedMorse)
def fileEncode(filename):
decodedLines = filename.readlines()
encodedLines = []
for line in decodedLines:
tempLine = []
for char in line:
if char.isalpha():
tempLine.append(charDict[char.upper()])
elif char in charDict:
tempLine.append(charDict[char])
elif char == " ":
tempLine.append(" ")
else:
print(f"Unrecognized character '{char}' in input. Ignoring the invalid character.")
print("For a list of valid characters, input 'EncodingScheme' or 'EncSch'.")
encodedLines.append(" ".join(tempLine))
return encodedLines,decodedLines
class Decoder:
def def_Decode(inp):
decodedText = []
for char in inp.split(" "):
if char in rawMorse:
decodedText.append(rawText[rawMorse.index(char)])
elif char == "":
decodedText.append(" ")
else:
print(f"Unrecognized character {char} in input. Kindly try again with valid characters.")
print("For a list of valid characters, input 'EncodingScheme' or 'EncSch'.")
return "".join(decodedText)
def fileDecode(filename):
encodedLines = filename.readlines()
decodedLines = []
for line in encodedLines.split(" "):
decodedText = []
for char in line:
if char in rawMorse:
decodedText.append(rawText[rawMorse.index(char)])
elif char == "":
decodedText.append(" ")
else:
print(f"Unrecognized character {char} in input. Kindly try again with valid characters.")
print("For a list of valid characters, input 'EncodingScheme' or 'EncSch'.")
decodedLines.append(" ".join(decodedText))
return decodedLines,encodedLines
#More functions soon