aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-12-26 19:57:34 +0000
committerGitHub <noreply@github.com>2023-12-26 19:57:34 +0000
commitb06726e30c3f2d97a41eae201c0c21aa16203daf (patch)
treed9aadd388cbcd78edb5b9cab7b0cf042454f092e
parentbac6d6ade1e9bc33aeb4192fe6786a8024100694 (diff)
parentec34f3232c555c181a086cd46e0c439c84764588 (diff)
downloadperlweeklychallenge-club-b06726e30c3f2d97a41eae201c0c21aa16203daf.tar.gz
perlweeklychallenge-club-b06726e30c3f2d97a41eae201c0c21aa16203daf.tar.bz2
perlweeklychallenge-club-b06726e30c3f2d97a41eae201c0c21aa16203daf.zip
Merge pull request #9296 from pokgopun/pwc249
pwc249 solution in go
-rw-r--r--challenge-249/pokgopun/go/ch-1.go88
-rw-r--r--challenge-249/pokgopun/go/ch-2.go159
2 files changed, 247 insertions, 0 deletions
diff --git a/challenge-249/pokgopun/go/ch-1.go b/challenge-249/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..c6f505180e
--- /dev/null
+++ b/challenge-249/pokgopun/go/ch-1.go
@@ -0,0 +1,88 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-249/
+/*#
+
+Task 1: Shortest Distance
+
+Submitted by: [66]Mohammad S Anwar
+ __________________________________________________________________
+
+ You are given an array of integers with even number of elements.
+
+ Write a script to divide the given array into equal pairs such that:
+a) Each element belongs to exactly one pair.
+b) The elements present in a pair are equal.
+
+Example 1
+
+Input: @ints = (3, 2, 3, 2, 2, 2)
+Output: (2, 2), (3, 3), (2, 2)
+
+There are 6 elements in @ints.
+They should be divided into 6 / 2 = 3 pairs.
+@ints is divided into the pairs (2, 2), (3, 3), and (2, 2) satisfying all the co
+nditions.
+
+Example 2
+
+Input: @ints = (1, 2, 3, 4)
+Output: ()
+
+There is no way to divide @ints 2 pairs such that the pairs satisfy every condit
+ion.
+
+Task 2: DI String Match
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "cmp"
+ "fmt"
+ "slices"
+
+ gocmp "github.com/google/go-cmp/cmp"
+)
+
+type duo [2]int
+
+type duos []duo
+
+func (ds duos) sort() duos {
+ if len(ds) == 0 {
+ return nil
+ }
+ slices.SortFunc(ds, func(a, b duo) int {
+ return cmp.Compare(a[0], b[0])
+ })
+ return ds
+}
+
+type ints []int
+
+func (is ints) equalPairs() duos {
+ var ds duos
+ slices.Sort(is)
+ m := make(map[int]int)
+ for _, v := range is {
+ m[v]++
+ if m[v] == 2 {
+ ds = append(ds, duo{v, v})
+ m[v] = 0
+ }
+ }
+ //return ds.sort()
+ return ds
+}
+
+func main() {
+ for _, data := range []struct {
+ input ints
+ output duos
+ }{
+ {ints{3, 2, 3, 2, 2, 2}, duos{duo{2, 2}, duo{3, 3}, duo{2, 2}}},
+ {ints{1, 2, 3, 4}, duos{}},
+ } {
+ fmt.Println(gocmp.Diff(data.input.equalPairs(), data.output.sort())) //output nothing if ok, otherise show the difference
+ }
+}
diff --git a/challenge-249/pokgopun/go/ch-2.go b/challenge-249/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..b9e687a641
--- /dev/null
+++ b/challenge-249/pokgopun/go/ch-2.go
@@ -0,0 +1,159 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-249/
+/*#
+
+Task 2: DI String Match
+
+Submitted by: [67]Mohammad S Anwar
+ __________________________________________________________________
+
+ You are given a string s, consisting of only the characters "D" and
+ "I".
+
+ Find a permutation of the integers [0 .. length(s)] such that for each
+ character s[i] in the string:
+s[i] == 'I' ⇒ perm[i] < perm[i + 1]
+s[i] == 'D' ⇒ perm[i] > perm[i + 1]
+
+Example 1
+
+Input: $str = "IDID"
+Output: (0, 4, 1, 3, 2)
+
+Example 2
+
+Input: $str = "III"
+Output: (0, 1, 2, 3)
+
+Example 3
+
+Input: $str = "DDI"
+Output: (3, 2, 0, 1)
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 31st December
+ 2023.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "fmt"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+func main() {
+ for _, data := range []struct {
+ input DI
+ output permute
+ }{
+ {newDI("IDID"), permute{0, 4, 1, 3, 2}},
+ {newDI("III"), permute{0, 1, 2, 3}},
+ {newDI("DDI"), permute{3, 2, 0, 1}},
+ } {
+ fmt.Println(data.input.match(data.output))
+ }
+}
+
+type DI []bool
+
+func newDI(s string) DI {
+ di := make(DI, len(s))
+ for i, v := range s {
+ switch v {
+ case 'I':
+ di[i] = false
+ case 'D':
+ di[i] = true
+ default:
+ return DI{}
+ }
+ }
+ return di
+}
+
+func (di DI) match(p permute) bool {
+ l := len(di)
+ //fmt.Println("l=", l)
+ pmtt := newPermutation(byte(l + 1))
+ for pm := range pmtt.ch {
+ //fmt.Println("-----")
+ var (
+ i int
+ v bool
+ )
+ for _, v = range di {
+ if v {
+ if pm[i] > pm[i+1] {
+ //fmt.Println("D", pm[i], pm[i+1])
+ i++
+ continue
+ }
+ } else {
+ if pm[i] < pm[i+1] {
+ //fmt.Println("I", pm[i], pm[i+1])
+ i++
+ continue
+ }
+ }
+ break
+ }
+ //fmt.Println("i=", i)
+ if i == l {
+ //fmt.Println(pm, p, cmp.Diff(pm, p))
+ if cmp.Diff(pm, p) == "" {
+ close(pmtt.done)
+ return true
+ }
+ }
+ }
+ return false
+}
+
+type permute []byte
+
+type permutation struct {
+ ch chan permute
+ done chan struct{}
+}
+
+func newPermutation(n byte) permutation {
+ pmtt := permutation{make(chan permute), make(chan struct{})}
+ go func() {
+ s := pmString(n)
+ pmtt.output(s, "")
+ close(pmtt.ch)
+ }()
+ return pmtt
+}
+
+func (pmtt permutation) output(s, t string) {
+ select {
+ case <-pmtt.done:
+ default:
+ if len(s) > 0 {
+ for i, v := range []byte(s) {
+ pmtt.output(s[:i]+s[i+1:], t+string(v))
+ }
+ } else {
+ select {
+ case <-pmtt.done:
+ case pmtt.ch <- permute(t):
+ }
+ }
+ }
+}
+
+func pmString(n byte) string {
+ bs := make([]byte, n)
+ for n > 0 {
+ n--
+ bs[n] = n
+ }
+ //fmt.Println(bs)
+ return string(bs)
+}