diff options
| -rw-r--r-- | challenge-344/pokgopun/python/ch-1.py | 64 | ||||
| -rw-r--r-- | challenge-344/pokgopun/python/ch-2.py | 85 |
2 files changed, 149 insertions, 0 deletions
diff --git a/challenge-344/pokgopun/python/ch-1.py b/challenge-344/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..c3738dcacb --- /dev/null +++ b/challenge-344/pokgopun/python/ch-1.py @@ -0,0 +1,64 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-344/ +""" + +Task 1: Array Form Compute + +Submitted by: [43]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an array of integers, @ints and an integer, $x. + + Write a script to add $x to the integer in the array-form. + + The array form of an integer is a digit-by-digit representation + stored as an array, where the most significant digit is at the 0th + index. + +Example 1 + +Input: @ints = (1, 2, 3, 4), $x = 12 +Output: (1, 2, 4, 6) + +Example 2 + +Input: @ints = (2, 7, 4), $x = 181 +Output: (4, 5, 5) + +Example 3 + +Input: @ints = (9, 9, 9), $x = 1 +Output: (1, 0, 0, 0) + +Example 4 + +Input: @ints = (1, 0, 0, 0, 0), $x = 9999 +Output: (1, 9, 9, 9, 9) + +Example 5 + +Input: @ints = (0), $x = 1000 +Output: (1, 0, 0, 0) + +Task 2: Array Formation +""" +### solution by pokgopun@gmail.com + +def afc(ints: tuple[int], x: int) -> tuple[int]: + return tuple( + int(e) for e in str(int("".join(str(n) for n in ints)) + x) + ) + +import unittest + +class TestAfc(unittest.TestCase): + def test(self): + for (ints, x), otpt in { + ((1, 2, 3, 4), 12): (1, 2, 4, 6), + ((2, 7, 4), 181): (4, 5, 5), + ((9, 9, 9), 1): (1, 0, 0, 0), + ((1, 0, 0, 0, 0), 9999): (1, 9, 9, 9, 9), + ((0,), 1000): (1, 0, 0, 0), + }.items(): + self.assertEqual(afc(ints,x), otpt) + +unittest.main() diff --git a/challenge-344/pokgopun/python/ch-2.py b/challenge-344/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..78203fa4b5 --- /dev/null +++ b/challenge-344/pokgopun/python/ch-2.py @@ -0,0 +1,85 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-344/ +""" + +Task 2: Array Formation + +Submitted by: [44]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given two list: @source and @target. + + Write a script to see if you can build the exact @target by putting + these smaller lists from @source together in some order. You cannot + break apart or change the order inside any of the smaller lists in + @source. + +Example 1 + +Input: @source = ([2,3], [1], [4]) + @target = (1, 2, 3, 4) +Output: true + +Use in the order: [1], [2,3], [4] + +Example 2 + +Input: @source = ([1,3], [2,4]) + @target = (1, 2, 3, 4) +Output: false + +Example 3 + +Input: @source = ([9,1], [5,8], [2]) + @target = (5, 8, 2, 9, 1) +Output: true + +Use in the order: [5,8], [2], [9,1] + +Example 4 + +Input: @source = ([1], [3]) + @target = (1, 2, 3) +Output: false + +Missing number: 2 + +Example 5 + +Input: @source = ([7,4,6]) + @target = (7, 4, 6) +Output: true + +Use in the order: [7, 4, 6] + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 26th October + 2025. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +from itertools import permutations, chain + +def af(source: tuple[tuple[int]], target: tuple[int]) -> bool: + for pmt in permutations(source, len(source)): + #print(f'pmt => {pmt}') + if target == tuple(chain.from_iterable(pmt)): + return True + return False + +import unittest + +class TestAf(unittest.TestCase): + def test(self): + for (source, target), otpt in { + (((2,3), (1,), (4,)), (1, 2, 3, 4)): True, + (((1,3), (2,4)), (1, 2, 3, 4)): False, + (((9,1), (5,8), (2,)), (5, 8, 2, 9, 1)): True, + (((1,), (3,)), (1, 2, 3)): False, + (((7,4,6),), (7, 4, 6)): True, + }.items(): + self.assertEqual(af(source,target),otpt) + +unittest.main() |
