diff options
| author | Michael Manring <michael@manring> | 2024-05-06 17:42:48 +1000 |
|---|---|---|
| committer | Michael Manring <michael@manring> | 2024-05-06 17:42:48 +1000 |
| commit | 73e95dd324f1bd37dbbefdfff0d37cbe6008acfb (patch) | |
| tree | a028f99f84361a8fc3120c627ae1e5e81b6bf817 | |
| parent | c1f23ff188765024d1b7f122ca1f5b2db251bd35 (diff) | |
| download | perlweeklychallenge-club-73e95dd324f1bd37dbbefdfff0d37cbe6008acfb.tar.gz perlweeklychallenge-club-73e95dd324f1bd37dbbefdfff0d37cbe6008acfb.tar.bz2 perlweeklychallenge-club-73e95dd324f1bd37dbbefdfff0d37cbe6008acfb.zip | |
pwc268 solution in go
| -rw-r--r-- | challenge-268/pokgopun/go/ch-1.go | 95 | ||||
| -rw-r--r-- | challenge-268/pokgopun/go/ch-2.go | 86 |
2 files changed, 181 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 + } +} |
