diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-09-20 21:40:27 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-09-20 21:40:27 +0100 |
| commit | b521af146ef7c2bd338bc5355e2fc8f6c49cbd30 (patch) | |
| tree | 8d8e24fcdbb56212b91343e1306cce425826dc53 /challenge-068/paulo-custodio/python/ch-1.py | |
| parent | 962fb60ab30715e2dedc4fda8619042d22fba65e (diff) | |
| parent | 4ae5477a9bc6c8b01ee984ed6a5a90dbec170833 (diff) | |
| download | perlweeklychallenge-club-b521af146ef7c2bd338bc5355e2fc8f6c49cbd30.tar.gz perlweeklychallenge-club-b521af146ef7c2bd338bc5355e2fc8f6c49cbd30.tar.bz2 perlweeklychallenge-club-b521af146ef7c2bd338bc5355e2fc8f6c49cbd30.zip | |
Merge pull request #10874 from pauloscustodio/master
Add Python solution to challenge 067
Diffstat (limited to 'challenge-068/paulo-custodio/python/ch-1.py')
| -rw-r--r-- | challenge-068/paulo-custodio/python/ch-1.py | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/challenge-068/paulo-custodio/python/ch-1.py b/challenge-068/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..6171b18125 --- /dev/null +++ b/challenge-068/paulo-custodio/python/ch-1.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python3 + +# Challenge 068 +# +# TASK #1 > Zero Matrix +# Submitted by: Mohammad S Anwar +# You are given a matrix of size M x N having only 0s and 1s. +# +# Write a script to set the entire row and column to 0 if an element is 0. +# +# Example 1 +# Input: [1, 0, 1] +# [1, 1, 1] +# [1, 1, 1] +# +# Output: [0, 0, 0] +# [1, 0, 1] +# [1, 0, 1] +# Example 2 +# Input: [1, 0, 1] +# [1, 1, 1] +# [1, 0, 1] +# +# Output: [0, 0, 0] +# [1, 0, 1] +# [0, 0, 0] + +from copy import deepcopy +import unittest + +def zero_matrix(m): + orig = deepcopy(m) + for r in range(len(m)): + for c in range(len(m[0])): + if not orig[r][c]: + m = zero_row(r, m) + m = zero_col(c, m) + return m + +def zero_row(r, m): + for c in range(len(m[0])): + m[r][c] = 0 + return m + +def zero_col(c, m): + for r in range(len(m)): + m[r][c] = 0 + return m + +class TestZeroMatrix(unittest.TestCase): + def test_zero_matrix(self): + self.assertEqual(zero_matrix([[1, 0, 1], [1, 1, 1], [1, 1, 1]]), [[0, 0, 0], [1, 0, 1], [1, 0, 1]]) + self.assertEqual(zero_matrix([[1, 0, 1], [1, 1, 1], [1, 0, 1]]), [[0, 0, 0], [1, 0, 1], [0, 0, 0]]) + +if __name__ == '__main__': + unittest.main() |
