From 6b468e51f0460af95ae852c6fb8a0b3024b25e56 Mon Sep 17 00:00:00 2001 From: maitriupadhyay03-cell Date: Sat, 6 Jun 2026 16:50:31 +0530 Subject: [PATCH 1/5] fix: correct treap split() to match docstring for equal values (#7854) The split() function in treap.py uses `<` comparison but the docstring states that the right subtree should contain values "greater or equal" to the split value. This fix changes `elif value < root.value:` to `elif value <= root.value:` so that equal values go to the right subtree as documented. Fixes #7854 --- data_structures/binary_tree/treap.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/data_structures/binary_tree/treap.py b/data_structures/binary_tree/treap.py index 3114c6fa1c26..5c67dc8d7ead 100644 --- a/data_structures/binary_tree/treap.py +++ b/data_structures/binary_tree/treap.py @@ -5,7 +5,7 @@ class Node: """ - Treap's node + Treap's nodeh Treap is a binary tree by value and heap by priority """ @@ -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 From 48e35c87ff23f2692c734700669d48280d91e73c Mon Sep 17 00:00:00 2001 From: maitriupadhyay03-cell Date: Sat, 6 Jun 2026 16:52:00 +0530 Subject: [PATCH 2/5] Fix typo in Treap node docstringfix: remove accidental typo in Node class docstring --- data_structures/binary_tree/treap.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/binary_tree/treap.py b/data_structures/binary_tree/treap.py index 5c67dc8d7ead..ca81d5967cb6 100644 --- a/data_structures/binary_tree/treap.py +++ b/data_structures/binary_tree/treap.py @@ -5,7 +5,7 @@ class Node: """ - Treap's nodeh + Treap's node Treap is a binary tree by value and heap by priority """ From 68519a8379382060c2298712b9b7d797b9356702 Mon Sep 17 00:00:00 2001 From: maitriupadhyay03-cell Date: Sun, 21 Jun 2026 20:43:39 +0530 Subject: [PATCH 3/5] fix(matrix): use adjoint_matrix for 3x3 inverse computation The 3x3 inverse computation had a bug: it was using cofactor_matrix instead of adjoint_matrix when seeding inverse_matrix (line ~148). The adjoint is correctly computed as the transpose of the cofactor matrix, but then discarded. This fix uses adjoint_matrix as required by the formula: inverse = (1/det) * adjoint. Also updates the 3x3 doctest to assert the correct inverse values. Fixes #14813 --- matrix/inverse_of_matrix.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/matrix/inverse_of_matrix.py b/matrix/inverse_of_matrix.py index e53d90df8253..e270d80da6a3 100644 --- a/matrix/inverse_of_matrix.py +++ b/matrix/inverse_of_matrix.py @@ -5,7 +5,7 @@ from numpy import array -def inverse_of_matrix(matrix: list[list[float]]) -> list[list[float]]: +def inverse_of_matrix(matrix: list[list[float]]) -> lihst[list[float]]: """ A matrix multiplied with its inverse gives the identity matrix. This function finds the inverse of a 2x2 and 3x3 matrix. @@ -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) From d69d82a850ab19d34b81d06ce139a5370e2edfac Mon Sep 17 00:00:00 2001 From: maitriupadhyay03-cell Date: Sun, 21 Jun 2026 21:33:48 +0530 Subject: [PATCH 4/5] fix(treap): make inorder() return string to fix thread-unsafe doctest The inorder() function previously used print() to output values, which made its doctests thread-unsafe (pytest-run-parallel does not support doctests that use print). This fix: - Changes inorder() to return a comma-separated string instead of printing - Updates all interact_treap doctests to assert the returned string value - Adds a doctest to inorder() itself Fixes the CI failure: FAILED data_structures/binary_tree/treap.py::data_structures.binary_tree.treap.interact_treap --- data_structures/binary_tree/treap.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/data_structures/binary_tree/treap.py b/data_structures/binary_tree/treap.py index ca81d5967cb6..3c1927701947 100644 --- a/data_structures/binary_tree/treap.py +++ b/data_structures/binary_tree/treap.py @@ -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 """ From e3e9135d7ba0cdf70487065e38c5609887141766 Mon Sep 17 00:00:00 2001 From: maitriupadhyay03-cell Date: Sun, 21 Jun 2026 21:39:50 +0530 Subject: [PATCH 5/5] fix(matrix): fix typo 'lihst' -> 'list' in return type annotation --- matrix/inverse_of_matrix.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/matrix/inverse_of_matrix.py b/matrix/inverse_of_matrix.py index e270d80da6a3..60ccbcefc14b 100644 --- a/matrix/inverse_of_matrix.py +++ b/matrix/inverse_of_matrix.py @@ -5,7 +5,7 @@ from numpy import array -def inverse_of_matrix(matrix: list[list[float]]) -> lihst[list[float]]: +def inverse_of_matrix(matrix: list[list[float]]) -> list[list[float]]: """ A matrix multiplied with its inverse gives the identity matrix. This function finds the inverse of a 2x2 and 3x3 matrix.