Created
May 2, 2019 23:57
Star
You must be signed in to star a gist
Python3 m+n solution
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
#!/usr/bin/env python3 | |
import unittest | |
def zero(matrix): | |
if len(matrix) == 0: | |
return matrix | |
rows = set() | |
cols = set() | |
m = len(matrix) | |
n = len(matrix[0]) | |
for c in range(m): | |
for r in range(n): | |
if matrix[c][r] == 0: | |
cols.add(c) | |
rows.add(r) | |
for c in cols: | |
for r in range(n): | |
matrix[c][r] = 0 | |
for r in rows: | |
for c in range(m): | |
matrix[c][r] = 0 | |
return matrix | |
if __name__ == '__main__': | |
tc = unittest.TestCase('__init__') | |
tc.assertEqual(zero([ | |
[1, 1, 1], | |
[1, 0, 1], | |
[1, 1, 1] | |
]), [ | |
[1, 0, 1], | |
[0, 0, 0], | |
[1, 0, 1] | |
]) | |
tc.assertEqual(zero([ | |
[0, 1, 2, 0], | |
[3, 4, 5, 2], | |
[1, 3, 1, 5] | |
]), [ | |
[0, 0, 0, 0], | |
[0, 4, 5, 0], | |
[0, 3, 1, 0] | |
]) | |
print('Tests pass!') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment