aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPok <pok@goyangi>2025-05-24 06:22:45 +1000
committerPok <pok@goyangi>2025-05-24 06:22:45 +1000
commit0b428d5eee57d94d4c4681b8d972e37a34baff31 (patch)
tree05fb3867fcc3801350d37425b59d7780cf45554d
parent9b545863337dfe4469644e1a201176d976343fef (diff)
downloadperlweeklychallenge-club-0b428d5eee57d94d4c4681b8d972e37a34baff31.tar.gz
perlweeklychallenge-club-0b428d5eee57d94d4c4681b8d972e37a34baff31.tar.bz2
perlweeklychallenge-club-0b428d5eee57d94d4c4681b8d972e37a34baff31.zip
pwc322 solution in go
-rw-r--r--challenge-322/pokgopun/go/ch-1.go87
-rw-r--r--challenge-322/pokgopun/go/ch-2.go90
2 files changed, 177 insertions, 0 deletions
diff --git a/challenge-322/pokgopun/go/ch-1.go b/challenge-322/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..00f943fed2
--- /dev/null
+++ b/challenge-322/pokgopun/go/ch-1.go
@@ -0,0 +1,87 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-322/
+/*#
+
+Task 1: String Format
+
+Submitted by: [45]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a string and a positive integer.
+
+ Write a script to format the string, removing any dashes, in groups of
+ size given by the integer. The first group can be smaller than the
+ integer but should have at least one character. Groups should be
+ separated by dashes.
+
+Example 1
+
+Input: $str = "ABC-D-E-F", $i = 3
+Output: "ABC-DEF"
+
+Example 2
+
+Input: $str = "A-BC-D-E", $i = 2
+Output: "A-BC-DE"
+
+Example 3
+
+Input: $str = "-A-B-CD-E", $i = 4
+Output: "A-BCDE"
+
+Task 2: Rank Array
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+ "slices"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+type input struct {
+ str string
+ i int
+}
+
+func (in input) process() string {
+ var (
+ b byte
+ bs []byte
+ )
+ sep := byte('-')
+ i := len(in.str)
+ l := 0
+ for i > 0 {
+ i--
+ b = in.str[i]
+ if b == sep {
+ continue
+ }
+ if l < in.i {
+ bs = append(bs, b)
+ l++
+ } else {
+ bs = append(bs, sep, b)
+ l = 1
+ }
+ }
+ slices.Reverse(bs)
+ return string(bs)
+}
+
+func main() {
+ for _, data := range []struct {
+ input input
+ output string
+ }{
+ {input{"ABC-D-E-F", 3}, "ABC-DEF"},
+ {input{"A-BC-D-E", 2}, "A-BC-DE"},
+ {input{"-A-B-CD-E", 4}, "A-BCDE"},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(data.input.process(), data.output)) // blank if ok, otherwise show the difference
+ }
+}
diff --git a/challenge-322/pokgopun/go/ch-2.go b/challenge-322/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..4501775dd7
--- /dev/null
+++ b/challenge-322/pokgopun/go/ch-2.go
@@ -0,0 +1,90 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-322/
+/*#
+
+Task 2: Rank Array
+
+Submitted by: [46]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given an array of integers.
+
+ Write a script to return an array of the ranks of each element: the
+ lowest value has rank 1, next lowest rank 2, etc. If two elements are
+ the same then they share the same rank.
+
+Example 1
+
+Input: @ints = (55, 22, 44, 33)
+Output: (4, 1, 3, 2)
+
+Example 2
+
+Input: @ints = (10, 10, 10)
+Output: (1, 1, 1)
+
+Example 3
+
+Input: @ints = (5, 1, 1, 4, 3)
+Output: (4, 1, 1, 3, 2)
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 25th May 2025.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+ "sort"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+type intPair struct {
+ a, b int
+}
+
+type ints []int
+
+func (in ints) process() ints {
+ var ps []intPair
+ for i, v := range in {
+ ps = append(ps, intPair{v, i})
+ }
+ sort.Slice(ps, func(i, j int) bool {
+ return ps[i].a < ps[j].a
+ })
+ r := 1
+ ps2 := []intPair{intPair{ps[0].b, r}}
+ for i := range len(ps) - 1 {
+ if ps[i+1].a > ps[i].a {
+ r++
+ }
+ ps2 = append(ps2, intPair{ps[i+1].b, r})
+ }
+ sort.Slice(ps2, func(i, j int) bool {
+ return ps2[i].a < ps2[j].a
+ })
+ var c ints
+ for _, v := range ps2 {
+ c = append(c, v.b)
+ }
+ return c
+}
+
+func main() {
+ for _, data := range []struct {
+ input, output ints
+ }{
+ {ints{55, 22, 44, 33}, ints{4, 1, 3, 2}},
+ {ints{10, 10, 10}, ints{1, 1, 1}},
+ {ints{5, 1, 1, 4, 3}, ints{4, 1, 1, 3, 2}},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(data.input.process(), data.output)) //blank if ok, otherwise show the difference
+ }
+}