Zeroing A Matrix

February 17, 2017

Today’s exercise is a simple interview question from ADP:

Given a two-dimensional matrix of integers, for any cell that initially contains a zero, change all elements of that cell’s row and column to zero.

Your task is to write the indicated program. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Advertisement

Pages: 1 2

2 Responses to “Zeroing A Matrix”

  1. Paul said

    In Python using numpy.

    import numpy as np
    def zero(matrix):
        rows, cols = [list(set(x)) for x in np.where(matrix == 0)]
        matrix[rows, :] = 0
        matrix[:, cols] = 0
    
  2. r. clayton said

    A solution in Racket.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: