From e9882e41ba7a1d1fbdc12362a522a1b646497192 Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj Date: Wed, 10 Nov 2021 20:52:52 +0530 Subject: [PATCH] [mypy] Fix `matrix_operation.py` (#5808) * Update matrix_operation.py * Update mypy.ini * Update DIRECTORY.md * formatting * Update matrix_operation.py * doctest for exception * A bit more... --- DIRECTORY.md | 2 ++ matrix/matrix_operation.py | 33 ++++++++++++++++++++++++--------- mypy.ini | 2 +- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 883b81b24..c46d81ab7 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -854,6 +854,8 @@ * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_135/sol1.py) * Problem 144 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_144/sol1.py) + * Problem 145 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_145/sol1.py) * Problem 173 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_173/sol1.py) * Problem 174 diff --git a/matrix/matrix_operation.py b/matrix/matrix_operation.py index dca01f9c3..6d0cd4e65 100644 --- a/matrix/matrix_operation.py +++ b/matrix/matrix_operation.py @@ -13,11 +13,16 @@ def add(*matrix_s: list[list]) -> list[list]: [[3.2, 5.4], [7, 9]] >>> add([[1, 2], [4, 5]], [[3, 7], [3, 4]], [[3, 5], [5, 7]]) [[7, 14], [12, 16]] + >>> add([3], [4, 5]) + Traceback (most recent call last): + ... + TypeError: Expected a matrix, got int/list instead """ if all(_check_not_integer(m) for m in matrix_s): for i in matrix_s[1:]: _verify_matrix_sizes(matrix_s[0], i) return [[sum(t) for t in zip(*m)] for m in zip(*matrix_s)] + raise TypeError("Expected a matrix, got int/list instead") def subtract(matrix_a: list[list], matrix_b: list[list]) -> list[list]: @@ -26,6 +31,10 @@ def subtract(matrix_a: list[list], matrix_b: list[list]) -> list[list]: [[-1, -1], [-1, -1]] >>> subtract([[1,2.5],[3,4]],[[2,3],[4,5.5]]) [[-1, -0.5], [-1, -1.5]] + >>> subtract([3], [4, 5]) + Traceback (most recent call last): + ... + TypeError: Expected a matrix, got int/list instead """ if ( _check_not_integer(matrix_a) @@ -33,9 +42,10 @@ def subtract(matrix_a: list[list], matrix_b: list[list]) -> list[list]: and _verify_matrix_sizes(matrix_a, matrix_b) ): return [[i - j for i, j in zip(*m)] for m in zip(matrix_a, matrix_b)] + raise TypeError("Expected a matrix, got int/list instead") -def scalar_multiply(matrix: list[list], n: int) -> list[list]: +def scalar_multiply(matrix: list[list], n: int | float) -> list[list]: """ >>> scalar_multiply([[1,2],[3,4]],5) [[5, 10], [15, 20]] @@ -79,18 +89,23 @@ def identity(n: int) -> list[list]: return [[int(row == column) for column in range(n)] for row in range(n)] -def transpose(matrix: list[list], return_map: bool = True) -> list[list]: +def transpose(matrix: list[list], return_map: bool = True) -> list[list] | map[list]: """ >>> transpose([[1,2],[3,4]]) # doctest: +ELLIPSIS >> transpose([[1,2],[3,4]], return_map=False) [[1, 3], [2, 4]] + >>> transpose([1, [2, 3]]) + Traceback (most recent call last): + ... + TypeError: Expected a matrix, got int/list instead """ if _check_not_integer(matrix): if return_map: return map(list, zip(*matrix)) else: return list(map(list, zip(*matrix))) + raise TypeError("Expected a matrix, got int/list instead") def minor(matrix: list[list], row: int, column: int) -> list[list]: @@ -118,7 +133,7 @@ def determinant(matrix: list[list]) -> int: ) -def inverse(matrix: list[list]) -> list[list]: +def inverse(matrix: list[list]) -> list[list] | None: """ >>> inverse([[1, 2], [3, 4]]) [[-2.0, 1.0], [1.5, -0.5]] @@ -138,21 +153,21 @@ def inverse(matrix: list[list]) -> list[list]: [x * (-1) ** (row + col) for col, x in enumerate(matrix_minor[row])] for row in range(len(matrix)) ] - adjugate = transpose(cofactors) + adjugate = list(transpose(cofactors)) return scalar_multiply(adjugate, 1 / det) def _check_not_integer(matrix: list[list]) -> bool: - if not isinstance(matrix, int) and not isinstance(matrix[0], int): - return True - raise TypeError("Expected a matrix, got int/list instead") + return not isinstance(matrix, int) and not isinstance(matrix[0], int) -def _shape(matrix: list[list]) -> list: +def _shape(matrix: list[list]) -> tuple[int, int]: return len(matrix), len(matrix[0]) -def _verify_matrix_sizes(matrix_a: list[list], matrix_b: list[list]) -> tuple[list]: +def _verify_matrix_sizes( + matrix_a: list[list], matrix_b: list[list] +) -> tuple[tuple, tuple]: shape = _shape(matrix_a) + _shape(matrix_b) if shape[0] != shape[3] or shape[1] != shape[2]: raise ValueError( diff --git a/mypy.ini b/mypy.ini index 94fb125fb..f00b3eeb6 100644 --- a/mypy.ini +++ b/mypy.ini @@ -2,4 +2,4 @@ ignore_missing_imports = True install_types = True non_interactive = True -exclude = (matrix_operation.py|other/least_recently_used.py|other/lfu_cache.py|other/lru_cache.py) +exclude = (other/least_recently_used.py|other/lfu_cache.py|other/lru_cache.py)