From: Scott Gasch Date: Wed, 29 Sep 2021 22:40:43 +0000 (-0700) Subject: Stuff. X-Git-Url: https://wannabe.guru.org/gitweb/?a=commitdiff_plain;h=791755ededfc0de84a81b4c7313830efbd27bf8c;p=python_utils.git Stuff. --- diff --git a/collect/bst.py b/collect/bst.py index 8e95fa2..d3231ee 100644 --- a/collect/bst.py +++ b/collect/bst.py @@ -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 diff --git a/letter_compress.py b/letter_compress.py index d5a4d60..9a6f32d 100644 --- a/letter_compress.py +++ b/letter_compress.py @@ -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 diff --git a/tests/run_tests.sh b/tests/run_tests.sh index 57fea28..e4b48a0 100755 --- a/tests/run_tests.sh +++ b/tests/run_tests.sh @@ -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