aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Manring <michael@manring>2023-11-28 13:26:31 +1100
committerMichael Manring <michael@manring>2023-11-28 13:26:31 +1100
commit7afc1f8c9df147bf7e09970a11fd6600cbc4098f (patch)
tree5acf54df822cdad78be8ad863fe86bcdc77cf371
parent7a0976d301a1090cbf3a9ae302e45b9a612d29b3 (diff)
downloadperlweeklychallenge-club-7afc1f8c9df147bf7e09970a11fd6600cbc4098f.tar.gz
perlweeklychallenge-club-7afc1f8c9df147bf7e09970a11fd6600cbc4098f.tar.bz2
perlweeklychallenge-club-7afc1f8c9df147bf7e09970a11fd6600cbc4098f.zip
pwc245 solution in go
-rw-r--r--challenge-245/pokgopun/go/ch-1.go60
-rw-r--r--challenge-245/pokgopun/go/ch-2.go150
2 files changed, 210 insertions, 0 deletions
diff --git a/challenge-245/pokgopun/go/ch-1.go b/challenge-245/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..f4f1351345
--- /dev/null
+++ b/challenge-245/pokgopun/go/ch-1.go
@@ -0,0 +1,60 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-245/
+/*#
+
+Task 1: Sort Language
+
+Submitted by: [45]Mohammad S Anwar
+ __________________________________________________________________
+
+ You are given two array of languages and its popularity.
+
+ Write a script to sort the language based on popularity.
+
+Example 1
+
+Input: @lang = ('perl', 'c', 'python')
+ @popularity = (2, 1, 3)
+Output: ('c', 'perl', 'python')
+
+Example 2
+
+Input: @lang = ('c++', 'haskell', 'java')
+ @popularity = (1, 3, 2)
+Output: ('c++', 'java', 'haskell')
+
+Task 2: Largest of Three
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "cmp"
+ "fmt"
+ "reflect"
+ "slices"
+)
+
+type ranks []int
+
+type words []string
+
+func (w words) sort(r ranks) words {
+ slices.SortFunc(w, func(a, b string) int {
+ return cmp.Compare(r[slices.Index(w, a)], r[slices.Index(w, b)])
+ })
+ return w
+}
+
+func main() {
+ for _, data := range []struct {
+ input1 words
+ input2 ranks
+ output words
+ }{
+ {words{"perl", "c", "python"}, ranks{2, 1, 3}, words{"c", "perl", "python"}},
+ {words{"c++", "haskell", "java"}, ranks{1, 3, 2}, words{"c++", "java", "haskell"}},
+ } {
+ fmt.Println(reflect.DeepEqual(data.input1.sort(data.input2), data.output))
+ }
+}
diff --git a/challenge-245/pokgopun/go/ch-2.go b/challenge-245/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..74527e790b
--- /dev/null
+++ b/challenge-245/pokgopun/go/ch-2.go
@@ -0,0 +1,150 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-245/
+/*#
+
+Task 2: Largest of Three
+
+Submitted by: [46]Mohammad S Anwar
+ __________________________________________________________________
+
+ You are given an array of integers >= 0.
+
+ Write a script to return the largest number formed by concatenating
+ some of the given integers in any order which is also multiple of 3.
+ Return -1 if none found.
+
+Example 1
+
+Input: @digits = (8, 1, 9)
+Output: 981
+
+981 % 3 == 0
+
+Example 2
+
+Input: @digits = (8, 6, 7, 1, 0)
+Output: 8760
+
+Example 3
+
+Input: @digits = (1)
+Output: -1
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 3rd December
+ 2023.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "fmt"
+ "reflect"
+ "slices"
+)
+
+func main() {
+ for _, data := range []struct {
+ input digits
+ output int
+ }{
+ {digits{8, 1, 9}, 981},
+ {digits{8, 6, 7, 1, 0}, 8760},
+ {digits{1}, -1},
+ } {
+ fmt.Println(reflect.DeepEqual(data.output, data.input.lot()))
+ }
+}
+
+type digits []int
+
+func (d digits) concat() int {
+ r := d[0]
+ var n, t int
+ for _, v := range d[1:] {
+ n, t = v, 10
+ for n > 10 {
+ t *= 10
+ n /= 10
+ }
+ r = r*t + v
+ }
+ return r
+}
+
+func (d digits) lot() int {
+ slices.Sort(d)
+ slices.Reverse(d)
+ l := len(d)
+ for i := l; i > 0; i-- {
+ res, closer := d.permute(i)
+ for v := range res {
+ r := v.concat()
+ if r%3 == 0 {
+ closer()
+ return r
+ }
+ }
+ }
+ return -1
+}
+
+// transcribed from https://docs.python.org/3/library/itertools.html#itertools.permutations
+func (d digits) permute(r int) (res chan digits, closer func()) {
+ res = make(chan digits)
+ done := make(chan struct{})
+ n := len(d)
+ idx := make([]int, n)
+ for i := range idx {
+ idx[i] = i
+ }
+ cyc := make([]int, r)
+ for i := range cyc {
+ cyc[i] = n - i
+ }
+ go func() {
+ r0 := make(digits, r)
+ copy(r0, d[:r])
+ select {
+ case <-done:
+ close(res)
+ return
+ case res <- r0:
+ }
+ for {
+ for i := r - 1; i >= 0; i-- {
+ cyc[i]--
+ if cyc[i] == 0 {
+ idx = append(idx, idx[i])
+ idx = slices.Delete(idx, i, i+1)
+ cyc[i] = n - i
+ } else {
+ j := cyc[i]
+ idx[i], idx[n-j] = idx[n-j], idx[i]
+ rn := make(digits, r)
+ for i, v := range idx[:r] {
+ rn[i] = d[v]
+ }
+ select {
+ case <-done:
+ close(res)
+ return
+ case res <- rn:
+ }
+ break
+ }
+ if i == 0 {
+ close(res)
+ return
+ }
+ }
+ }
+ close(res)
+ }()
+ return res, func() {
+ close(done)
+ }
+}