From c03f16dc4c823f7a18340a45105dd40424cbe1bf Mon Sep 17 00:00:00 2001 From: MaximSmolskiy Date: Wed, 1 May 2024 18:58:38 +0000 Subject: [PATCH 1/4] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index f6d6cb463faa..4a053a3f1b7f 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -773,6 +773,7 @@ * [Inverse Of Matrix](matrix/inverse_of_matrix.py) * [Largest Square Area In Matrix](matrix/largest_square_area_in_matrix.py) * [Matrix Class](matrix/matrix_class.py) + * [Matrix Equalization](matrix/matrix_equalization.py) * [Matrix Multiplication Recursion](matrix/matrix_multiplication_recursion.py) * [Matrix Operation](matrix/matrix_operation.py) * [Max Area Of Island](matrix/max_area_of_island.py) From 7b95e220d490e8cb5d4a304f4253eb8d717b4df3 Mon Sep 17 00:00:00 2001 From: MaximSmolskiy Date: Mon, 6 May 2024 22:51:21 +0300 Subject: [PATCH 2/4] Fix some SIM114 per file ignores --- data_structures/binary_tree/treap.py | 4 +--- data_structures/heap/max_heap.py | 4 +--- graphs/minimum_spanning_tree_prims.py | 4 +--- machine_learning/decision_tree.py | 4 +--- matrix/sherman_morrison.py | 4 +--- pyproject.toml | 6 +----- 6 files changed, 6 insertions(+), 20 deletions(-) diff --git a/data_structures/binary_tree/treap.py b/data_structures/binary_tree/treap.py index e7ddf931b83a..3114c6fa1c26 100644 --- a/data_structures/binary_tree/treap.py +++ b/data_structures/binary_tree/treap.py @@ -39,9 +39,7 @@ def split(root: Node | None, value: int) -> tuple[Node | None, Node | None]: Left tree contains all values less than split value. Right tree contains all values greater or equal, than split value """ - if root is None: # None tree is split into 2 Nones - return None, None - elif root.value is None: + if root is None or root.value is None: # None tree is split into 2 Nones return None, None elif value < root.value: """ diff --git a/data_structures/heap/max_heap.py b/data_structures/heap/max_heap.py index 5a9f9cf88433..19d7008e68f6 100644 --- a/data_structures/heap/max_heap.py +++ b/data_structures/heap/max_heap.py @@ -38,9 +38,7 @@ def insert(self, value: int) -> None: def __swap_down(self, i: int) -> None: """Swap the element down""" while self.__size >= 2 * i: - if 2 * i + 1 > self.__size: - bigger_child = 2 * i - elif self.__heap[2 * i] > self.__heap[2 * i + 1]: + if 2 * i + 1 > self.__size or self.__heap[2 * i] > self.__heap[2 * i + 1]: bigger_child = 2 * i else: bigger_child = 2 * i + 1 diff --git a/graphs/minimum_spanning_tree_prims.py b/graphs/minimum_spanning_tree_prims.py index 90c9f4c91e86..f6f029d3706f 100644 --- a/graphs/minimum_spanning_tree_prims.py +++ b/graphs/minimum_spanning_tree_prims.py @@ -16,9 +16,7 @@ def top_to_bottom(self, heap, start, size, positions): if start > size // 2 - 1: return else: - if 2 * start + 2 >= size: - smallest_child = 2 * start + 1 - elif heap[2 * start + 1] < heap[2 * start + 2]: + if 2 * start + 2 >= size or heap[2 * start + 1] < heap[2 * start + 2]: smallest_child = 2 * start + 1 else: smallest_child = 2 * start + 2 diff --git a/machine_learning/decision_tree.py b/machine_learning/decision_tree.py index e48905eeac6a..7b08b56bb725 100644 --- a/machine_learning/decision_tree.py +++ b/machine_learning/decision_tree.py @@ -105,9 +105,7 @@ def train(self, x, y): the predictor """ for i in range(len(x)): - if len(x[:i]) < self.min_leaf_size: - continue - elif len(x[i:]) < self.min_leaf_size: + if len(x[:i]) < self.min_leaf_size or len(x[i:]) < self.min_leaf_size: continue else: error_left = self.mean_squared_error(x[:i], np.mean(y[:i])) diff --git a/matrix/sherman_morrison.py b/matrix/sherman_morrison.py index 7f10ae706e85..e94e752f7d04 100644 --- a/matrix/sherman_morrison.py +++ b/matrix/sherman_morrison.py @@ -65,9 +65,7 @@ def validate_indices(self, loc: tuple[int, int]) -> bool: >>> a.validate_indices((0, 0)) True """ - if not (isinstance(loc, (list, tuple)) and len(loc) == 2): - return False - elif not (0 <= loc[0] < self.row and 0 <= loc[1] < self.column): + if not (isinstance(loc, (list, tuple)) and len(loc) == 2) or not (0 <= loc[0] < self.row and 0 <= loc[1] < self.column): return False else: return True diff --git a/pyproject.toml b/pyproject.toml index 4c512ca896b4..c07bc9c48e51 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -77,14 +77,10 @@ max-complexity = 17 # default: 10 [tool.ruff.lint.per-file-ignores] "arithmetic_analysis/newton_raphson.py" = ["PGH001"] "data_structures/binary_tree/binary_search_tree_recursive.py" = ["BLE001"] -"data_structures/binary_tree/treap.py" = ["SIM114"] "data_structures/hashing/tests/test_hash_map.py" = ["BLE001"] -"data_structures/heap/max_heap.py" = ["SIM114"] -"graphs/minimum_spanning_tree_prims.py" = ["SIM114"] "hashes/enigma_machine.py" = ["BLE001"] -"machine_learning/decision_tree.py" = ["SIM114"] "machine_learning/sequential_minimum_optimization.py" = ["SIM115"] -"matrix/sherman_morrison.py" = ["SIM103", "SIM114"] +"matrix/sherman_morrison.py" = ["SIM103"] "other/l*u_cache.py" = ["RUF012"] "physics/newtons_second_law_of_motion.py" = ["BLE001"] "project_euler/problem_099/sol1.py" = ["SIM115"] From 79bdca382d9e5a1fdeef822b199a0d4e8febf976 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 6 May 2024 19:52:49 +0000 Subject: [PATCH 3/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- matrix/sherman_morrison.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/matrix/sherman_morrison.py b/matrix/sherman_morrison.py index e94e752f7d04..b921f2fcf26e 100644 --- a/matrix/sherman_morrison.py +++ b/matrix/sherman_morrison.py @@ -65,7 +65,9 @@ def validate_indices(self, loc: tuple[int, int]) -> bool: >>> a.validate_indices((0, 0)) True """ - if not (isinstance(loc, (list, tuple)) and len(loc) == 2) or not (0 <= loc[0] < self.row and 0 <= loc[1] < self.column): + if not (isinstance(loc, (list, tuple)) and len(loc) == 2) or not ( + 0 <= loc[0] < self.row and 0 <= loc[1] < self.column + ): return False else: return True From 8fbd7cbce2a6b0e25a5b916b6fc777e4b38c577f Mon Sep 17 00:00:00 2001 From: MaximSmolskiy Date: Fri, 10 May 2024 12:58:41 +0300 Subject: [PATCH 4/4] Fix review issue --- data_structures/heap/max_heap.py | 4 +++- graphs/minimum_spanning_tree_prims.py | 4 +++- machine_learning/decision_tree.py | 4 +++- matrix/sherman_morrison.py | 6 +++--- 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/data_structures/heap/max_heap.py b/data_structures/heap/max_heap.py index 19d7008e68f6..589f2595a8da 100644 --- a/data_structures/heap/max_heap.py +++ b/data_structures/heap/max_heap.py @@ -38,7 +38,9 @@ def insert(self, value: int) -> None: def __swap_down(self, i: int) -> None: """Swap the element down""" while self.__size >= 2 * i: - if 2 * i + 1 > self.__size or self.__heap[2 * i] > self.__heap[2 * i + 1]: + if 2 * i + 1 > self.__size: # noqa: SIM114 + bigger_child = 2 * i + elif self.__heap[2 * i] > self.__heap[2 * i + 1]: bigger_child = 2 * i else: bigger_child = 2 * i + 1 diff --git a/graphs/minimum_spanning_tree_prims.py b/graphs/minimum_spanning_tree_prims.py index f6f029d3706f..d0b45d7ef139 100644 --- a/graphs/minimum_spanning_tree_prims.py +++ b/graphs/minimum_spanning_tree_prims.py @@ -16,7 +16,9 @@ def top_to_bottom(self, heap, start, size, positions): if start > size // 2 - 1: return else: - if 2 * start + 2 >= size or heap[2 * start + 1] < heap[2 * start + 2]: + if 2 * start + 2 >= size: # noqa: SIM114 + smallest_child = 2 * start + 1 + elif heap[2 * start + 1] < heap[2 * start + 2]: smallest_child = 2 * start + 1 else: smallest_child = 2 * start + 2 diff --git a/machine_learning/decision_tree.py b/machine_learning/decision_tree.py index 7b08b56bb725..d0bd6ab0b555 100644 --- a/machine_learning/decision_tree.py +++ b/machine_learning/decision_tree.py @@ -105,7 +105,9 @@ def train(self, x, y): the predictor """ for i in range(len(x)): - if len(x[:i]) < self.min_leaf_size or len(x[i:]) < self.min_leaf_size: + if len(x[:i]) < self.min_leaf_size: # noqa: SIM114 + continue + elif len(x[i:]) < self.min_leaf_size: continue else: error_left = self.mean_squared_error(x[:i], np.mean(y[:i])) diff --git a/matrix/sherman_morrison.py b/matrix/sherman_morrison.py index b921f2fcf26e..e2a09c1d0070 100644 --- a/matrix/sherman_morrison.py +++ b/matrix/sherman_morrison.py @@ -65,9 +65,9 @@ def validate_indices(self, loc: tuple[int, int]) -> bool: >>> a.validate_indices((0, 0)) True """ - if not (isinstance(loc, (list, tuple)) and len(loc) == 2) or not ( - 0 <= loc[0] < self.row and 0 <= loc[1] < self.column - ): + if not (isinstance(loc, (list, tuple)) and len(loc) == 2): # noqa: SIM114 + return False + elif not (0 <= loc[0] < self.row and 0 <= loc[1] < self.column): return False else: return True