easy +8 pts

Matrix Multiply 2D

Implement matrix multiplication for 2D lists with validation.

Write a function `matrix_multiply(A, B)` that takes two matrices `A` and `B` represented as lists of lists of numbers (int/float). The function must return the matrix product `A × B` as a new list of lists. The matrices are non-empty and the inner dimensions match (i.e., `len(A[0]) == len(B)`). Each row of a matrix has the same length. The product matrix will have dimensions `len(A)` rows × `len(B[0])` columns. Compute the standard matrix multiplication: each cell `[i][j]` is the dot product of row `i` of `A` and column `j` of `B`. Your function should not modify the input matrices.

Constraints

Matrices are non-empty. The inner dimensions match. Values are integers or floats. Number of rows/columns ≤ 100. Complexity O(p·q·r) where p=rows(A), q=cols(A)=rows(B), r=cols(B).

Example

>>> matrix_multiply([[1,2],[3,4]], [[5,6],[7,8]])
[[19, 22], [43, 50]]
>>> matrix_multiply([[1,2,3]], [[4],[5],[6]])
[[32]]
>>> matrix_multiply([[2,0],[0,2]], [[1,2],[3,4]])
[[2, 4], [6, 8]]
8 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

The result will have len(A) rows and len(B[0]) columns.
Use three nested loops: iterate rows of A, columns of B, and the shared dimension.
Initialize each result row as a list of zeros before filling it.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.