Skip to content

Added matrix multiplication #471

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
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
28 changes: 28 additions & 0 deletions algorithms/matrix/multiply.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""
You are given 2 matrices A and B
first it checks if the 2 martrices can be multiplied
if it can be multiplied it multiplies them in O(n^3) time
"""

def multiply(A: list,B: list)->list:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please put in a space between ,B . And a space between operators like -> or =

rowsA,rowsB=len(A),len(B)
colsA,colsB=len(A[0]),len(B[0])
if(colsA != rowsB):
raise Exception("Matrices cannot be multipled")

result=[[0 for i in range(colsB)] for j in range(rowsA)]#create a result matrix
Copy link
Collaborator

Choose a reason for hiding this comment

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

Instead of [0 for i range(colsB)] you can simply write ([0] * colsB)

for i in range(rowsA):
for j in range(colsB):
for k in range(len(B)):
result[i][j] += A[i][k] * B[k][j]
return result

if __name__ == "__main__":
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you write a unit test? See directory tests. It exists for every directory in algorithms a py-file with tests.

Copy link
Author

Choose a reason for hiding this comment

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

Yeah I guess that was the major issue, will be working on it, thanks

A = [[12, 7, 3],
[4, 5, 6],
[7, 8, 9]]
B = [[5, 8, 1, 2],
[6, 7, 3, 0],
[4, 5, 9, 1]]
import pprint
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please write imports at the begin of the file.

Copy link
Author

Choose a reason for hiding this comment

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

I just wanted to import pprint if the user is running .py file otherwise there is no need to import at all.

pprint.pprint(multiply(A,B))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please write the expected output of the test.