Skip to content

Commit e9882e4

Browse files
[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...
1 parent d6a1623 commit e9882e4

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

Diff for: DIRECTORY.md

+2
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,8 @@
854854
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_135/sol1.py)
855855
* Problem 144
856856
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_144/sol1.py)
857+
* Problem 145
858+
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_145/sol1.py)
857859
* Problem 173
858860
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_173/sol1.py)
859861
* Problem 174

Diff for: matrix/matrix_operation.py

+24-9
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,16 @@ def add(*matrix_s: list[list]) -> list[list]:
1313
[[3.2, 5.4], [7, 9]]
1414
>>> add([[1, 2], [4, 5]], [[3, 7], [3, 4]], [[3, 5], [5, 7]])
1515
[[7, 14], [12, 16]]
16+
>>> add([3], [4, 5])
17+
Traceback (most recent call last):
18+
...
19+
TypeError: Expected a matrix, got int/list instead
1620
"""
1721
if all(_check_not_integer(m) for m in matrix_s):
1822
for i in matrix_s[1:]:
1923
_verify_matrix_sizes(matrix_s[0], i)
2024
return [[sum(t) for t in zip(*m)] for m in zip(*matrix_s)]
25+
raise TypeError("Expected a matrix, got int/list instead")
2126

2227

2328
def subtract(matrix_a: list[list], matrix_b: list[list]) -> list[list]:
@@ -26,16 +31,21 @@ def subtract(matrix_a: list[list], matrix_b: list[list]) -> list[list]:
2631
[[-1, -1], [-1, -1]]
2732
>>> subtract([[1,2.5],[3,4]],[[2,3],[4,5.5]])
2833
[[-1, -0.5], [-1, -1.5]]
34+
>>> subtract([3], [4, 5])
35+
Traceback (most recent call last):
36+
...
37+
TypeError: Expected a matrix, got int/list instead
2938
"""
3039
if (
3140
_check_not_integer(matrix_a)
3241
and _check_not_integer(matrix_b)
3342
and _verify_matrix_sizes(matrix_a, matrix_b)
3443
):
3544
return [[i - j for i, j in zip(*m)] for m in zip(matrix_a, matrix_b)]
45+
raise TypeError("Expected a matrix, got int/list instead")
3646

3747

38-
def scalar_multiply(matrix: list[list], n: int) -> list[list]:
48+
def scalar_multiply(matrix: list[list], n: int | float) -> list[list]:
3949
"""
4050
>>> scalar_multiply([[1,2],[3,4]],5)
4151
[[5, 10], [15, 20]]
@@ -79,18 +89,23 @@ def identity(n: int) -> list[list]:
7989
return [[int(row == column) for column in range(n)] for row in range(n)]
8090

8191

82-
def transpose(matrix: list[list], return_map: bool = True) -> list[list]:
92+
def transpose(matrix: list[list], return_map: bool = True) -> list[list] | map[list]:
8393
"""
8494
>>> transpose([[1,2],[3,4]]) # doctest: +ELLIPSIS
8595
<map object at ...
8696
>>> transpose([[1,2],[3,4]], return_map=False)
8797
[[1, 3], [2, 4]]
98+
>>> transpose([1, [2, 3]])
99+
Traceback (most recent call last):
100+
...
101+
TypeError: Expected a matrix, got int/list instead
88102
"""
89103
if _check_not_integer(matrix):
90104
if return_map:
91105
return map(list, zip(*matrix))
92106
else:
93107
return list(map(list, zip(*matrix)))
108+
raise TypeError("Expected a matrix, got int/list instead")
94109

95110

96111
def minor(matrix: list[list], row: int, column: int) -> list[list]:
@@ -118,7 +133,7 @@ def determinant(matrix: list[list]) -> int:
118133
)
119134

120135

121-
def inverse(matrix: list[list]) -> list[list]:
136+
def inverse(matrix: list[list]) -> list[list] | None:
122137
"""
123138
>>> inverse([[1, 2], [3, 4]])
124139
[[-2.0, 1.0], [1.5, -0.5]]
@@ -138,21 +153,21 @@ def inverse(matrix: list[list]) -> list[list]:
138153
[x * (-1) ** (row + col) for col, x in enumerate(matrix_minor[row])]
139154
for row in range(len(matrix))
140155
]
141-
adjugate = transpose(cofactors)
156+
adjugate = list(transpose(cofactors))
142157
return scalar_multiply(adjugate, 1 / det)
143158

144159

145160
def _check_not_integer(matrix: list[list]) -> bool:
146-
if not isinstance(matrix, int) and not isinstance(matrix[0], int):
147-
return True
148-
raise TypeError("Expected a matrix, got int/list instead")
161+
return not isinstance(matrix, int) and not isinstance(matrix[0], int)
149162

150163

151-
def _shape(matrix: list[list]) -> list:
164+
def _shape(matrix: list[list]) -> tuple[int, int]:
152165
return len(matrix), len(matrix[0])
153166

154167

155-
def _verify_matrix_sizes(matrix_a: list[list], matrix_b: list[list]) -> tuple[list]:
168+
def _verify_matrix_sizes(
169+
matrix_a: list[list], matrix_b: list[list]
170+
) -> tuple[tuple, tuple]:
156171
shape = _shape(matrix_a) + _shape(matrix_b)
157172
if shape[0] != shape[3] or shape[1] != shape[2]:
158173
raise ValueError(

Diff for: mypy.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
ignore_missing_imports = True
33
install_types = True
44
non_interactive = True
5-
exclude = (matrix_operation.py|other/least_recently_used.py|other/lfu_cache.py|other/lru_cache.py)
5+
exclude = (other/least_recently_used.py|other/lfu_cache.py|other/lru_cache.py)

0 commit comments

Comments
 (0)