Skip to content

Instantly share code, notes, and snippets.

@Jwsonic
Created May 2, 2019 23:57
Show Gist options
  • Select an option

  • Save Jwsonic/8fd0ae6b57f96bf55d9e09b441f938b0 to your computer and use it in GitHub Desktop.

Select an option

Save Jwsonic/8fd0ae6b57f96bf55d9e09b441f938b0 to your computer and use it in GitHub Desktop.

Revisions

  1. Jwsonic created this gist May 2, 2019.
    54 changes: 54 additions & 0 deletions m+n.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    #!/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!')