aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Manring <michael@manring>2024-07-22 14:39:34 +1000
committerMichael Manring <michael@manring>2024-07-22 14:39:34 +1000
commitd16417257e7f2381b0970c2d27ac2086c0ace1e9 (patch)
treeda970d205797c2da5a5fbc8dfa44694302341ed5
parentdd7263a3d97abb4546bebec943af4f33a8427746 (diff)
downloadperlweeklychallenge-club-d16417257e7f2381b0970c2d27ac2086c0ace1e9.tar.gz
perlweeklychallenge-club-d16417257e7f2381b0970c2d27ac2086c0ace1e9.tar.bz2
perlweeklychallenge-club-d16417257e7f2381b0970c2d27ac2086c0ace1e9.zip
pwc279 solution in go
-rw-r--r--challenge-279/pokgopun/go/ch-1.go64
-rw-r--r--challenge-279/pokgopun/go/ch-2.go106
2 files changed, 170 insertions, 0 deletions
diff --git a/challenge-279/pokgopun/go/ch-1.go b/challenge-279/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..1417aaec23
--- /dev/null
+++ b/challenge-279/pokgopun/go/ch-1.go
@@ -0,0 +1,64 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-279/
+/*#
+
+Task 1: Sort Letters
+
+Submitted by: [45]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given two arrays, @letters and @weights.
+
+ Write a script to sort the given array @letters based on the @weights.
+
+Example 1
+
+Input: @letters = ('R', 'E', 'P', 'L')
+ @weights = (3, 2, 1, 4)
+Output: PERL
+
+Example 2
+
+Input: @letters = ('A', 'U', 'R', 'K')
+ @weights = (2, 4, 1, 3)
+Output: RAKU
+
+Example 3
+
+Input: @letters = ('O', 'H', 'Y', 'N', 'P', 'T')
+ @weights = (5, 4, 2, 6, 1, 3)
+Output: PYTHON
+
+Task 2: Split String
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+func sortLetters(letters []rune, weights []int) string {
+ r := make([]rune, len(letters))
+ for i, v := range letters {
+ r[weights[i]-1] = v
+ }
+ return string(r)
+}
+
+func main() {
+ for _, data := range []struct {
+ letters []rune
+ weights []int
+ output string
+ }{
+ {[]rune{'R', 'E', 'P', 'L'}, []int{3, 2, 1, 4}, "PERL"},
+ {[]rune{'A', 'U', 'R', 'K'}, []int{2, 4, 1, 3}, "RAKU"},
+ {[]rune{'O', 'H', 'Y', 'N', 'P', 'T'}, []int{5, 4, 2, 6, 1, 3}, "PYTHON"},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(sortLetters(data.letters, data.weights), data.output)) // blank if ok otherwise show the difference
+ }
+}
diff --git a/challenge-279/pokgopun/go/ch-2.go b/challenge-279/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..852579e91a
--- /dev/null
+++ b/challenge-279/pokgopun/go/ch-2.go
@@ -0,0 +1,106 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-279/
+/*#
+
+Task 2: Split String
+
+Submitted by: [46]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a string, $str.
+
+ Write a script to split the given string into two containing exactly
+ same number of vowels and return true if you can otherwise false.
+
+Example 1
+
+Input: $str = "perl"
+Ouput: false
+
+Example 2
+
+Input: $str = "book"
+Ouput: true
+
+Two possible strings "bo" and "ok" containing exactly one vowel each.
+
+Example 3
+
+Input: $str = "good morning"
+Ouput: true
+
+Two possible strings "good " and "morning" containing two vowels each or "good m
+" and "orning" containing two vowels each.
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 28th July 2024.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+type text string
+
+func (tx text) vwlIdx() []int {
+ var r []int
+ for i, v := range tx {
+ switch v {
+ case 'a', 'e', 'i', 'o', 'u':
+ r = append(r, i)
+ }
+ }
+ return r
+}
+
+func splitString0(str text) bool {
+ l := len(str.vwlIdx())
+ if l > 0 {
+ return l%2 == 0
+ }
+ return false
+}
+
+type splitted struct {
+ First, Second text
+ Ok bool
+}
+
+func splitString1(str text) splitted {
+ idx := str.vwlIdx()
+ l := len(idx)
+ h := l / 2
+ b := l%2 == 0
+ if b {
+ h -= 1
+ }
+ var i int
+ if l > 1 {
+ i = idx[h+1]
+ } else {
+ i = idx[h] + 1
+ }
+ return splitted{str[:i], str[i:], b}
+}
+
+func main() {
+ for _, data := range []struct {
+ str text
+ output splitted
+ }{
+ {"perl", splitted{"pe", "rl", false}},
+ {"book", splitted{"bo", "ok", true}},
+ {"good morning", splitted{"good m", "orning", true}},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(splitString0(data.str), data.output.Ok)) // blank if ok, otherwise show the difference
+ io.WriteString(os.Stdout, cmp.Diff(splitString1(data.str), data.output)) // blank if ok, otherwise show the difference
+ }
+}