diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2024-09-20 09:30:28 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2024-09-20 09:30:28 +0100 |
| commit | 6efda219c95b36111a3be91d0b7908f0f7e7bd55 (patch) | |
| tree | 19ebee5dd1a3e6284d035a3d67372d36c31c363d /challenge-068 | |
| parent | 16d9bf5e3c07756229d5dca902065ed2cc926fe0 (diff) | |
| download | perlweeklychallenge-club-6efda219c95b36111a3be91d0b7908f0f7e7bd55.tar.gz perlweeklychallenge-club-6efda219c95b36111a3be91d0b7908f0f7e7bd55.tar.bz2 perlweeklychallenge-club-6efda219c95b36111a3be91d0b7908f0f7e7bd55.zip | |
Add Python solution to challenge 068
Diffstat (limited to 'challenge-068')
| -rw-r--r-- | challenge-068/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-068/paulo-custodio/perl/ch-1.pl | 2 | ||||
| -rw-r--r-- | challenge-068/paulo-custodio/perl/ch-2.pl | 6 | ||||
| -rw-r--r-- | challenge-068/paulo-custodio/python/ch-1.py | 56 | ||||
| -rw-r--r-- | challenge-068/paulo-custodio/python/ch-2.py | 39 |
5 files changed, 101 insertions, 4 deletions
diff --git a/challenge-068/paulo-custodio/Makefile b/challenge-068/paulo-custodio/Makefile index ba9a2bf399..6bed5804bf 100644 --- a/challenge-068/paulo-custodio/Makefile +++ b/challenge-068/paulo-custodio/Makefile @@ -1,3 +1,5 @@ all: perl perl/ch-1.pl + python3 python/ch-1.py perl perl/ch-2.pl + python3 python/ch-2.py diff --git a/challenge-068/paulo-custodio/perl/ch-1.pl b/challenge-068/paulo-custodio/perl/ch-1.pl index e7279cec76..929071c1c6 100644 --- a/challenge-068/paulo-custodio/perl/ch-1.pl +++ b/challenge-068/paulo-custodio/perl/ch-1.pl @@ -2,7 +2,7 @@ # Challenge 068 # -# TASK #1 › Zero Matrix +# TASK #1 > Zero Matrix # Submitted by: Mohammad S Anwar # You are given a matrix of size M x N having only 0s and 1s. # diff --git a/challenge-068/paulo-custodio/perl/ch-2.pl b/challenge-068/paulo-custodio/perl/ch-2.pl index b4730ddce8..e61fa4c0bd 100644 --- a/challenge-068/paulo-custodio/perl/ch-2.pl +++ b/challenge-068/paulo-custodio/perl/ch-2.pl @@ -2,15 +2,15 @@ # Challenge 068 # -# TASK #2 › Reorder List +# TASK #2 > Reorder List # Submitted by: Mohammad S Anwar # You are given a singly linked list $L as below: # -# L0 ? L1 ? … ? Ln-1 ? Ln +# L0 ? L1 ? ... ? Ln-1 ? Ln # Write a script to reorder list as below: # # L0 ? Ln ? L1 ? Ln-1 ? L2 ? Ln-2 ? -# You are ONLY allowed to do this in-place without altering the nodes’ values. +# You are ONLY allowed to do this in-place without altering the nodes' values. # # Example # Input: 1 ? 2 ? 3 ? 4 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() diff --git a/challenge-068/paulo-custodio/python/ch-2.py b/challenge-068/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..55e0db5737 --- /dev/null +++ b/challenge-068/paulo-custodio/python/ch-2.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python3 + +# Challenge 068 +# +# TASK #2 > Reorder List +# Submitted by: Mohammad S Anwar +# You are given a singly linked list $L as below: +# +# L0 ? L1 ? ... ? Ln-1 ? Ln +# Write a script to reorder list as below: +# +# L0 ? Ln ? L1 ? Ln-1 ? L2 ? Ln-2 ? +# You are ONLY allowed to do this in-place without altering the nodes' values. +# +# Example +# Input: 1 ? 2 ? 3 ? 4 +# Output: 1 ? 4 ? 2 ? 3 + +import unittest + +def reorder_list(l): + # get second element + tail = l[1] + # get and remove last element + p = tail + last = None + while len(p) > 1: + last = p + p = p[1] + eln = last.pop() + + return [l[0], [eln[0], tail]] + +class TestReorderList(unittest.TestCase): + def test_reorder_list(self): + self.assertEqual(reorder_list([1, [2, [3, [4]]]]), [1, [4, [2, [3]]]]) + +if __name__ == '__main__': + unittest.main() |
