Problem 241309 · medium · Level 02 Linear Data Structures

Multiply Two Small Matrices

2d-lists · matrix · triple-loop

Given two grids of integers a (shape n x m) and b (shape m x p), return their matrix product: an n x p grid whose cell at row i, column j is the sum over t of a[i][t] * b[t][j].

Examples

Input:  a = [[1, 2],
             [3, 4]]
        b = [[5, 6],
             [7, 8]]
Output: [[19, 22],
         [43, 50]]

Input:  a = [[1, 2, 3]]
        b = [[1],
             [2],
             [3]]
Output: [[14]]

Constraints

  • 1 <= n, m, p <= 40; the number of columns of a always equals the number of rows of b

Goals

  • Implement the row-times-column rule of matrix multiplication
  • Get the output dimensions right for non-square inputs
Starting Python…