Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* black

* Update matrix/matrix_operation.py

Co-authored-by: Christian Clauss <[email protected]>

* Update matrix/matrix_operation.py

Co-authored-by: Christian Clauss <[email protected]>

* Update matrix/matrix_operation.py

Co-authored-by: Christian Clauss <[email protected]>

* Update matrix/matrix_operation.py

Co-authored-by: Christian Clauss <[email protected]>

* flake8 error fix

* Remove unreachable code

* union

Co-authored-by: Christian Clauss <[email protected]>
  • Loading branch information
itsvinayak and cclauss authored Jul 4, 2020
1 parent f70a0a2 commit 70cf565
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 20 deletions.
23 changes: 7 additions & 16 deletions matrix/matrix_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand All @@ -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]:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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]:
Expand All @@ -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",
Expand Down
10 changes: 6 additions & 4 deletions matrix/searching_in_sorted_matrix.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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]:
Expand All @@ -38,5 +39,6 @@ def main():

if __name__ == "__main__":
import doctest

doctest.testmod()
main()

0 comments on commit 70cf565

Please sign in to comment.