From 3b6c5a807e11e2eb06d495c83ce89cd21f5bbb4c Mon Sep 17 00:00:00 2001
From: jo2-holdsworth <jack2.holdsworth@live.uwe.ac.uk>
Date: Tue, 28 Feb 2023 16:16:53 +0000
Subject: [PATCH] task 2 complete + beginning of task 3

---
 .gitignore |   1 +
 README.md  |   3 ++
 main.py    |   7 ++++
 morse.py   | 103 ++++++++++++++++++++++++++++++++++-------------------
 4 files changed, 78 insertions(+), 36 deletions(-)
 create mode 100644 .gitignore
 create mode 100644 main.py

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..c18dd8d
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+__pycache__/
diff --git a/README.md b/README.md
index 5d950e5..6bf7c90 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 0000000..533fa29
--- /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 25c0d50..125e84c 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)
 
 
 
-- 
GitLab