Skip to content
Snippets Groups Projects
Commit 3b6c5a80 authored by jo2-holdsworth's avatar jo2-holdsworth
Browse files

task 2 complete + beginning of task 3

parent 7966caa5
Branches
No related tags found
No related merge requests found
__pycache__/
......@@ -52,6 +52,9 @@ Contributors names and contact info
ex. Jack Holdsworth @ [my website](https://holdsworth.dev)
## Version History
* 0.3
* task 2 done
* started task 3
* 0.2
* most of task 2 done
* README made
......
import morse
if __name__ == "__main__":
e = morse.encode('us')
print(e)
d = morse.decode(e)
print(d)
......@@ -4,6 +4,7 @@ class node:
self.leftChildNode = left
self.rightChildNode = right
def setLeftChildNode(self,nodes):
self.leftChildNode = nodes
......@@ -32,19 +33,22 @@ class node:
self.rightChildNode.printNode((level+1), "R")
def words(self,values):
ret = ""
if values:
x = values.split(" ")
decodes = []
for y in x:
print(self.value)
print("HELLO" + y)
if y != "":
decodes.append(self.decode(y))
# print(self.decode(y))
else:
decodes.append(" ")
return decodes
# ret = ret.join(decodes)
# print(f"{values} Decoded: {ret}")
# return f"{values} Decoded: {ret}"
return "".join(decodes)
def decode(self,values):
print(values)
if values:
if values[0] == ".":
return self.leftChildNode.decode(values[1:])
......@@ -56,11 +60,32 @@ class node:
return self.value
def encode():
#dictionary ie e == --.-.-.-..-
x = x
def encode(self,word, string = ""):
word = word.upper()
ret2 = ""
output = []
for letter in word:
ret = self.encoder(letter)
if ret != "":
output.append(ret)
output.append(" ")
# ret2 = ret2.join(output)
# return f"{word} encoded: {ret2}"
return "".join(output)
def encoder(self,letter, string = ""):
if letter == self.value:
return string
else:
if self.leftChildNode != None:
if self.leftChildNode.encoder(letter,(string + "-")) != "":
return self.leftChildNode.encoder(letter,(string + "."))
if self.rightChildNode != None:
if self.rightChildNode.encoder(letter,(string + ".")) != "":
return self.rightChildNode.encoder(letter,(string + "-"))
return ""
def initTree():
alphabet = ["E","T","I","A","N","M","S","U","R","W","D","K","G","O","H","V","F","","L","","P","","J","B","X","C","Y","Z","Q","",""]
queue = []
......@@ -70,11 +95,7 @@ queue.append(start)
count = 0
while count < len(alphabet)-1:
# print(count)
parent = queue.pop(0)
# print(parent.value)
# print(alphabet[count])
childLeft = node(alphabet[count])
parent.setLeftChildNode(childLeft)
queue.append(childLeft)
......@@ -83,11 +104,21 @@ while count < len(alphabet)-1:
parent.setRightChildNode(childRight)
queue.append(childRight)
count += 1
# start.printNode()
# print(start.words("..- ..-"))
# print(start.encode("us"))
return start
start.printNode()
def encode(value):
tree = initTree()
return tree.encode(value)
print(start.words("..- ..-"))
def decode(value):
tree = initTree()
return tree.words(value)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment