diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-05-06 15:37:36 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-05-06 15:37:36 +0100 |
| commit | 5090f83d041c83d9448d2b6369ab91ee16b8bc79 (patch) | |
| tree | 12aaabb2935c26a2711c863352a78020528622ae | |
| parent | aeee8846c43a30b6c9ce0a368aadee695b261ecc (diff) | |
| parent | 811a6e59746d91ffd24fbe96494aacfcb969a664 (diff) | |
| download | perlweeklychallenge-club-5090f83d041c83d9448d2b6369ab91ee16b8bc79.tar.gz perlweeklychallenge-club-5090f83d041c83d9448d2b6369ab91ee16b8bc79.tar.bz2 perlweeklychallenge-club-5090f83d041c83d9448d2b6369ab91ee16b8bc79.zip | |
Merge pull request #11986 from pokgopun/pwc320
Pwc320
| -rw-r--r-- | challenge-320/pokgopun/go/ch-1.go | 80 | ||||
| -rw-r--r-- | challenge-320/pokgopun/go/ch-2.go | 99 | ||||
| -rw-r--r-- | challenge-320/pokgopun/python/ch-1.py | 62 | ||||
| -rw-r--r-- | challenge-320/pokgopun/python/ch-2.py | 70 |
4 files changed, 311 insertions, 0 deletions
diff --git a/challenge-320/pokgopun/go/ch-1.go b/challenge-320/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..3873853b7b --- /dev/null +++ b/challenge-320/pokgopun/go/ch-1.go @@ -0,0 +1,80 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-320/ +/*# + +Task 1: Maximum Count + +Submitted by: [43]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an array of integers. + + Write a script to return the maximum between the number of positive and + negative integers. Zero is neither positive nor negative. + +Example 1 + +Input: @ints = (-3, -2, -1, 1, 2, 3) +Output: 3 + +There are 3 positive integers. +There are 3 negative integers. +The maximum between 3 and 3 is 3. + +Example 2 + +Input: @ints = (-2, -1, 0, 0, 1) +Output: 2 + +There are 1 positive integers. +There are 2 negative integers. +The maximum between 2 and 1 is 2. + +Example 3 + +Input: @ints = (1, 2, 3, 4) +Output: 4 + +There are 4 positive integers. +There are 0 negative integers. +The maximum between 4 and 0 is 4. + +Task 2: Sum Difference +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + + "github.com/google/go-cmp/cmp" +) + +type input []int + +func (in input) process() int { + var cntn, cntp int + for _, v := range in { + switch { + case v < 0: + cntn++ + case v > 0: + cntp++ + } + } + return max(cntn, cntp) +} + +func main() { + for _, data := range []struct { + input input + output int + }{ + {input{-3, -2, -1, 1, 2, 3}, 3}, + {input{-2, -1, 0, 0, 1}, 2}, + {input{1, 2, 3, 4}, 4}, + } { + io.WriteString(os.Stdout, cmp.Diff(data.input.process(), data.output)) // blank if ok, otherwise the differences + } +} diff --git a/challenge-320/pokgopun/go/ch-2.go b/challenge-320/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..bbe2f8b9aa --- /dev/null +++ b/challenge-320/pokgopun/go/ch-2.go @@ -0,0 +1,99 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-320/ +/*# + +Task 2: Sum Difference + +Submitted by: [44]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an array of positive integers. + + Write a script to return the absolute difference between digit sum and + element sum of the given array. + +Example 1 + +Input: @ints = (1, 23, 4, 5) +Output: 18 + +Element sum: 1 + 23 + 4 + 5 => 33 +Digit sum: 1 + 2 + 3 + 4 + 5 => 15 +Absolute difference: | 33 - 15 | => 18 + +Example 2 + +Input: @ints = (1, 2, 3, 4, 5) +Output: 0 + +Element sum: 1 + 2 + 3 + 4 + 5 => 15 +Digit sum: 1 + 2 + 3 + 4 + 5 => 15 +Absolute difference: | 15 - 15 | => 0 + +Example 3 + +Input: @ints = (1, 2, 34) +Output: 27 + +Element sum: 1 + 2 + 34 => 37 +Digit sum: 1 + 2 + 3 + 4 => 10 +Absolute difference: | 37 - 10 | => 27 + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 11th May 2025. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + "strconv" + + "github.com/google/go-cmp/cmp" +) + +type input []int + +func (in input) process() int { + sm := in.sum() + smd := in.sumDigit() + if sm > smd { + return sm - smd + } + return smd - sm +} + +func (in input) sum() int { + sm := 0 + for _, v := range in { + sm += v + } + return sm +} + +func (in input) sumDigit() int { + sm := 0 + for _, v := range in { + for _, r := range strconv.Itoa(v) { + sm += int(r - '0') + } + } + return sm +} + +func main() { + for _, data := range []struct { + input input + output int + }{ + {input{1, 23, 4, 5}, 18}, + {input{1, 2, 3, 4, 5}, 0}, + {input{1, 2, 34}, 27}, + } { + io.WriteString(os.Stdout, cmp.Diff(data.input.process(), data.output)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-320/pokgopun/python/ch-1.py b/challenge-320/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..4091725ad5 --- /dev/null +++ b/challenge-320/pokgopun/python/ch-1.py @@ -0,0 +1,62 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-320/ +""" + +Task 1: Maximum Count + +Submitted by: [43]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an array of integers. + + Write a script to return the maximum between the number of positive and + negative integers. Zero is neither positive nor negative. + +Example 1 + +Input: @ints = (-3, -2, -1, 1, 2, 3) +Output: 3 + +There are 3 positive integers. +There are 3 negative integers. +The maximum between 3 and 3 is 3. + +Example 2 + +Input: @ints = (-2, -1, 0, 0, 1) +Output: 2 + +There are 1 positive integers. +There are 2 negative integers. +The maximum between 2 and 1 is 2. + +Example 3 + +Input: @ints = (1, 2, 3, 4) +Output: 4 + + +There are 4 positive integers. +There are 0 negative integers. +The maximum between 4 and 0 is 4. + +Task 2: Sum Difference +""" +### solution by pokgopun@gmail.com + +def mc(ints: tuple[int]) -> int: + return max( sum(1 for e in ints if e < 0), sum(1 for e in ints if e > 0)) + +import unittest + +class TestMc(unittest.TestCase): + def test(self): + for inpt, otpt in { + (-3, -2, -1, 1, 2, 3): 3, + (-2, -1, 0, 0, 1): 2, + (1, 2, 3, 4): 4, + }.items(): + self.assertEqual(mc(inpt), otpt) + +unittest.main() + + diff --git a/challenge-320/pokgopun/python/ch-2.py b/challenge-320/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..63cdd60e66 --- /dev/null +++ b/challenge-320/pokgopun/python/ch-2.py @@ -0,0 +1,70 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-320/ +""" + +Task 2: Sum Difference + +Submitted by: [44]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an array of positive integers. + + Write a script to return the absolute difference between digit sum and + element sum of the given array. + +Example 1 + +Input: @ints = (1, 23, 4, 5) +Output: 18 + +Element sum: 1 + 23 + 4 + 5 => 33 +Digit sum: 1 + 2 + 3 + 4 + 5 => 15 +Absolute difference: | 33 - 15 | => 18 + +Example 2 + +Input: @ints = (1, 2, 3, 4, 5) +Output: 0 + +Element sum: 1 + 2 + 3 + 4 + 5 => 15 +Digit sum: 1 + 2 + 3 + 4 + 5 => 15 +Absolute difference: | 15 - 15 | => 0 + +Example 3 + +Input: @ints = (1, 2, 34) +Output: 27 + +Element sum: 1 + 2 + 34 => 37 +Digit sum: 1 + 2 + 3 + 4 => 10 +Absolute difference: | 37 - 10 | => 27 + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 11th May 2025. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +def sd(ints: tuple[int]) -> int: + return abs( + sum(ints) - sum( + int(d) for d in "".join( + str(e) for e in ints + ) + ) + ) + +import unittest + +class TestSd(unittest.TestCase): + def test(self): + for inpt, otpt in { + (1, 23, 4, 5): 18, + (1, 2, 3, 4, 5): 0, + (1, 2, 34): 27, + }.items(): + self.assertEqual(sd(inpt), otpt) + +unittest.main() + |
