Infection of the Ones

Published by Helen Yu in

Write a function that replaces every row and column that contains at least one 1 into a row/column that is filled entirely with 1s.

Solve this without returning a copy of the input array.

Examples

ones_infection([
  [0, 0, 1],
  [0, 0, 0],
  [0, 0, 0]
]) ➞ [
  [1, 1, 1],
  [0, 0, 1],
  [0, 0, 1]
]

ones_infection([
  [1, 0, 1, 0],
  [0, 1, 0, 0],
  [0, 0, 0, 0]
]) ➞ [
  [1, 1, 1, 1],
  [1, 1, 1, 1],
  [1, 1, 1, 0]
]

ones_infection([
  [0, 1, 0, 1],
  [0, 0, 0, 0],
  [0, 1, 0, 0]
]) ➞ [
  [1, 1, 1, 1],
  [0, 1, 0, 1],
  [1, 1, 1, 1]
]

Notes

  • You must mutate the original array.
  • Input matrices will have at least row and one column.
  • Bonus: Solve this without using any higher-order functions.
Watch a quick demo on how Edabit works.