diff options
| -rw-r--r-- | challenge-262/pokgopun/go/ch-1.go | 77 | ||||
| -rw-r--r-- | challenge-262/pokgopun/go/ch-2.go | 74 | ||||
| -rw-r--r-- | challenge-262/pokgopun/python/ch-1.py | 60 | ||||
| -rw-r--r-- | challenge-262/pokgopun/python/ch-2.py | 58 |
4 files changed, 269 insertions, 0 deletions
diff --git a/challenge-262/pokgopun/go/ch-1.go b/challenge-262/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..7fc74a7531 --- /dev/null +++ b/challenge-262/pokgopun/go/ch-1.go @@ -0,0 +1,77 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-262/ +/*# + +Task 1: Max Positive Negative + +Submitted by: [46]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an array of integers, @ints. + + Write a script to return the maximum number of either positive or + negative integers in the given array. + +Example 1 + +Input: @ints = (-3, 1, 2, -1, 3, -2, 4) +Output: 4 + +Count of positive integers: 4 +Count of negative integers: 3 +Maximum of count of positive and negative integers: 4 + +Example 2 + +Input: @ints = (-1, -2, -3, 1) +Output: 3 + +Count of positive integers: 1 +Count of negative integers: 3 +Maximum of count of positive and negative integers: 3 + +Example 3 + +Input: @ints = (1,2) +Output: 2 + +Count of positive integers: 2 +Count of negative integers: 0 +Maximum of count of positive and negative integers: 2 + +Task 2: Count Equal Divisible +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + + "github.com/google/go-cmp/cmp" +) + +type ints []int + +func (is ints) max() int { + c := 0 + for _, v := range is { + if v < 0 { + c++ + } + } + return max(len(is)-c, c) +} + +func main() { + for _, data := range []struct { + input ints + output int + }{ + {ints{-3, 1, 2, -1, 3, -2, 4}, 4}, + {ints{-1, -2, -3, 1}, 3}, + {ints{1, 2}, 2}, + } { + io.WriteString(os.Stdout, cmp.Diff(data.input.max(), data.output)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-262/pokgopun/go/ch-2.go b/challenge-262/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..eea313a253 --- /dev/null +++ b/challenge-262/pokgopun/go/ch-2.go @@ -0,0 +1,74 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-262/ +/*# + +Task 2: Count Equal Divisible + +Submitted by: [47]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an array of integers, @ints and an integer $k. + + Write a script to return the number of pairs (i, j) where +a) 0 <= i < j < size of @ints +b) ints[i] == ints[j] +c) i x j is divisible by k + +Example 1 + +Input: @ints = (3,1,2,2,2,1,3) and $k = 2 +Output: 4 + +(0, 6) => ints[0] == ints[6] and 0 x 6 is divisible by 2 +(2, 3) => ints[2] == ints[3] and 2 x 3 is divisible by 2 +(2, 4) => ints[2] == ints[4] and 2 x 4 is divisible by 2 +(3, 4) => ints[3] == ints[4] and 3 x 4 is divisible by 2 + +Example 2 + +Input: @ints = (1,2,3) and $k = 1 +Output: 0 + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 31st March + 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + + "github.com/google/go-cmp/cmp" +) + +type ints []int + +func (is ints) ced(k int) int { + c := 0 + l := len(is) + for i := 0; i < l-1; i++ { + for j := i + 1; j < l; j++ { + if is[i] == is[j] && (i*j)%2 == 0 { + c++ + } + } + } + return c +} + +func main() { + for _, data := range []struct { + nums ints + k, output int + }{ + {ints{3, 1, 2, 2, 2, 1, 3}, 2, 4}, + {ints{1, 2, 3}, 1, 0}, + } { + io.WriteString(os.Stdout, cmp.Diff(data.nums.ced(data.k), data.output)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-262/pokgopun/python/ch-1.py b/challenge-262/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..e2d03ff95f --- /dev/null +++ b/challenge-262/pokgopun/python/ch-1.py @@ -0,0 +1,60 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-262/ +""" + +Task 1: Max Positive Negative + +Submitted by: [46]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an array of integers, @ints. + + Write a script to return the maximum number of either positive or + negative integers in the given array. + +Example 1 + +Input: @ints = (-3, 1, 2, -1, 3, -2, 4) +Output: 4 + +Count of positive integers: 4 +Count of negative integers: 3 +Maximum of count of positive and negative integers: 4 + +Example 2 + +Input: @ints = (-1, -2, -3, 1) +Output: 3 + +Count of positive integers: 1 +Count of negative integers: 3 +Maximum of count of positive and negative integers: 3 + +Example 3 + +Input: @ints = (1,2) +Output: 2 + +Count of positive integers: 2 +Count of negative integers: 0 +Maximum of count of positive and negative integers: 2 + +Task 2: Count Equal Divisible +""" +### solution by pokgopun@gmail.com + +def mpn(ints: tuple): + n = sum(1 for i in ints if i < 0) + return max(n, len(ints) - n) + +import unittest + +class TestMpn(unittest.TestCase): + def test(self): + for inpt, otpt in { + (-3, 1, 2, -1, 3, -2, 4): 4, + (-1, -2, -3, 1): 3, + (1,2): 2, + }.items(): + self.assertEqual(mpn(inpt),otpt) + +unittest.main() diff --git a/challenge-262/pokgopun/python/ch-2.py b/challenge-262/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..9ab45e565a --- /dev/null +++ b/challenge-262/pokgopun/python/ch-2.py @@ -0,0 +1,58 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-262/ +""" + +Task 2: Count Equal Divisible + +Submitted by: [47]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an array of integers, @ints and an integer $k. + + Write a script to return the number of pairs (i, j) where +a) 0 <= i < j < size of @ints +b) ints[i] == ints[j] +c) i x j is divisible by k + +Example 1 + +Input: @ints = (3,1,2,2,2,1,3) and $k = 2 +Output: 4 + +(0, 6) => ints[0] == ints[6] and 0 x 6 is divisible by 2 +(2, 3) => ints[2] == ints[3] and 2 x 3 is divisible by 2 +(2, 4) => ints[2] == ints[4] and 2 x 4 is divisible by 2 +(3, 4) => ints[3] == ints[4] and 3 x 4 is divisible by 2 + +Example 2 + +Input: @ints = (1,2,3) and $k = 1 +Output: 0 + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 31st March + 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +from itertools import combinations + +def ced(ints: tuple, k: int): + return sum( + 1 for e in combinations(range(len(ints)),2) + if (e[0]*e[1]) % 2 == 0 and ints[e[0]]==ints[e[1]] + ) + +import unittest + +class TestCed(unittest.TestCase): + def test(self): + for otpt, (ints, k) in { + 4: ((3,1,2,2,2,1,3), 2), + 0: ((1,2,3), 1), + }.items(): + self.assertEqual(otpt, ced(ints, k)) + +unittest.main() |
