diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-10-20 16:56:29 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-10-20 16:56:29 +0100 |
| commit | 12bea413dde33182ccc24bc96b0cf434892deedc (patch) | |
| tree | 8c649d2bf374c33c3c50e262c726bbeb749b677a | |
| parent | 1630c2fa99e8115b0bd302f57d802c44523d1fbd (diff) | |
| parent | bedb01372c255ebec06ae07cbee65e371ee16152 (diff) | |
| download | perlweeklychallenge-club-12bea413dde33182ccc24bc96b0cf434892deedc.tar.gz perlweeklychallenge-club-12bea413dde33182ccc24bc96b0cf434892deedc.tar.bz2 perlweeklychallenge-club-12bea413dde33182ccc24bc96b0cf434892deedc.zip | |
Merge pull request #12882 from pokgopun/pwc344
Pwc344
| -rw-r--r-- | challenge-344/pokgopun/go/ch-1.go | 95 | ||||
| -rw-r--r-- | challenge-344/pokgopun/go/ch-2.go | 153 | ||||
| -rw-r--r-- | challenge-344/pokgopun/python/ch-1.py | 64 | ||||
| -rw-r--r-- | challenge-344/pokgopun/python/ch-2.py | 85 |
4 files changed, 397 insertions, 0 deletions
diff --git a/challenge-344/pokgopun/go/ch-1.go b/challenge-344/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..ab6a470f5c --- /dev/null +++ b/challenge-344/pokgopun/go/ch-1.go @@ -0,0 +1,95 @@ +//# 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 + +package main + +import ( + "io" + "os" + "slices" + + "github.com/google/go-cmp/cmp" +) + +type ints []int + +func (in ints) int() int { + l := len(in) + n := 0 + t := 1 + for l > 0 { + l-- + n += in[l] * t + t *= 10 + } + return n +} + +func (in ints) process(n int) ints { + num := in.int() + n + var nums ints + for num > 0 { + nums = append(nums, num%10) + num /= 10 + } + slices.Reverse(nums) + return nums +} + +func main() { + for _, data := range []struct { + ints ints + int int + output ints + }{ + {ints{1, 2, 3, 4}, 12, ints{1, 2, 4, 6}}, + {ints{2, 7, 4}, 181, ints{4, 5, 5}}, + {ints{9, 9, 9}, 1, ints{1, 0, 0, 0}}, + {ints{1, 0, 0, 0, 0}, 9999, ints{1, 9, 9, 9, 9}}, + {ints{0}, 1000, ints{1, 0, 0, 0}}, + } { + io.WriteString(os.Stdout, cmp.Diff(data.ints.process(data.int), data.output)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-344/pokgopun/go/ch-2.go b/challenge-344/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..b1044e41e1 --- /dev/null +++ b/challenge-344/pokgopun/go/ch-2.go @@ -0,0 +1,153 @@ +//# 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 + +package main + +import ( + "io" + "iter" + "os" + "slices" + + "github.com/google/go-cmp/cmp" +) + +func permutations[E any](s []E, r int) iter.Seq[[]E] { + return func(yield func([]E) bool) { + n := len(s) + if r == 0 || n == 0 || r > n { + return + } + if !yield(s[:r]) { + return + } + res := make([]E, r) + idx := make([]int, n) + for v := range n { + idx[v] = v + } + cyc := append(idx[n-r+1:], n) + slices.Reverse(cyc) + for { + i := r + for i > 0 { + i-- + cyc[i]-- + if cyc[i] == 0 { + copy(idx[i:], append(idx[i+1:], idx[i])) + cyc[i] = n - i + } else { + j := cyc[i] + idx[i], idx[n-j] = idx[n-j], idx[i] + for i, v := range idx[:r] { + res[i] = s[v] + } + if !yield(res) { + return + } + break + } + if i > 0 { + continue + } + return + } + } + } +} + +type ints []int + +func af(source []ints, target ints) bool { + l := len(target) + for pmt := range permutations(source, len(source)) { + t := make(ints, l) + i := 0 + for _, p := range pmt { + i += copy(t[i:], p) + } + if i != l { + return false + } + if cmp.Diff(target, t) == "" { + return true + } + } + return false +} + +func main() { + for _, data := range []struct { + source []ints + target ints + output bool + }{ + {[]ints{ints{2, 3}, ints{1}, ints{4}}, ints{1, 2, 3, 4}, true}, + {[]ints{ints{1, 3}, ints{2, 4}}, ints{1, 2, 3, 4}, false}, + {[]ints{ints{9, 1}, ints{5, 8}, ints{2}}, ints{5, 8, 2, 9, 1}, true}, + {[]ints{ints{1}, ints{3}}, ints{1, 2, 3}, false}, + {[]ints{ints{7, 4, 6}}, ints{7, 4, 6}, true}, + } { + //fmt.Println(data.source, data.target, data.output) + io.WriteString(os.Stdout, cmp.Diff(af(data.source, data.target), data.output)) // blank if ok, otherwise show the difference + } +} 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() |
