Sparse Matrix Multiplication | MLExpert https://www.algoexpert.io/machine-learning/coding-questions/Sparse%...
MLExpert Quad Layout 14px
00:00:00 | 00:00:00
Sublime
Prompt Scratchpad Our Solution(s) Video
Your
Explanation
Solutions Run Code
Solution 1 Solution 2 Solution 3
Category: Math Concepts
1 {
Sparse Matrix 2
3
"matrix_a": [
[0, 2, 0],
Multiplication 4 [0, -3, 5]
5 ],
Write a function that takes in two integer 6 "matrix_b": [
7 [0, 10, 0],
matrices and multiplies them together.
8 [0, 0, 0],
Both matrices will be sparse, meaning that 9 [0, 0, 4]
10 ]
most of their elements will be zero. Take
11 }
advantage of that to reduce the number of 12
total computations that your function 13 # Copyright © 2022 AlgoExpert LLC. All right
performs. 14
15 def sparse_matrix_multiplication(matrix_a, m
If the matrices can't be multiplied together, 16 if len(matrix_a[0]) != len(matrix_b):
your function should return [[]] . 17 return [[]]
18
19 sparse_a = get_dict_of_nonzero_cells(mat
Sample Input 20 sparse_b = get_dict_of_nonzero_cells(mat
21
matrix_a = [ 22 matrix_c = [[0] * len(matrix_b[0
[0, 2, 0], 23
[0, -3, 5], 24 for i, k in sparse_a.keys():
] 25 for j in range(len(matrix_b[
matrix_b = [ 26 if (k, j) in sparse_b.keys
[0, 10, 0],
[0, 0, 0],
[0, 0, 4],
]
Sample Output
[
[0, 0, 0],
[0, 0, 20],
]
Hints
1 de 3 20/04/2022 09:13 p. m.
Sparse Matrix Multiplication | MLExpert https://www.algoexpert.io/machine-learning/coding-questions/Sparse%...
Hint 1
Hint 2
Hint 3
Hint 4
Tests Quick Test Sandbox Custom Output Raw Output Submit Code
Run or submit code when
you're ready.
2 de 3 20/04/2022 09:13 p. m.
Sparse Matrix Multiplication | MLExpert https://www.algoexpert.io/machine-learning/coding-questions/Sparse%...
3 de 3 20/04/2022 09:13 p. m.