diff --git a/data_structures/binary_tree/treap.py b/data_structures/binary_tree/treap.py index 3114c6fa1c26..3c1927701947 100644 --- a/data_structures/binary_tree/treap.py +++ b/data_structures/binary_tree/treap.py @@ -41,7 +41,7 @@ def split(root: Node | None, value: int) -> tuple[Node | None, Node | None]: """ if root is None or root.value is None: # None tree is split into 2 Nones return None, None - elif value < root.value: + elif value <= root.value: """ Right tree's root will be current node. Now we split(with the same value) current node's left son @@ -106,16 +106,16 @@ def erase(root: Node | None, value: int) -> Node | None: return merge(left, right) -def inorder(root: Node | None) -> None: +def inorder(root: Node | None) -> str: """ - Just recursive print of a tree + Returns a comma-separated string of tree values in sorted order. + + >>> inorder(None) + '' """ - if not root: # None - return - else: - inorder(root.left) - print(root.value, end=",") - inorder(root.right) + if root is None: + return "" + return inorder(root.left) + str(root.value) + "," + inorder(root.right) def interact_treap(root: Node | None, args: str) -> Node | None: @@ -126,19 +126,19 @@ def interact_treap(root: Node | None, args: str) -> Node | None: >>> root = interact_treap(None, "+1") >>> inorder(root) - 1, + '1,' >>> root = interact_treap(root, "+3 +5 +17 +19 +2 +16 +4 +0") >>> inorder(root) - 0,1,2,3,4,5,16,17,19, + '0,1,2,3,4,5,16,17,19,' >>> root = interact_treap(root, "+4 +4 +4") >>> inorder(root) - 0,1,2,3,4,4,4,4,5,16,17,19, + '0,1,2,3,4,4,4,4,5,16,17,19,' >>> root = interact_treap(root, "-0") >>> inorder(root) - 1,2,3,4,4,4,4,5,16,17,19, + '1,2,3,4,4,4,4,5,16,17,19,' >>> root = interact_treap(root, "-4") >>> inorder(root) - 1,2,3,5,16,17,19, + '1,2,3,5,16,17,19,' >>> root = interact_treap(root, "=0") Unknown command """ diff --git a/matrix/inverse_of_matrix.py b/matrix/inverse_of_matrix.py index e53d90df8253..60ccbcefc14b 100644 --- a/matrix/inverse_of_matrix.py +++ b/matrix/inverse_of_matrix.py @@ -31,7 +31,7 @@ def inverse_of_matrix(matrix: list[list[float]]) -> list[list[float]]: Doctests for 3x3 >>> inverse_of_matrix([[2, 5, 7], [2, 0, 1], [1, 2, 3]]) - [[2.0, 5.0, -4.0], [1.0, 1.0, -1.0], [-5.0, -12.0, 10.0]] + [[2.0, 1.0, -5.0], [5.0, 1.0, -12.0], [-4.0, -1.0, 10.0]] >>> inverse_of_matrix([[1, 2, 2], [1, 2, 2], [3, 2, -1]]) Traceback (most recent call last): ... @@ -145,7 +145,7 @@ def inverse_of_matrix(matrix: list[list[float]]) -> list[list[float]]: adjoint_matrix[i][j] = cofactor_matrix[j][i] # Inverse of the matrix using the formula (1/determinant) * adjoint matrix - inverse_matrix = array(cofactor_matrix) + inverse_matrix = array(adjoint_matrix) for i in range(3): for j in range(3): inverse_matrix[i][j] /= d(determinant)