aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Firkins <michael@firkins>2023-10-10 13:45:39 +1100
committerMichael Firkins <michael@firkins>2023-10-10 13:45:39 +1100
commit962c2aa4bebf9d52f59e1384e804fc620afa7cd6 (patch)
tree7c2788cbef89fa73c87cbd997430a4412fac8d3a
parentde215720df537a15de474f0125f2994651be09c5 (diff)
downloadperlweeklychallenge-club-962c2aa4bebf9d52f59e1384e804fc620afa7cd6.tar.gz
perlweeklychallenge-club-962c2aa4bebf9d52f59e1384e804fc620afa7cd6.tar.bz2
perlweeklychallenge-club-962c2aa4bebf9d52f59e1384e804fc620afa7cd6.zip
pwc238 solution in go
-rw-r--r--challenge-238/pokgopun/go/ch-1.go59
-rw-r--r--challenge-238/pokgopun/go/ch-2.go82
2 files changed, 141 insertions, 0 deletions
diff --git a/challenge-238/pokgopun/go/ch-1.go b/challenge-238/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..e429633daf
--- /dev/null
+++ b/challenge-238/pokgopun/go/ch-1.go
@@ -0,0 +1,59 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-238/
+/*#
+
+Task 1: Running Sum
+
+Submitted by: [43]Mohammad S Anwar
+ __________________________________________________________________
+
+ You are given an array of integers.
+
+ Write a script to return the running sum of the given array. The
+ running sum can be calculated as sum[i] = num[0] + num[1] + …. +
+ num[i].
+
+Example 1
+
+Input: @int = (1, 2, 3, 4, 5)
+Output: (1, 3, 6, 10, 15)
+
+Example 2
+
+Input: @int = (1, 1, 1, 1, 1)
+Output: (1, 2, 3, 4, 5)
+
+Example 3
+
+Input: @int = (0, -1, 1, 2)
+Output: (0, -1, 0, 2)
+
+Task 2: Persistence Sort
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "fmt"
+ "slices"
+)
+
+func main() {
+ for _, data := range []struct {
+ input, output []int
+ }{
+ {[]int{1, 2, 3, 4, 5}, []int{1, 3, 6, 10, 15}},
+ {[]int{1, 1, 1, 1, 1}, []int{1, 2, 3, 4, 5}},
+ {[]int{0, -1, 1, 2}, []int{0, -1, 0, 2}},
+ } {
+ fmt.Println(slices.Equal(runningSum(data.input), data.output))
+ }
+}
+
+func runningSum(s []int) []int {
+ l := len(s)
+ for i := 1; i < l; i++ {
+ s[i] += s[i-1]
+ }
+ return s
+}
diff --git a/challenge-238/pokgopun/go/ch-2.go b/challenge-238/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..b0cff2d66e
--- /dev/null
+++ b/challenge-238/pokgopun/go/ch-2.go
@@ -0,0 +1,82 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-238/
+/*#
+
+Task 2: Persistence Sort
+
+Submitted by: [44]Mohammad S Anwar
+ __________________________________________________________________
+
+ You are given an array of positive integers.
+
+ Write a script to sort the given array in increasing order with respect
+ to the count of steps required to obtain a single-digit number by
+ multiplying its digits recursively for each array element. If any two
+ numbers have the same count of steps, then print the smaller number
+ first.
+
+Example 1
+
+Input: @int = (15, 99, 1, 34)
+Output: (1, 15, 34, 99)
+
+15 => 1 x 5 => 5 (1 step)
+99 => 9 x 9 => 81 => 8 x 1 => 8 (2 steps)
+1 => 0 step
+34 => 3 x 4 => 12 => 1 x 2 => 2 (2 steps)
+
+Example 2
+
+Input: @int = (50, 25, 33, 22)
+Output: (22, 33, 50, 25)
+
+50 => 5 x 0 => 0 (1 step)
+25 => 2 x 5 => 10 => 1 x 0 => 0 (2 steps)
+33 => 3 x 3 => 9 (1 step)
+22 => 2 x 2 => 4 (1 step)
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 15th October
+ 2023.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "fmt"
+ "slices"
+ "sort"
+ "strconv"
+)
+
+func main() {
+ for _, data := range []struct {
+ input, output []int
+ }{
+ {[]int{15, 99, 1, 34}, []int{1, 15, 34, 99}},
+ {[]int{50, 25, 33, 22}, []int{22, 33, 50, 25}},
+ } {
+ sort.SliceStable(data.input, func(i, j int) bool {
+ return true
+ })
+ sort.SliceStable(data.input, func(i, j int) bool {
+ return mk1dgt(data.input[i]) < mk1dgt(data.input[j])
+ })
+ fmt.Println(slices.Equal(data.input, data.output))
+ }
+}
+
+func mk1dgt(n int) (i int) {
+ for n > 9 {
+ i++
+ r := 1
+ for _, v := range strconv.Itoa(n) {
+ r *= int(v) - 48
+ }
+ n = r
+ }
+ return i
+}