diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-03-21 17:40:48 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-03-21 17:40:48 +0000 |
| commit | 8324f1a32086d3c296f2cc1aa2e2d36456213810 (patch) | |
| tree | 93a5c085e974aa2ab1ffba5a92928325a4067dbc | |
| parent | 896df0cd1b451cad96a23257285738c9a5387a5c (diff) | |
| parent | 004f070b39938d856fe969c2f5598d3e7d56df66 (diff) | |
| download | perlweeklychallenge-club-8324f1a32086d3c296f2cc1aa2e2d36456213810.tar.gz perlweeklychallenge-club-8324f1a32086d3c296f2cc1aa2e2d36456213810.tar.bz2 perlweeklychallenge-club-8324f1a32086d3c296f2cc1aa2e2d36456213810.zip | |
Merge pull request #9787 from pokgopun/pwc261
pwc261 solution
| -rw-r--r-- | challenge-261/pokgopun/go/ch-1.go | 105 | ||||
| -rw-r--r-- | challenge-261/pokgopun/go/ch-2.go | 88 | ||||
| -rw-r--r-- | challenge-261/pokgopun/python/ch-1.py | 71 | ||||
| -rw-r--r-- | challenge-261/pokgopun/python/ch-2.py | 71 |
4 files changed, 335 insertions, 0 deletions
diff --git a/challenge-261/pokgopun/go/ch-1.go b/challenge-261/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..984774f1d9 --- /dev/null +++ b/challenge-261/pokgopun/go/ch-1.go @@ -0,0 +1,105 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-261/ +/*# + +Task 1: Element Digit Sum + +Submitted by: [45]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an array of integers, @ints. + + Write a script to evaluate the absolute difference between element and + digit sum of the given array. + +Example 1 + +Input: @ints = (1,2,3,45) +Output: 36 + +Element Sum: 1 + 2 + 3 + 45 = 51 +Digit Sum: 1 + 2 + 3 + 4 + 5 = 15 +Absolute Difference: | 51 - 15 | = 36 + +Example 2 + +Input: @ints = (1,12,3) +Output: 9 + +Element Sum: 1 + 12 + 3 = 16 +Digit Sum: 1 + 1 + 2 + 3 = 7 +Absolute Difference: | 16 - 7 | = 9 + +Example 3 + +Input: @ints = (1,2,3,4) +Output: 0 + +Element Sum: 1 + 2 + 3 + 4 = 10 +Digit Sum: 1 + 2 + 3 + 4 = 10 +Absolute Difference: | 10 - 10 | = 0 + +Example 4 + +Input: @ints = (236, 416, 336, 350) +Output: 1296 + +Task 2: Multiply by Two +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + + "github.com/google/go-cmp/cmp" +) + +func sumDigit(n int) int { + if n < 11 { + return n + } + s := 0 + for n > 0 { + s += n % 10 + n /= 10 + } + return s +} + +type ints []int + +func (is ints) sum() int { + s := 0 + for _, v := range is { + s += v + } + return s +} + +func (is ints) sumDigit() int { + s := 0 + for _, v := range is { + s += sumDigit(v) + } + return s +} + +func (is ints) eds() int { + return is.sum() - is.sumDigit() +} + +func main() { + for _, data := range []struct { + input ints + output int + }{ + {ints{1, 2, 3, 45}, 36}, + {ints{1, 12, 3}, 9}, + {ints{1, 2, 3, 4}, 0}, + {ints{236, 416, 336, 350}, 1296}, + } { + io.WriteString(os.Stdout, cmp.Diff(data.input.eds(), data.output)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-261/pokgopun/go/ch-2.go b/challenge-261/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..79552f497b --- /dev/null +++ b/challenge-261/pokgopun/go/ch-2.go @@ -0,0 +1,88 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-261/ +/*# + +Task 2: Multiply by Two + +Submitted by: [46]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an array of integers, @ints and an integer $start.. + + Write a script to do the followings: +a) Look for $start in the array @ints, if found multiply the number by 2 +b) If not found stop the process otherwise repeat + + In the end return the final value. + +Example 1 + +Input: @ints = (5,3,6,1,12) and $start = 3 +Output: 24 + +Step 1: 3 is in the array so 3 x 2 = 6 +Step 2: 6 is in the array so 6 x 2 = 12 +Step 3: 12 is in the array so 12 x 2 = 24 + +24 is not found in the array so return 24. + +Example 2 + +Input: @ints = (1,2,4,3) and $start = 1 +Output: 8 + +Step 1: 1 is in the array so 1 x 2 = 2 +Step 2: 2 is in the array so 2 x 2 = 4 +Step 3: 4 is in the array so 4 x 2 = 8 + +8 is not found in the array so return 8. + +Example 3 + +Input: @ints = (5,6,7) and $start = 2 +Output: 2 + +2 is not found in the array so return 2. + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 24th March + 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + "slices" + + "github.com/google/go-cmp/cmp" +) + +type ints []int + +func (is ints) mb2(start int) int { + slices.Sort(is) + for _, v := range is { + if v == start { + start *= 2 + } + } + return start +} + +func main() { + for _, data := range []struct { + ints ints + start, end int + }{ + {ints{5, 3, 6, 1, 12}, 3, 24}, + {ints{1, 2, 4, 3}, 1, 8}, + {ints{5, 6, 7}, 2, 2}, + } { + io.WriteString(os.Stdout, cmp.Diff(data.ints.mb2(data.start), data.end)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-261/pokgopun/python/ch-1.py b/challenge-261/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..073ab918fa --- /dev/null +++ b/challenge-261/pokgopun/python/ch-1.py @@ -0,0 +1,71 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-261/ +""" + +Task 1: Element Digit Sum + +Submitted by: [45]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an array of integers, @ints. + + Write a script to evaluate the absolute difference between element and + digit sum of the given array. + +Example 1 + +Input: @ints = (1,2,3,45) +Output: 36 + +Element Sum: 1 + 2 + 3 + 45 = 51 +Digit Sum: 1 + 2 + 3 + 4 + 5 = 15 +Absolute Difference: | 51 - 15 | = 36 + +Example 2 + +Input: @ints = (1,12,3) +Output: 9 + +Element Sum: 1 + 12 + 3 = 16 +Digit Sum: 1 + 1 + 2 + 3 = 7 +Absolute Difference: | 16 - 7 | = 9 + +Example 3 + +Input: @ints = (1,2,3,4) +Output: 0 + +Element Sum: 1 + 2 + 3 + 4 = 10 +Digit Sum: 1 + 2 + 3 + 4 = 10 +Absolute Difference: | 10 - 10 | = 0 + +Example 4 + +Input: @ints = (236, 416, 336, 350) +Output: 1296 + +Task 2: Multiply by Two +""" +### solution by pokgopun@gmail.com + +from itertools import chain + +def EDS(ints: tuple): + return sum(ints) - sum( + int(e) for e in chain.from_iterable( + str(i) for i in ints + ) + ) + +import unittest + +class TestEDS(unittest.TestCase): + def test(self): + for inpt, otpt in { + (1,2,3,45): 36, + (1,12,3): 9, + (1,2,3,4): 0, + (236, 416, 336, 350): 1296, + }.items(): + self.assertEqual(EDS(inpt),otpt) + +unittest.main() diff --git a/challenge-261/pokgopun/python/ch-2.py b/challenge-261/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..f8e85bf717 --- /dev/null +++ b/challenge-261/pokgopun/python/ch-2.py @@ -0,0 +1,71 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-261/ +""" + +Task 2: Multiply by Two + +Submitted by: [46]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an array of integers, @ints and an integer $start.. + + Write a script to do the followings: +a) Look for $start in the array @ints, if found multiply the number by 2 +b) If not found stop the process otherwise repeat + + In the end return the final value. + +Example 1 + +Input: @ints = (5,3,6,1,12) and $start = 3 +Output: 24 + +Step 1: 3 is in the array so 3 x 2 = 6 +Step 2: 6 is in the array so 6 x 2 = 12 +Step 3: 12 is in the array so 12 x 2 = 24 + +24 is not found in the array so return 24. + +Example 2 + +Input: @ints = (1,2,4,3) and $start = 1 +Output: 8 + +Step 1: 1 is in the array so 1 x 2 = 2 +Step 2: 2 is in the array so 2 x 2 = 4 +Step 3: 4 is in the array so 4 x 2 = 8 + +8 is not found in the array so return 8. + +Example 3 + +Input: @ints = (5,6,7) and $start = 2 +Output: 2 + +2 is not found in the array so return 2. + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 24th March + 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +def MB2(ints: tuple, start: int): + while start in ints: + start *= 2 + return start + +import unittest + +class TestMB2(unittest.TestCase): + def test(self): + for (ints, start), otpt in { + ((5,3,6,1,12),3): 24, + ((1,2,4,3),1): 8, + ((5,6,7),2): 2, + }.items(): + self.assertEqual(MB2(ints,start), otpt) + +unittest.main() |
