Editor

Zero Out Rows and Columns

#278 · Grids and matrices · Intermediate · 60 XP · about 10 min

A grid of sensors reports its readings as a list of rows, each row a list of integers. A reading of 0 means that sensor failed, and when a sensor fails, every reading in its row and in its column can no longer be trusted.

Write zero_out(matrix) that returns a new grid of the same size in which every row and every column that contains a 0 in the original grid is filled with zeros. All other readings stay as they are. Do not change matrix itself.

Examples

zero_out([[1, 2, 3],
          [4, 0, 6],
          [7, 8, 9]])        # returns [[1, 0, 3], [0, 0, 0], [7, 0, 9]]
zero_out([[0, 1, 2, 0],
          [3, 4, 5, 2],
          [1, 3, 1, 5]])     # returns [[0, 0, 0, 0], [0, 4, 5, 0], [0, 3, 1, 0]]
zero_out([[5, 6]])           # returns [[5, 6]]

Only zeros in the original grid count: a zero you write while filling does not spread any further.

Constraints

  • 0 to 200 rows, all of the same length (1 to 200); an empty grid [] gives [].
  • Readings are between -1,000 and 1,000.

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.