#!/usr/bin/env python3
-from typing import Any, Optional
+from typing import Any, List, Optional
class Node(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
# 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
else
echo -e "${FAILED}"
FAILURES=$((FAILURES+1))
- FAILED_TESTS="${FAILED_TESTS}, ${BASE}"
+ FAILED_TESTS="${FAILED_TESTS},${BASE} (python3 ${doctest})"
fi
fi
done
${test}
if [ $? -ne 0 ]; then
FAILURES=$((FAILURES+1))
- FAILED_TESTS="${FAILED_TESTS}, ${BASE}"
+ FAILED_TESTS="${FAILED_TESTS},${BASE} (python3 ${test})"
fi
done
fi
${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