aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpok <pok@ailouros.nemeses.net>2025-08-04 20:03:30 +0700
committerpok <pok@ailouros.nemeses.net>2025-08-04 20:03:30 +0700
commit672769b6bfa365e497069e98347d69bb65666f4b (patch)
tree8e6a6aab37dcfba9ddb0ba1f7b7177015558df9e
parent33210f66a2689be10b3efcb4235ec66cfb32249a (diff)
downloadperlweeklychallenge-club-672769b6bfa365e497069e98347d69bb65666f4b.tar.gz
perlweeklychallenge-club-672769b6bfa365e497069e98347d69bb65666f4b.tar.bz2
perlweeklychallenge-club-672769b6bfa365e497069e98347d69bb65666f4b.zip
pwc333 solution in go
-rw-r--r--challenge-333/pokgopun/go/ch-1.go96
-rw-r--r--challenge-333/pokgopun/go/ch-2.go91
2 files changed, 187 insertions, 0 deletions
diff --git a/challenge-333/pokgopun/go/ch-1.go b/challenge-333/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..9f169ec6b6
--- /dev/null
+++ b/challenge-333/pokgopun/go/ch-1.go
@@ -0,0 +1,96 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-333/
+/*#
+
+Task 1: Straight Line
+
+Submitted by: [43]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a list of co-ordinates.
+
+ Write a script to find out if the given points make a straight line.
+
+Example 1
+
+Input: @list = ([2, 1], [2, 3], [2, 5])
+Output: true
+
+Example 2
+
+Input: @list = ([1, 4], [3, 4], [10, 4])
+Output: true
+
+Example 3
+
+Input: @list = ([0, 0], [1, 1], [2, 3])
+Output: false
+
+Example 4
+
+Input: @list = ([1, 1], [1, 1], [1, 1])
+Output: true
+
+Example 5
+
+Input: @list = ([1000000, 1000000], [2000000, 2000000], [3000000, 3000000])
+Output: true
+
+Task 2: Duplicate Zeros
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "github.com/google/go-cmp/cmp"
+ "io"
+ "os"
+)
+
+type Point struct {
+ x, y int
+}
+
+func (pnt Point) Slope(p Point) Slope {
+ return Slope{pnt.y - p.y, pnt.x - p.x}
+}
+
+type Slope struct {
+ y, x int
+}
+
+func (slp Slope) IsEqual(s Slope) bool {
+ return slp.x*s.y == slp.y*s.x
+}
+
+type Input []Point
+
+func (in Input) process() bool {
+ i := len(in) - 1
+ if i < 2 {
+ return true
+ }
+ s0 := in[0].Slope(in[1])
+ for i > 1 {
+ if !s0.IsEqual(in[0].Slope(in[i])){
+ return false
+ }
+ i--
+ }
+ return true
+}
+
+func main() {
+ for _, data := range []struct {
+ input Input
+ output bool
+ }{
+ {Input{Point{2, 1}, Point{2, 3}, Point{2, 5}}, true},
+ {Input{Point{1, 4}, Point{3, 4}, Point{10, 4}}, true},
+ {Input{Point{0, 0}, Point{1, 1}, Point{2, 3}}, false},
+ {Input{Point{1, 1}, Point{1, 1}, Point{1, 1}}, true},
+ {Input{Point{1000000, 1000000}, Point{2000000, 2000000}, Point{3000000, 3000000}}, true},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(data.input.process(), data.output)) // blank if ok, otherwise show the difference
+ }
+}
diff --git a/challenge-333/pokgopun/go/ch-2.go b/challenge-333/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..47961bfa7c
--- /dev/null
+++ b/challenge-333/pokgopun/go/ch-2.go
@@ -0,0 +1,91 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-333/
+/*#
+
+Task 2: Duplicate Zeros
+
+Submitted by: [44]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given an array of integers.
+
+ Write a script to duplicate each occurrence of zero, shifting the
+ remaining elements to the right. The elements beyond the length of the
+ original array are not written.
+
+Example 1
+
+Input: @ints = (1, 0, 2, 3, 0, 4, 5, 0)
+Output: (1, 0, 0, 2, 3, 0, 0, 4)
+
+Each zero is duplicated.
+Elements beyond the original length (like 5 and last 0) are discarded.
+
+Example 2
+
+Input: @ints = (1, 2, 3)
+Output: (1, 2, 3)
+
+No zeros exist, so the array remains unchanged.
+
+Example 3
+
+Input: @ints = (1, 2, 3, 0)
+Output: (1, 2, 3, 0)
+
+Example 4
+
+Input: @ints = (0, 0, 1, 2)
+Output: (0, 0, 0, 0)
+
+Example 5
+
+Input: @ints = (1, 2, 0, 3, 4)
+Output: (1, 2, 0, 0, 3)
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 10th August
+ 2025.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "github.com/google/go-cmp/cmp"
+ "io"
+ "os"
+ "slices"
+)
+
+type ints []int
+
+func (in ints) dz() ints {
+ l := len(in) - 1
+ i := 0
+ for i < l {
+ if in[i] == 0 {
+ in = slices.Insert(in, i, 0)
+ i++
+ }
+ i++
+ }
+ return in[:l+1]
+}
+
+func main() {
+ for _, data := range []struct {
+ input, output ints
+ }{
+ {ints{1, 0, 2, 3, 0, 4, 5, 0}, ints{1, 0, 0, 2, 3, 0, 0, 4}},
+ {ints{1, 2, 3}, ints{1, 2, 3}},
+ {ints{1, 2, 3, 0}, ints{1, 2, 3, 0}},
+ {ints{0, 0, 1, 2}, ints{0, 0, 0, 0}},
+ {ints{1, 2, 0, 3, 4}, ints{1, 2, 0, 0, 3}},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(data.input.dz(), data.output)) // blank if ok, otherwise show the differeice
+
+ }
+}