diff options
| author | Pok <pok@goyangi> | 2025-06-23 18:05:26 +1000 |
|---|---|---|
| committer | Pok <pok@goyangi> | 2025-06-23 18:05:26 +1000 |
| commit | 023165766dbfa4199ac91455eb8982c6c445ce2b (patch) | |
| tree | 3d12dd26a168ed4e0907f0007bc323b58408702b | |
| parent | b74e62fef15ea0c4c967f79b41d4e7639f5ae066 (diff) | |
| download | perlweeklychallenge-club-023165766dbfa4199ac91455eb8982c6c445ce2b.tar.gz perlweeklychallenge-club-023165766dbfa4199ac91455eb8982c6c445ce2b.tar.bz2 perlweeklychallenge-club-023165766dbfa4199ac91455eb8982c6c445ce2b.zip | |
pwc327 solution in go
| -rw-r--r-- | challenge-327/pokgopun/go/ch-1.go | 74 | ||||
| -rw-r--r-- | challenge-327/pokgopun/go/ch-2.go | 94 |
2 files changed, 168 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 + } +} |
