Created
December 8, 2017 17:48
-
-
Save chelseadole/31a676edbe37ba40e65e4a0af1fbcf83 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column are set to 0. | |
M = width | |
N = height | |
""" | |
def zero_matrix(matrix): | |
zero_cols = set() | |
zero_rows = set() | |
for lst in matrix: | |
for num in range(len(lst)): | |
if lst[num] == 0: | |
zero_cols.add(num) | |
zero_rows.add(matrix.index(lst)) | |
for lst in range(len(matrix)): | |
if lst in zero_rows: | |
matrix[lst] = [0 for i in range(len(matrix[0]))] | |
for lst in matrix: | |
for num in range(len(lst)): | |
if num in zero_cols: | |
for lst in matrix: | |
lst[num] = 0 | |
return matrix |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment