diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-06-24 23:30:45 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-06-24 23:30:45 +0100 |
| commit | 2c405c527929f1de37f5aa415efca788a3e24221 (patch) | |
| tree | dc0ab19250272fa380ca3a51ea42401efd70ad05 | |
| parent | 85d39f2fc722611afa6cdcb2751444226288e536 (diff) | |
| parent | 023165766dbfa4199ac91455eb8982c6c445ce2b (diff) | |
| download | perlweeklychallenge-club-2c405c527929f1de37f5aa415efca788a3e24221.tar.gz perlweeklychallenge-club-2c405c527929f1de37f5aa415efca788a3e24221.tar.bz2 perlweeklychallenge-club-2c405c527929f1de37f5aa415efca788a3e24221.zip | |
Merge pull request #12225 from pokgopun/pwc327
Pwc327
| -rw-r--r-- | challenge-327/pokgopun/go/ch-1.go | 74 | ||||
| -rw-r--r-- | challenge-327/pokgopun/go/ch-2.go | 94 | ||||
| -rw-r--r-- | challenge-327/pokgopun/python/ch-1.py | 53 | ||||
| -rw-r--r-- | challenge-327/pokgopun/python/ch-2.py | 68 |
4 files changed, 289 insertions, 0 deletions
diff --git a/challenge-327/pokgopun/go/ch-1.go b/challenge-327/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..c5731f2dbd --- /dev/null +++ b/challenge-327/pokgopun/go/ch-1.go @@ -0,0 +1,74 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-327/ +/*# + +Task 1: Missing Integers + +Submitted by: [44]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an array of n integers. + + Write a script to find all the missing integers in the range 1..n in + the given array. + +Example 1 + +Input: @ints = (1, 2, 1, 3, 2, 5) +Output: (4, 6) + +The given array has 6 elements. +So we are looking for integers in the range 1..6 in the given array. +The missing integers: (4, 6) + +Example 2 + +Input: @ints = (1, 1, 1) +Output: (2, 3) + +Example 3 + +Input: @ints = (2, 2, 1) +Output: (3) + +Task 2: MAD +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + + "github.com/google/go-cmp/cmp" +) + +type ints []int + +func (in ints) process() ints { + m := make(map[int]bool) + for _, v := range in { + m[v] = true + } + var s []int + for i := range len(in) { + j := i + 1 + if m[j] { + continue + } + s = append(s, j) + } + return s +} + +func main() { + for _, data := range []struct { + input, output ints + }{ + {ints{1, 2, 1, 3, 2, 5}, ints{4, 6}}, + {ints{1, 1, 1}, ints{2, 3}}, + {ints{2, 2, 1}, ints{3}}, + } { + io.WriteString(os.Stdout, cmp.Diff(data.input.process(), data.output)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-327/pokgopun/go/ch-2.go b/challenge-327/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..7186c99d92 --- /dev/null +++ b/challenge-327/pokgopun/go/ch-2.go @@ -0,0 +1,94 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-327/ +/*# + +Task 2: MAD + +Submitted by: [45]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an array of distinct integers. + + Write a script to find all pairs of elements with minimum absolute + difference (MAD) of any two elements. + +Example 1 + +Input: @ints = (4, 1, 2, 3) +Output: [1,2], [2,3], [3,4] + +The minimum absolute difference is 1. +Pairs with MAD: [1,2], [2,3], [3,4] + +Example 2 + +Input: @ints = (1, 3, 7, 11, 15) +Output: [1,3] + +Example 3 + +Input: @ints = (1, 5, 3, 8) +Output: [1,3], [3,5] + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 29th June 2025. + __________________________________________________________________ + +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 + +type intss []ints + +func (in ints) process() intss { + s := slices.Clone(in) + slices.Sort(s) + l := len(s) + if l < 2 { + return intss{} + } + mn := s[1] - s[0] + i := l + for i > 2 { + i-- + m := s[i] - s[i-1] + if mn > m { + mn = m + } + } + var r intss + for i := range l - 1 { + j := i + 1 + for j < l { + if s[j]-s[i] == mn { + r = append(r, ints{s[i], s[j]}) + } + j++ + } + } + return r +} + +func main() { + for _, data := range []struct { + input ints + output intss + }{ + {ints{4, 1, 2, 3}, intss{ints{1, 2}, ints{2, 3}, ints{3, 4}}}, + {ints{1, 3, 7, 11, 15}, intss{ints{1, 3}}}, + {ints{1, 5, 3, 8}, intss{ints{1, 3}, ints{3, 5}}}, + } { + io.WriteString(os.Stdout, cmp.Diff(data.input.process(), data.output)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-327/pokgopun/python/ch-1.py b/challenge-327/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..729c2f6a01 --- /dev/null +++ b/challenge-327/pokgopun/python/ch-1.py @@ -0,0 +1,53 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-327/ +""" + +Task 1: Missing Integers + +Submitted by: [44]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an array of n integers. + + Write a script to find all the missing integers in the range 1..n in + the given array. + +Example 1 + +Input: @ints = (1, 2, 1, 3, 2, 5) +Output: (4, 6) + +The given array has 6 elements. +So we are looking for integers in the range 1..6 in the given array. +The missing integers: (4, 6) + +Example 2 + +Input: @ints = (1, 1, 1) +Output: (2, 3) + +Example 3 + +Input: @ints = (2, 2, 1) +Output: (3) + +Task 2: MAD +""" +### solution by pokgopun@gmail.com + +def mi(ints: tuple[int]) -> tuple[int]: + return tuple(set(range(1,len(ints)+1)) - set(ints)) + +import unittest + +class TestMi(unittest.TestCase): + def test(self): + for inpt, otpt in { + (1, 2, 1, 3, 2, 5): (4, 6), + (1, 1, 1): (2, 3), + (2, 2, 1): (3,), + }.items(): + self.assertEqual(mi(inpt),otpt) + +unittest.main() + + diff --git a/challenge-327/pokgopun/python/ch-2.py b/challenge-327/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..bffee1db15 --- /dev/null +++ b/challenge-327/pokgopun/python/ch-2.py @@ -0,0 +1,68 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-327/ +""" + +Task 2: MAD + +Submitted by: [45]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an array of distinct integers. + + Write a script to find all pairs of elements with minimum absolute + difference (MAD) of any two elements. + +Example 1 + +Input: @ints = (4, 1, 2, 3) +Output: [1,2], [2,3], [3,4] + +The minimum absolute difference is 1. +Pairs with MAD: [1,2], [2,3], [3,4] + +Example 2 + +Input: @ints = (1, 3, 7, 11, 15) +Output: [1,3] + +Example 3 + +Input: @ints = (1, 5, 3, 8) +Output: [1,3], [3,5] + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 29th June 2025. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +def mad(ints: tuple[int]) -> list[tuple[int]]: + ints = sorted(ints) + l = len(ints) + mn = ints[1] - ints[0] + i = l + while i > 2: + i -= 1 + m = ints[i] - ints[i-1] + if mn > m: + mn = m + lst = [] + for i in range(l-1): + for j in range(i+1,l): + if ints[j] - ints[i] == mn: + lst.append((ints[i],ints[j])) + return lst + +import unittest + +class TestMad(unittest.TestCase): + def test(self): + for inpt, otpt in { + (4, 1, 2, 3): [(1,2), (2,3), (3,4)], + (1, 3, 7, 11, 15): [(1,3)], + (1, 5, 3, 8): [(1,3), (3,5)], + }.items(): + self.assertEqual(mad(inpt),otpt) + +unittest.main() |
