diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..c18dd8d83ceed1806b50b0aaa46beb7e335fff13 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__/ diff --git a/README.md b/README.md index 5d950e5858e01f93a7bf8f37789aa1147382c51d..6bf7c90c61d351f1121bc8807617cc34703f3d10 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/main.py b/main.py new file mode 100644 index 0000000000000000000000000000000000000000..533fa29823cfd2d23c5d488e2745fa3fc0e11eb3 --- /dev/null +++ b/main.py @@ -0,0 +1,7 @@ +import morse + +if __name__ == "__main__": + e = morse.encode('us') + print(e) + d = morse.decode(e) + print(d) diff --git a/morse.py b/morse.py index 25c0d5068c6f162723a803144b17eaccd0f8c2d4..125e84c3119f61c461f87ed691f39a5bf2a61f3d 100644 --- a/morse.py +++ b/morse.py @@ -3,6 +3,7 @@ class node: self.value = value 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) - decodes.append(self.decode(y)) - # print(self.decode(y)) + if y != "": + decodes.append(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,38 +60,65 @@ class node: return self.value - def encode(): - #dictionary ie e == --.-.-.-..- - - x = x - -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 = [] - -start = node("start") - -queue.append(start) -count = 0 - -while count < len(alphabet)-1: - # print(count) + 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 = [] + + start = node("start") + + queue.append(start) + count = 0 + + while count < len(alphabet)-1: + parent = queue.pop(0) + childLeft = node(alphabet[count]) + parent.setLeftChildNode(childLeft) + queue.append(childLeft) + count += 1 + childRight = node(alphabet[count]) + parent.setRightChildNode(childRight) + queue.append(childRight) + count += 1 + # start.printNode() + + # print(start.words("..- ..-")) + + # print(start.encode("us")) + return start + +def encode(value): + tree = initTree() + return tree.encode(value) - parent = queue.pop(0) - # print(parent.value) - # print(alphabet[count]) - childLeft = node(alphabet[count]) - parent.setLeftChildNode(childLeft) - queue.append(childLeft) - count += 1 - childRight = node(alphabet[count]) - parent.setRightChildNode(childRight) - queue.append(childRight) - count += 1 - -start.printNode() - -print(start.words("..- ..-")) +def decode(value): + tree = initTree() + return tree.words(value)