aboutsummaryrefslogtreecommitdiff
path: root/challenge-256
diff options
context:
space:
mode:
authorMichael Manring <michael@manring>2024-02-13 01:10:39 +1100
committerMichael Manring <michael@manring>2024-02-13 03:29:38 +1100
commit2f29832746cd89b07b6fee887676fa2bc7a578d6 (patch)
tree652c77c5e9c848d931d29ea30620e131a48264b9 /challenge-256
parentcec1c29e0cfc6891cd75225c7b45e375d51076b7 (diff)
downloadperlweeklychallenge-club-2f29832746cd89b07b6fee887676fa2bc7a578d6.tar.gz
perlweeklychallenge-club-2f29832746cd89b07b6fee887676fa2bc7a578d6.tar.bz2
perlweeklychallenge-club-2f29832746cd89b07b6fee887676fa2bc7a578d6.zip
pwc256 solution in go
Diffstat (limited to 'challenge-256')
-rw-r--r--challenge-256/pokgopun/go/ch-1.go80
-rw-r--r--challenge-256/pokgopun/go/ch-2.go80
2 files changed, 160 insertions, 0 deletions
diff --git a/challenge-256/pokgopun/go/ch-1.go b/challenge-256/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..b52df3379b
--- /dev/null
+++ b/challenge-256/pokgopun/go/ch-1.go
@@ -0,0 +1,80 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-256/
+/*#
+
+Task 1: Maximum Pairs
+
+Submitted by: [41]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given an array of distinct words, @words.
+
+ Write a script to find the maximum pairs in the given array. The words
+ $words[i] and $words[j] can be a pair one is reverse of the other.
+
+Example 1
+
+Input: @words = ("ab", "de", "ed", "bc")
+Output: 1
+
+There is one pair in the given array: "de" and "ed"
+
+Example 2
+
+Input: @words = ("aa", "ba", "cd", "ed")
+Output: 0
+
+Example 3
+
+Input: @words = ("uv", "qp", "st", "vu", "mn", "pq"))
+Output: 2
+
+Task 2: Merge Strings
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+ "slices"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+type str string
+
+func (s str) reverse() str {
+ r := []rune(s)
+ slices.Reverse(r)
+ return str(r)
+}
+
+type strs []str
+
+func (st strs) maxPair() int {
+ l := len(st)
+ var c, i int
+ for i < l-1 {
+ for _, v := range st[i+1:] {
+ if st[i] == v.reverse() {
+ c++
+ }
+ }
+ i++
+ }
+ return c
+}
+
+func main() {
+ for _, data := range []struct {
+ input strs
+ output int
+ }{
+ {strs{"ab", "de", "ed", "bc"}, 1},
+ {strs{"aa", "ba", "cd", "ed"}, 0},
+ {strs{"uv", "qp", "st", "vu", "mn", "pq"}, 2},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(data.input.maxPair(), data.output)) // blank if ok, otherwise show the difference
+ }
+}
diff --git a/challenge-256/pokgopun/go/ch-2.go b/challenge-256/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..eb931d9d7e
--- /dev/null
+++ b/challenge-256/pokgopun/go/ch-2.go
@@ -0,0 +1,80 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-256/
+/*#
+
+Task 2: Merge Strings
+
+Submitted by: [42]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given two strings, $str1 and $str2.
+
+ Write a script to merge the given strings by adding in alternative
+ order starting with the first string. If a string is longer than the
+ other then append the remaining at the end.
+
+Example 1
+
+Input: $str1 = "abcd", $str2 = "1234"
+Output: "a1b2c3d4"
+
+Example 2
+
+Input: $str1 = "abc", $str2 = "12345"
+Output: "a1b2c345"
+
+Example 3
+
+Input: $str1 = "abcde", $str2 = "123"
+Output: "a1b2c3de"
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 18th February
+ 2024.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+ "strings"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+type strPair struct {
+ str1, str2 string
+}
+
+func (sp strPair) merge() string {
+ l1 := len(sp.str1)
+ l2 := len(sp.str2)
+ l := max(l1, l2)
+ var b strings.Builder
+ for i := 0; i < l; i++ {
+ if i < l1 {
+ b.WriteByte(sp.str1[i])
+ }
+ if i < l2 {
+ b.WriteByte(sp.str2[i])
+ }
+ }
+ return b.String()
+}
+
+func main() {
+ for _, data := range []struct {
+ input strPair
+ output string
+ }{
+ {strPair{"abcd", "1234"}, "a1b2c3d4"},
+ {strPair{"abc", "12345"}, "a1b2c345"},
+ {strPair{"abcde", "123"}, "a1b2c3de"},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(data.input.merge(), data.output)) // blank if ok, otherwise show the difference
+ }
+}