mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-01-30 22:23:42 +00:00
black (#2166)
* black * Update matrix/matrix_operation.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update matrix/matrix_operation.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update matrix/matrix_operation.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update matrix/matrix_operation.py Co-authored-by: Christian Clauss <cclauss@me.com> * flake8 error fix * Remove unreachable code * union Co-authored-by: Christian Clauss <cclauss@me.com>
This commit is contained in:
parent
f70a0a2980
commit
70cf565387
|
@ -14,9 +14,7 @@ def add(matrix_a: List[list], matrix_b: List[list]) -> List[list]:
|
|||
"""
|
||||
if _check_not_integer(matrix_a) and _check_not_integer(matrix_b):
|
||||
_verify_matrix_sizes(matrix_a, matrix_b)
|
||||
matrix_c = [[i + j for i, j in zip(m, n)]
|
||||
for m, n in zip(matrix_a, matrix_b)]
|
||||
return matrix_c
|
||||
return [[i + j for i, j in zip(m, n)] for m, n in zip(matrix_a, matrix_b)]
|
||||
|
||||
|
||||
def subtract(matrix_a: List[list], matrix_b: List[list]) -> List[list]:
|
||||
|
@ -28,9 +26,7 @@ def subtract(matrix_a: List[list], matrix_b: List[list]) -> List[list]:
|
|||
"""
|
||||
if _check_not_integer(matrix_a) and _check_not_integer(matrix_b):
|
||||
_verify_matrix_sizes(matrix_a, matrix_b)
|
||||
matrix_c = [[i - j for i, j in zip(m, n)]
|
||||
for m, n in zip(matrix_a, matrix_b)]
|
||||
return matrix_c
|
||||
return [[i - j for i, j in zip(m, n)] for m, n in zip(matrix_a, matrix_b)]
|
||||
|
||||
|
||||
def scalar_multiply(matrix: List[list], n: int) -> List[list]:
|
||||
|
@ -101,9 +97,8 @@ def minor(matrix: List[list], row: int, column: int) -> List[list]:
|
|||
>>> minor([[1, 2], [3, 4]], 1, 1)
|
||||
[[1]]
|
||||
"""
|
||||
minor = matrix[:row] + matrix[row + 1:]
|
||||
minor = [row[:column] + row[column + 1:] for row in minor]
|
||||
return minor
|
||||
minor = matrix[:row] + matrix[row + 1 :]
|
||||
return [row[:column] + row[column + 1 :] for row in minor]
|
||||
|
||||
|
||||
def determinant(matrix: List[list]) -> int:
|
||||
|
@ -156,8 +151,7 @@ def _shape(matrix: List[list]) -> list:
|
|||
return list((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[list]:
|
||||
shape = _shape(matrix_a)
|
||||
shape += _shape(matrix_b)
|
||||
if shape[0] != shape[2] or shape[1] != shape[3]:
|
||||
|
@ -171,12 +165,9 @@ def _verify_matrix_sizes(
|
|||
def main():
|
||||
matrix_a = [[12, 10], [3, 9]]
|
||||
matrix_b = [[3, 4], [7, 4]]
|
||||
matrix_c = [[11, 12, 13, 14], [21, 22, 23, 24],
|
||||
[31, 32, 33, 34], [41, 42, 43, 44]]
|
||||
matrix_c = [[11, 12, 13, 14], [21, 22, 23, 24], [31, 32, 33, 34], [41, 42, 43, 44]]
|
||||
matrix_d = [[3, 0, 2], [2, 0, -2], [0, 1, 1]]
|
||||
print(
|
||||
f"Add Operation, {matrix_a} + {matrix_b}"
|
||||
f" = {add(matrix_a, matrix_b)} \n")
|
||||
print(f"Add Operation, {matrix_a} + {matrix_b} = {add(matrix_a, matrix_b)} \n")
|
||||
print(
|
||||
f"Multiply Operation, {matrix_a} * {matrix_b}",
|
||||
f"= {multiply(matrix_a, matrix_b)} \n",
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
from typing import List
|
||||
from typing import List, Union
|
||||
|
||||
|
||||
def search_in_a_sorted_matrix(
|
||||
mat: List[list], m: int, n: int, key: int or float) -> None:
|
||||
'''
|
||||
mat: List[list], m: int, n: int, key: Union[int, float]
|
||||
) -> None:
|
||||
"""
|
||||
>>> search_in_a_sorted_matrix(\
|
||||
[[2, 5, 7], [4, 8, 13], [9, 11, 15], [12, 17, 20]], 3, 3, 5)
|
||||
Key 5 found at row- 1 column- 2
|
||||
|
@ -16,7 +17,7 @@ def search_in_a_sorted_matrix(
|
|||
>>> search_in_a_sorted_matrix(\
|
||||
[[2.1, 5, 7], [4, 8, 13], [9, 11, 15], [12, 17, 20]], 3, 3, 2.2)
|
||||
Key 2.2 not found
|
||||
'''
|
||||
"""
|
||||
i, j = m - 1, 0
|
||||
while i >= 0 and j < n:
|
||||
if key == mat[i][j]:
|
||||
|
@ -38,5 +39,6 @@ def main():
|
|||
|
||||
if __name__ == "__main__":
|
||||
import doctest
|
||||
|
||||
doctest.testmod()
|
||||
main()
|
||||
|
|
Loading…
Reference in New Issue
Block a user