Skip to content

Update matrix_operation.py #2344

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 21, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions matrix/matrix_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def transpose(matrix: List[list], return_map: bool = True) -> List[list]:
if return_map:
return map(list, zip(*matrix))
else:
return [[row[i] for row in matrix] for i in range(len(matrix[0]))]
return list(map(list, zip(*matrix)))


def minor(matrix: List[list], row: int, column: int) -> List[list]:
Expand All @@ -109,8 +109,8 @@ def determinant(matrix: List[list]) -> int:
if len(matrix) == 1:
return matrix[0][0]

return sum(matrix[0][x] * determinant(minor(matrix, 0, x)) * (-1) ** x
for x in range(len(matrix)))
return sum(x * determinant(minor(matrix, 0, i)) * (-1) ** i
for i, x in enumerate(matrix[0]))


def inverse(matrix: List[list]) -> List[list]:
Expand Down Expand Up @@ -162,7 +162,6 @@ def main():
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]]
multiply([[1, 2, 3]], [[2], [3], [4]])
print(
Copy link
Member

@cclauss cclauss Aug 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Python >= 3.8, you can use this trailing = syntax...

% python3.8

Python 3.8.5 (default, Jul 21 2020, 10:48:26)
>>> from operator import add
>>> f"{add(2, 3) = }"
'add(2, 3) = 5'
>>>

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

print(f"Add Operation: {add(matrix_a, matrix_b) = }")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I'm still using py3.7... I would fix it.

f"Add Operation, {matrix_a} + {matrix_b} =" f"{add(matrix_a, matrix_b)} \n")
print(
Expand Down