Stuff.
authorScott Gasch <[email protected]>
Wed, 29 Sep 2021 22:40:43 +0000 (15:40 -0700)
committerScott Gasch <[email protected]>
Wed, 29 Sep 2021 22:40:43 +0000 (15:40 -0700)
collect/bst.py
letter_compress.py
tests/run_tests.sh

index 8e95fa23aeb09c4d86ffd74055358fd54f2002c7..d3231eecaa22060d0fc47aca073e9e4f98a09310 100644 (file)
@@ -1,6 +1,6 @@
 #!/usr/bin/env python3
 
-from typing import Any, Optional
+from typing import Any, List, Optional
 
 
 class Node(object):
@@ -448,6 +448,58 @@ class BinaryTree(object):
     def height(self):
         return self.depth()
 
+    def repr_traverse(self, padding: str, pointer: str, node: Node, has_right_sibling: bool) -> str:
+        if node is not None:
+            self.viz += f'\n{padding}{pointer}{node.value}'
+            if has_right_sibling:
+                padding += "│  "
+            else:
+                padding += '   '
+
+            pointer_right = "└──"
+            if node.right is not None:
+                pointer_left = "├──"
+            else:
+                pointer_left = "└──"
+
+            self.repr_traverse(padding, pointer_left, node.left, node.right is not None)
+            self.repr_traverse(padding, pointer_right, node.right, False)
+
+    def __repr__(self):
+        """
+        Draw the tree in ASCII.
+
+        >>> t = BinaryTree()
+        >>> t.insert(50)
+        >>> t.insert(25)
+        >>> t.insert(75)
+        >>> t.insert(12)
+        >>> t.insert(33)
+        >>> t.insert(88)
+        >>> t.insert(55)
+        >>> t
+        50
+        ├──25
+        │  ├──12
+        │  └──33
+        └──75
+           ├──55
+           └──88
+        """
+        if self.root is None:
+            return ""
+
+        self.viz = f'{self.root.value}'
+        pointer_right = "└──"
+        if self.root.right is None:
+            pointer_left = "└──"
+        else:
+            pointer_left = "├──"
+
+        self.repr_traverse('', pointer_left, self.root.left, self.root.left is not None)
+        self.repr_traverse('', pointer_right, self.root.right, False)
+        return self.viz
+
 
 if __name__ == '__main__':
     import doctest
index d5a4d60ef06483ee07bd2761e10a5ee9bba7385e..9a6f32db43fb09762cca71f79cb268a8862e2452 100644 (file)
@@ -70,12 +70,12 @@ def decompress(kompressed: bytes) -> str:
     # complete the partial 4th byte.  In the 4th byte, however, one
     # bit is information and seven are padding.
     #
-    # It's likely that this APIs client code will treat a zero byte as
-    # a termination character and not regard it as part of the
-    # message.  This is a bug in the client code.
+    # It's likely that this API's client code may treat a zero byte as
+    # a termination character and not regard it as a legitimate part
+    # of the message.  This is a bug in that client code, to be clear.
     #
     # However, it's a bug we can work around:
-
+    #
     # Here, I'm appending an extra 0x00 byte to the compressed message
     # passed in.  If the client code dropped the last 0x00 byte (and,
     # with it, some of the legitimate message bits) by treating it as
index 57fea2862e04f314e7bc2e93469f058ec3793619..e4b48a0ce600020ec5a71907d1ec5cd6fc46529e 100755 (executable)
@@ -97,7 +97,7 @@ if [ ${DOCTEST} -eq 1 ]; then
             else
                 echo -e "${FAILED}"
                 FAILURES=$((FAILURES+1))
-                FAILED_TESTS="${FAILED_TESTS}, ${BASE}"
+                FAILED_TESTS="${FAILED_TESTS},${BASE} (python3 ${doctest})"
             fi
         fi
     done
@@ -111,7 +111,7 @@ if [ ${UNITTEST} -eq 1 ]; then
         ${test}
         if [ $? -ne 0 ]; then
             FAILURES=$((FAILURES+1))
-            FAILED_TESTS="${FAILED_TESTS}, ${BASE}"
+            FAILED_TESTS="${FAILED_TESTS},${BASE} (python3 ${test})"
         fi
     done
 fi
@@ -124,13 +124,14 @@ if [ ${INTEGRATION} -eq 1 ]; then
         ${test}
         if [ $? -ne 0 ]; then
             FAILURES=$((FAILURES+1))
-            FAILED_TESTS="${FAILED_TESTS}, ${BASE}"
+            FAILED_TESTS="${FAILED_TESTS},${BASE} (python3 ${test})"
         fi
     done
 fi
 
 if [ ${FAILURES} -ne 0 ]; then
-    FAILED_TESTS=$(echo ${FAILED_TESTS} | sed 's/^, //g')
+    FAILED_TESTS=$(echo ${FAILED_TESTS} | sed 's/^,/__/g')
+    FAILED_TESTS=$(echo ${FAILED_TESTS} | sed 's/,/\n__/g')
     if [ ${FAILURES} -eq 1 ]; then
         echo -e "${RED}There was ${FAILURES} failure:"
     else