diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-05-06 11:03:51 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-05-06 11:03:51 +0100 |
| commit | 2613c50fbf02be26a82bb14a464fa61d2afc23ac (patch) | |
| tree | 976ee9d2e1c8782d66958d91ce4791b7f882a43d | |
| parent | 9321438807960ff715b29491b005ec23c9ab4bf1 (diff) | |
| parent | 73e95dd324f1bd37dbbefdfff0d37cbe6008acfb (diff) | |
| download | perlweeklychallenge-club-2613c50fbf02be26a82bb14a464fa61d2afc23ac.tar.gz perlweeklychallenge-club-2613c50fbf02be26a82bb14a464fa61d2afc23ac.tar.bz2 perlweeklychallenge-club-2613c50fbf02be26a82bb14a464fa61d2afc23ac.zip | |
Merge pull request #10046 from pokgopun/pwc268
pwc268 solution
| -rw-r--r-- | challenge-268/pokgopun/go/ch-1.go | 95 | ||||
| -rw-r--r-- | challenge-268/pokgopun/go/ch-2.go | 86 | ||||
| -rw-r--r-- | challenge-268/pokgopun/python/ch-1.py | 69 | ||||
| -rw-r--r-- | challenge-268/pokgopun/python/ch-2.py | 65 |
4 files changed, 315 insertions, 0 deletions
diff --git a/challenge-268/pokgopun/go/ch-1.go b/challenge-268/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..7869d90839 --- /dev/null +++ b/challenge-268/pokgopun/go/ch-1.go @@ -0,0 +1,95 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-268/ +/*# + +Task 1: Magic Number + +Submitted by: [47]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given two arrays of integers of same size, @x and @y. + + Write a script to find the magic number that when added to each + elements of one of the array gives the second array. Elements order is + not important. + +Example 1 + +Input: @x = (3, 7, 5) + @y = (9, 5, 7) +Output: 2 + +The magic number is 2. +@x = (3, 7, 5) + + 2 2 2 +@y = (5, 9, 7) + +Example 2 + +Input: @x = (1, 2, 1) + @y = (5, 4, 4) +Output: 3 + +The magic number is 3. +@x = (1, 2, 1) + + 3 3 3 +@y = (5, 4, 4) + +Example 3 + +Input: @x = (2) + @y = (5) +Output: 3 + +Task 2: Number Game +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + "slices" + + "github.com/google/go-cmp/cmp" +) + +type ints []int + +type Answer struct { + Val int + None bool +} + +func magicNumber(x, y ints) Answer { + l := len(x) + if l == 0 || l != len(y) { + return Answer{None: true} + } + slices.Sort(x) + slices.Sort(y) + l-- + d := y[l] - x[l] + for i := range l { + if d != y[i]-x[i] { + return Answer{None: true} + } + } + return Answer{Val: d} +} + +func main() { + for _, data := range []struct { + x, y ints + ans Answer + }{ + {ints{3, 7, 5}, ints{9, 5, 7}, Answer{Val: 2}}, + {ints{1, 2, 1}, ints{5, 4, 4}, Answer{Val: 3}}, + {ints{1, 2, 2}, ints{5, 4, 4}, Answer{None: true}}, + {ints{2}, ints{5}, Answer{Val: 3}}, + {ints{2}, ints{5, 4}, Answer{None: true}}, + {ints{}, ints{}, Answer{None: true}}, + } { + io.WriteString(os.Stdout, cmp.Diff(magicNumber(data.x, data.y), data.ans)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-268/pokgopun/go/ch-2.go b/challenge-268/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..9dc4ef0482 --- /dev/null +++ b/challenge-268/pokgopun/go/ch-2.go @@ -0,0 +1,86 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-268/ +/*# + +Task 2: Number Game + +Submitted by: [48]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an array of integers, @ints, with even number of + elements. + + Write a script to create a new array made up of elements of the given + array. Pick the two smallest integers and add it to new array in + decreasing order i.e. high to low. Keep doing until the given array is + empty. + +Example 1 + +Input: @ints = (2, 5, 3, 4) +Output: (3, 2, 5, 4) + +Round 1: we picked (2, 3) and push it to the new array (3, 2) +Round 2: we picked the remaining (4, 5) and push it to the new array (5, 4) + +Example 2 + +Input: @ints = (9, 4, 1, 3, 6, 4, 6, 1) +Output: (1, 1, 4, 3, 6, 4, 9, 6) + +Example 3 + +Input: @ints = (1, 2, 2, 3) +Output: (2, 1, 3, 2) + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 12th May 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 + +type Answer struct { + Arr ints + None bool +} + +func numberGame(is ints) Answer { + l := len(is) + if l%2 != 0 { + return Answer{None: true} + } + slices.Sort(is) + for l > 0 { + is[l-1], is[l-2] = is[l-2], is[l-1] + l -= 2 + } + return Answer{Arr: is} +} + +func main() { + for _, data := range []struct { + input ints + output Answer + }{ + {ints{2, 5, 3, 4}, Answer{Arr: ints{3, 2, 5, 4}}}, + {ints{9, 4, 1, 3, 6, 4, 6, 1}, Answer{Arr: ints{1, 1, 4, 3, 6, 4, 9, 6}}}, + {ints{1, 2, 2, 3}, Answer{Arr: ints{2, 1, 3, 2}}}, + {ints{1, 2, 3}, Answer{None: true}}, + {ints{}, Answer{Arr: ints{}}}, + } { + io.WriteString(os.Stdout, cmp.Diff(numberGame(data.input), data.output)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-268/pokgopun/python/ch-1.py b/challenge-268/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..93a566716a --- /dev/null +++ b/challenge-268/pokgopun/python/ch-1.py @@ -0,0 +1,69 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-268/ +""" + +Task 1: Magic Number + +Submitted by: [47]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given two arrays of integers of same size, @x and @y. + + Write a script to find the magic number that when added to each + elements of one of the array gives the second array. Elements order is + not important. + +Example 1 + +Input: @x = (3, 7, 5) + @y = (9, 5, 7) +Output: 2 + +The magic number is 2. +@x = (3, 7, 5) + + 2 2 2 +@y = (5, 9, 7) + +Example 2 + +Input: @x = (1, 2, 1) + @y = (5, 4, 4) +Output: 3 + +The magic number is 3. +@x = (1, 2, 1) + + 3 3 3 +@y = (5, 4, 4) + +Example 3 + +Input: @x = (2) + @y = (5) +Output: 3 + +Task 2: Number Game +""" +### solution by pokgopun@gmail.com + +def magicNumber(x: tuple, y: tuple): + l = len(x) + if l == 0 or l != len(y): + return None + x, y = sorted(x), sorted(y) + d = y[0]-x[0] + for i in range(1,l): + if d != y[i]-x[i]: + return None + return d + +import unittest + +class TestMagicNumber(unittest.TestCase): + def test(self): + for (x,y), ans in { + ((3, 7, 5),(9, 5, 7)): 2, + ((1, 2, 1),(5, 4, 4)): 3, + ((2,),(5,)): 3, + }.items(): + self.assertEqual(magicNumber(x,y),ans) + +unittest.main() diff --git a/challenge-268/pokgopun/python/ch-2.py b/challenge-268/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..ce4e10e2b4 --- /dev/null +++ b/challenge-268/pokgopun/python/ch-2.py @@ -0,0 +1,65 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-268/ +""" + +Task 2: Number Game + +Submitted by: [48]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an array of integers, @ints, with even number of + elements. + + Write a script to create a new array made up of elements of the given + array. Pick the two smallest integers and add it to new array in + decreasing order i.e. high to low. Keep doing until the given array is + empty. + +Example 1 + +Input: @ints = (2, 5, 3, 4) +Output: (3, 2, 5, 4) + +Round 1: we picked (2, 3) and push it to the new array (3, 2) +Round 2: we picked the remaining (4, 5) and push it to the new array (5, 4) + +Example 2 + +Input: @ints = (9, 4, 1, 3, 6, 4, 6, 1) +Output: (1, 1, 4, 3, 6, 4, 9, 6) + +Example 3 + +Input: @ints = (1, 2, 2, 3) +Output: (2, 1, 3, 2) + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 12th May 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +def numberGame(ints: tuple): + l = len(ints) + if l==0 or l % 2 != 0: + return None + ints = tuple(sorted(ints,reverse=True)) + ans = () + while l > 0: + ans += ints[l-2:l] + l -= 2 + return ans + +import unittest + +class TestNumberGame(unittest.TestCase): + def test(self): + for inpt, otpt in { + (2, 5, 3, 4): (3, 2, 5, 4), + (9, 4, 1, 3, 6, 4, 6, 1): (1, 1, 4, 3, 6, 4, 9, 6), + (1, 2, 2, 3): (2, 1, 3, 2), + }.items(): + self.assertEqual(numberGame(inpt),otpt) + +unittest.main() |
