-
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?
Conversation
|
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.
Good Work! I have some suggestions
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 comment
The 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 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.
[6, 7, 3, 0], | ||
[4, 5, 9, 1]] | ||
import pprint | ||
pprint.pprint(multiply(A,B)) |
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 write the expected output of the test.
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 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.
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.
Yeah I guess that was the major issue, will be working on it, thanks
if it can be multiplied it multiplies them in O(n^3) time | ||
""" | ||
|
||
def multiply(A: list,B: list)->list: |
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 =
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 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)
(Put an
X
inside the[ ]
to denote check mark[X]
.)If creating a new file :
if done some changes :
other