Skip to content

Instantly share code, notes, and snippets.

@Jwsonic
Created May 2, 2019 23:57
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Jwsonic/8fd0ae6b57f96bf55d9e09b441f938b0 to your computer and use it in GitHub Desktop.
Python3 m+n solution
#!/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