Skip to content
26 changes: 13 additions & 13 deletions data_structures/binary_tree/treap.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
"""
Expand Down
4 changes: 2 additions & 2 deletions matrix/inverse_of_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
...
Expand Down Expand Up @@ -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)
Expand Down