-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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: | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of |
||
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__": | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please write imports at the begin of the file. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please write the expected output of the test. |
There was a problem hiding this comment.
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=