Click4Ai

37.

Medium

Write a function matrix_multiply(A, B) that performs matrix multiplication without using np.dot(), np.matmul(), or the @ operator.

For matrices A (m×n) and B (n×p), the result C (m×p) is: C[i][j] = sum(A[i][k] * B[k][j]) for k in 0..n-1

Example:

Input: A = [[1, 2], [3, 4]], B = [[5, 6], [7, 8]]

Output: [[19, 22], [43, 50]]

**Explanation:** C[0][0] = 1*5 + 2*7 = 19, C[0][1] = 1*6 + 2*8 = 22, etc.

Constraints:

  • Number of columns in A equals number of rows in B
  • Return a list of lists
  • Test Cases

    Test Case 1
    Input: [[1, 2], [3, 4]], [[5, 6], [7, 8]]
    Expected: [[19, 22], [43, 50]]
    Test Case 2
    Input: [[1, 0], [0, 1]], [[5, 6], [7, 8]]
    Expected: [[5, 6], [7, 8]]
    Test Case 3
    Input: [[2, 0], [0, 3]], [[4, 1], [2, 5]]
    Expected: [[8, 2], [6, 15]]
    + 2 hidden test cases