diff options
| author | Pok <pok@goyangi> | 2025-10-20 18:10:55 +0700 |
|---|---|---|
| committer | Pok <pok@goyangi> | 2025-10-20 18:10:55 +0700 |
| commit | bedb01372c255ebec06ae07cbee65e371ee16152 (patch) | |
| tree | ffcf1cbbe42138820acb296ba09ce9fcb8ef1b83 | |
| parent | b85eba663bb4d903d2b48d209bd56411cb3786a0 (diff) | |
| download | perlweeklychallenge-club-bedb01372c255ebec06ae07cbee65e371ee16152.tar.gz perlweeklychallenge-club-bedb01372c255ebec06ae07cbee65e371ee16152.tar.bz2 perlweeklychallenge-club-bedb01372c255ebec06ae07cbee65e371ee16152.zip | |
pwc344 solution in go
| -rw-r--r-- | challenge-344/pokgopun/go/ch-1.go | 95 | ||||
| -rw-r--r-- | challenge-344/pokgopun/go/ch-2.go | 153 |
2 files changed, 248 insertions, 0 deletions
diff --git a/challenge-344/pokgopun/go/ch-1.go b/challenge-344/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..ab6a470f5c --- /dev/null +++ b/challenge-344/pokgopun/go/ch-1.go @@ -0,0 +1,95 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-344/ +/*# + +Task 1: Array Form Compute + +Submitted by: [43]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an array of integers, @ints and an integer, $x. + + Write a script to add $x to the integer in the array-form. + + The array form of an integer is a digit-by-digit representation + stored as an array, where the most significant digit is at the 0th + index. + +Example 1 + +Input: @ints = (1, 2, 3, 4), $x = 12 +Output: (1, 2, 4, 6) + +Example 2 + +Input: @ints = (2, 7, 4), $x = 181 +Output: (4, 5, 5) + +Example 3 + +Input: @ints = (9, 9, 9), $x = 1 +Output: (1, 0, 0, 0) + +Example 4 + +Input: @ints = (1, 0, 0, 0, 0), $x = 9999 +Output: (1, 9, 9, 9, 9) + +Example 5 + +Input: @ints = (0), $x = 1000 +Output: (1, 0, 0, 0) + +Task 2: Array Formation +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + "slices" + + "github.com/google/go-cmp/cmp" +) + +type ints []int + +func (in ints) int() int { + l := len(in) + n := 0 + t := 1 + for l > 0 { + l-- + n += in[l] * t + t *= 10 + } + return n +} + +func (in ints) process(n int) ints { + num := in.int() + n + var nums ints + for num > 0 { + nums = append(nums, num%10) + num /= 10 + } + slices.Reverse(nums) + return nums +} + +func main() { + for _, data := range []struct { + ints ints + int int + output ints + }{ + {ints{1, 2, 3, 4}, 12, ints{1, 2, 4, 6}}, + {ints{2, 7, 4}, 181, ints{4, 5, 5}}, + {ints{9, 9, 9}, 1, ints{1, 0, 0, 0}}, + {ints{1, 0, 0, 0, 0}, 9999, ints{1, 9, 9, 9, 9}}, + {ints{0}, 1000, ints{1, 0, 0, 0}}, + } { + io.WriteString(os.Stdout, cmp.Diff(data.ints.process(data.int), data.output)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-344/pokgopun/go/ch-2.go b/challenge-344/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..b1044e41e1 --- /dev/null +++ b/challenge-344/pokgopun/go/ch-2.go @@ -0,0 +1,153 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-344/ +/*# + +Task 2: Array Formation + +Submitted by: [44]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given two list: @source and @target. + + Write a script to see if you can build the exact @target by putting + these smaller lists from @source together in some order. You cannot + break apart or change the order inside any of the smaller lists in + @source. + +Example 1 + +Input: @source = ([2,3], [1], [4]) + @target = (1, 2, 3, 4) +Output: true + +Use in the order: [1], [2,3], [4] + +Example 2 + +Input: @source = ([1,3], [2,4]) + @target = (1, 2, 3, 4) +Output: false + +Example 3 + +Input: @source = ([9,1], [5,8], [2]) + @target = (5, 8, 2, 9, 1) +Output: true + +Use in the order: [5,8], [2], [9,1] + +Example 4 + +Input: @source = ([1], [3]) + @target = (1, 2, 3) +Output: false + +Missing number: 2 + +Example 5 + +Input: @source = ([7,4,6]) + @target = (7, 4, 6) +Output: true + +Use in the order: [7, 4, 6] + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 26th October + 2025. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "iter" + "os" + "slices" + + "github.com/google/go-cmp/cmp" +) + +func permutations[E any](s []E, r int) iter.Seq[[]E] { + return func(yield func([]E) bool) { + n := len(s) + if r == 0 || n == 0 || r > n { + return + } + if !yield(s[:r]) { + return + } + res := make([]E, r) + idx := make([]int, n) + for v := range n { + idx[v] = v + } + cyc := append(idx[n-r+1:], n) + slices.Reverse(cyc) + for { + i := r + for i > 0 { + i-- + cyc[i]-- + if cyc[i] == 0 { + copy(idx[i:], append(idx[i+1:], idx[i])) + cyc[i] = n - i + } else { + j := cyc[i] + idx[i], idx[n-j] = idx[n-j], idx[i] + for i, v := range idx[:r] { + res[i] = s[v] + } + if !yield(res) { + return + } + break + } + if i > 0 { + continue + } + return + } + } + } +} + +type ints []int + +func af(source []ints, target ints) bool { + l := len(target) + for pmt := range permutations(source, len(source)) { + t := make(ints, l) + i := 0 + for _, p := range pmt { + i += copy(t[i:], p) + } + if i != l { + return false + } + if cmp.Diff(target, t) == "" { + return true + } + } + return false +} + +func main() { + for _, data := range []struct { + source []ints + target ints + output bool + }{ + {[]ints{ints{2, 3}, ints{1}, ints{4}}, ints{1, 2, 3, 4}, true}, + {[]ints{ints{1, 3}, ints{2, 4}}, ints{1, 2, 3, 4}, false}, + {[]ints{ints{9, 1}, ints{5, 8}, ints{2}}, ints{5, 8, 2, 9, 1}, true}, + {[]ints{ints{1}, ints{3}}, ints{1, 2, 3}, false}, + {[]ints{ints{7, 4, 6}}, ints{7, 4, 6}, true}, + } { + //fmt.Println(data.source, data.target, data.output) + io.WriteString(os.Stdout, cmp.Diff(af(data.source, data.target), data.output)) // blank if ok, otherwise show the difference + } +} |
