-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
130 lines (106 loc) · 3.86 KB
/
main.py
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import random
import time
from tree import drawTree # This library will only be used to draw the binary tree on the screen
# A class to implement a Node / Tree
class Node:
def __init__(self, value, left=None, right=None):
self.value = value
self.left = left
self.right = right
# Convert Character (Find the character using a pre-order traversal of the Binary Tree)
def getMorseCode(node, character, code):
if node is None:
return False
elif node.value == character:
return True
else:
if getMorseCode(node.left, character, code):
code.insert(0, ".")
return True
elif getMorseCode(node.right, character, code):
code.insert(0, "-")
return True
# Convert Morse Code to Character
def getCharacterFromMorse(node, morse_code):
current_node = node
for symbol in morse_code:
if symbol == ".":
current_node = current_node.left
elif symbol == "-":
current_node = current_node.right
if current_node is None:
return None
return current_node.value
# Let's initialise our binary tree:
tree = Node("START") # The root node of our binary tree
# 1st Level
tree.left = Node("E")
tree.right = Node("T")
# 2nd Level
tree.left.left = Node("I")
tree.left.right = Node("A")
tree.right.left = Node("N")
tree.right.right = Node("M")
# 3rd Level
tree.left.left.left = Node("S")
tree.left.left.right = Node("U")
tree.left.right.left = Node("R")
tree.left.right.right = Node("W")
tree.right.left.left = Node("D")
tree.right.left.right = Node("K")
tree.right.right.left = Node("G")
tree.right.right.right = Node("O")
# 4th Level
tree.left.left.left.left = Node("H")
tree.left.left.left.right = Node("V")
tree.left.left.right.left = Node("F")
tree.left.left.right.right = Node("")
tree.left.right.left.left = Node("L")
tree.left.right.left.right = Node("")
tree.left.right.right.left = Node("P")
tree.left.right.right.right = Node("J")
tree.right.left.left.left = Node("B")
tree.right.left.left.right = Node("X")
tree.right.left.right.left = Node("C")
tree.right.left.right.right = Node("Y")
tree.right.right.left.left = Node("Z")
tree.right.right.left.right = Node("Q")
tree.right.right.right.left = Node("")
tree.right.right.right.right = Node("")
drawTree(tree)
# Main program starts here...
while True:
choice = input("Do you want to (E)ncode or (D)ecode a message? (E/D): ").upper()
if choice == "E":
message = input("Enter a message to convert into Morse Code: (e.g. SOS)").upper()
morseCode = ""
# Convert the message, one character at a time!
for character in message:
dotsdashes = []
if getMorseCode(tree, character, dotsdashes):
code = "".join(dotsdashes)
morseCode = morseCode + code + " "
else:
morseCode = morseCode + "? "
print("Morse Code:")
print(morseCode)
elif choice == "D":
morse_message = input("Enter a Morse Code message to decode (use space to separate letters): ").strip()
words = morse_message.split(" ")
decoded_message = ""
for code in words:
if code.strip() != "":
character = getCharacterFromMorse(tree, code)
if character:
decoded_message += character
else:
decoded_message += "?"
else:
decoded_message += " "
print("Decoded Message:")
print(decoded_message)
else:
print("Invalid choice. Please enter 'E' to encode or 'D' to decode.")
again = input("Do you want to encode/decode another message? (Y/N): ").upper()
if again != "Y":
break