aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPok <pok@goyangi>2025-09-15 14:57:50 +0700
committerPok <pok@goyangi>2025-09-15 15:22:26 +0700
commit1303c231ef355bea64e4f7547dbea1c99f9ef971 (patch)
tree917d0f11a04720851e1c84b5e6baad7546ce3bd6
parente989fb26d543d53bd610250db5125396daeb2477 (diff)
downloadperlweeklychallenge-club-1303c231ef355bea64e4f7547dbea1c99f9ef971.tar.gz
perlweeklychallenge-club-1303c231ef355bea64e4f7547dbea1c99f9ef971.tar.bz2
perlweeklychallenge-club-1303c231ef355bea64e4f7547dbea1c99f9ef971.zip
pwc339 solution in go
-rw-r--r--challenge-339/pokgopun/go/ch-1.go148
-rw-r--r--challenge-339/pokgopun/go/ch-2.go134
2 files changed, 282 insertions, 0 deletions
diff --git a/challenge-339/pokgopun/go/ch-1.go b/challenge-339/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..4839a8cc33
--- /dev/null
+++ b/challenge-339/pokgopun/go/ch-1.go
@@ -0,0 +1,148 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-339/
+/*#
+
+Task 1: Max Diff
+
+Submitted by: [42]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given an array of integers having four or more elements.
+
+ Write a script to find two pairs of numbers from this list (four
+ numbers total) so that the difference between their products is as
+ large as possible.
+
+ In the end return the max difference.
+
+ With Two pairs (a, b) and (c, d), the product difference is (a * b)
+ - (c * d).
+
+Example 1
+
+Input: @ints = (5, 9, 3, 4, 6)
+Output: 42
+
+Pair 1: (9, 6)
+Pair 2: (3, 4)
+Product Diff: (9 * 6) - (3 * 4) => 54 - 12 => 42
+
+Example 2
+
+Input: @ints = (1, -2, 3, -4)
+Output: 8
+
+Pair 1: (3, 1)
+Pair 2: (-2, -4)
+
+Example 3
+
+Input: @ints = (-3, -1, -2, -4)
+Output: 10
+
+Pair 1: (-1, -2)
+Pair 2: (-3, -4)
+
+Example 4
+
+Input: @ints = (10, 2, 0, 5, 1)
+Output: 50
+
+Pair 1: (10, 5)
+Pair 2: (0, 1)
+
+Example 5
+
+Input: @ints = (7, 8, 9, 10, 10)
+Output: 44
+
+Pair 1: (10, 10)
+Pair 2: (7, 8)
+
+Task 2: Peak Point
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "iter"
+ "os"
+ "slices"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+type pair [2]int
+
+func (pr pair) prod() int {
+ return pr[0] * pr[1]
+}
+
+func (pr pair) diff(p pair) int {
+ d1, d2 := pr.prod(), p.prod()
+ return max(d1, d2) - min(d1, d2)
+}
+
+func (pr pair) uniq(p pair) bool {
+ s := []int{pr[0], pr[1], p[0], p[1]}
+ slices.Sort(s)
+ return len(slices.Compact(s)) == 4
+}
+
+type pairs []pair
+
+func indexPairSeq(l int) iter.Seq[pair] {
+ return func(yield func(p pair) bool) {
+ for i := 0; i < l-1; i++ {
+ for j := i + 1; j < l; j++ {
+ if !yield(pair{i, j}) {
+ return
+ }
+ }
+ }
+ }
+}
+
+type ints []int
+
+func (in ints) process() int {
+ idxPrs := slices.Collect(indexPairSeq(len(in)))
+ l := len(idxPrs)
+ var (
+ init bool
+ mx int
+ ps pairs
+ )
+ for i := range indexPairSeq(l) {
+ i1, i2 := idxPrs[i[0]], idxPrs[i[1]]
+ if i1.uniq(i2) == false {
+ continue
+ }
+ v1, v2 := pair{in[i1[0]], in[i1[1]]}, pair{in[i2[0]], in[i2[1]]}
+ d := v1.diff(v2)
+ if init == false || mx < d {
+ init = true
+ mx = d
+ ps = pairs{v1, v2}
+ }
+ }
+ //fmt.Println(ps, mx)
+ r, _ := mx, ps
+ return r
+}
+
+func main() {
+ for _, data := range []struct {
+ input ints
+ output int
+ }{
+ {ints{5, 9, 3, 4, 6}, 42},
+ {ints{1, -2, 3, -4}, 10},
+ {ints{-3, -1, -2, -4}, 10},
+ {ints{10, 2, 0, 5, 1}, 50},
+ {ints{7, 8, 9, 10, 10}, 44},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(data.input.process(), data.output)) // blank if ok, otherwise show the difference
+ }
+}
diff --git a/challenge-339/pokgopun/go/ch-2.go b/challenge-339/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..a56d64bb57
--- /dev/null
+++ b/challenge-339/pokgopun/go/ch-2.go
@@ -0,0 +1,134 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-339/
+/*#
+
+Task 2: Peak Point
+
+Submitted by: [43]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given an array of altitude gain.
+
+ Write a script to find the peak point gained.
+
+Example 1
+
+Input: @gain = (-5, 1, 5, -9, 2)
+Output: 1
+
+start: 0
+1st change: 0 + (-5) = -5
+2nd change: -5 + 1 = -4
+3rd change: -4 + 5 = 1
+4th change: 1 + (-9) = -8
+5th change: -8 + 2 = -6
+
+max(0, -5, -4, 1, -8, -6) = 1
+
+Example 2
+
+Input: @gain = (10, 10, 10, -25)
+Output: 30
+
+start: 0
+1st change: 0 + 10 = 10
+2nd change: 10 + 10 = 20
+3rd change: 20 + 10 = 30
+4th change: 30 + (-25) = 5
+
+max(0, 10, 20, 30, 5) = 30
+
+Example 3
+
+Input: @gain = (3, -4, 2, 5, -6, 1)
+Output: 6
+
+start: 0
+1st change: 0 + 3 = 3
+2nd change: 3 + (-4) = -1
+3rd change: -1 + 2 = 1
+4th change: 1 + 5 = 6
+5th change: 6 + (-6) = 0
+6th change: 0 + 1 = 1
+
+max(0, 3, -1, 1, 6, 0, 1) = 6
+
+Example 4
+
+Input: @gain = (-1, -2, -3, -4)
+Output: 0
+
+start: 0
+1st change: 0 + (-1) = -1
+2nd change: -1 + (-2) = -3
+3rd change: -3 + (-3) = -6
+4th change: -6 + (-4) = -10
+
+max(0, -1, -3, -6, -10) = 0
+
+Example 5
+
+Input: @gain = (-10, 15, 5)
+Output: 10
+
+start: 0
+1st change: 0 + (-10) = -10
+2nd change: -10 + 15 = 5
+3rd change: 5 + 5 = 10
+
+max(0, -10, 5, 10) = 10
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 21st September
+ 2025.
+ __________________________________________________________________
+
+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 (in ints) process() int {
+ sm := 0
+ mx := sm
+ for _, v := range in {
+ sm += v
+ //fmt.Println(sm)
+ if mx < sm {
+ mx = sm
+ }
+ }
+ return mx
+}
+
+func main() {
+ for _, data := range []struct {
+ input ints
+ output int
+ }{
+ {ints{-5, 1, 5, -9, 2}, 1},
+ {ints{10, 10, 10, -25}, 30},
+ {ints{3, -4, 2, 5, -6, 1}, 6},
+ {ints{-1, -2, -3, -4}, 0},
+ {ints{-10, 15, 5}, 10},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(data.input.process(), data.output)) // blank if ok, otherwise show the difference
+ }
+}
+
+/*
+ {ints{5, 9, 3, 4, 6}, 42},
+ {ints{1, -2, 3, -4}, 8},
+ {ints{-3, -1, -2, -4}, 10},
+ {ints{10, 2, 0, 5, 1}, 50},
+ {ints{7, 8, 9, 10, 10}, 44},
+*/