aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Manring <michael@manring>2024-06-24 21:10:19 +1000
committerMichael Manring <michael@manring>2024-06-24 21:23:12 +1000
commite47302d99328b696544a2fcf1457f86aa5b3c5ab (patch)
treebc2d63e52bec0d3037a5f55ed7e92891f38107e9
parent5129e0b9516902c4b1ed68e1061888d2d799dfc6 (diff)
downloadperlweeklychallenge-club-e47302d99328b696544a2fcf1457f86aa5b3c5ab.tar.gz
perlweeklychallenge-club-e47302d99328b696544a2fcf1457f86aa5b3c5ab.tar.bz2
perlweeklychallenge-club-e47302d99328b696544a2fcf1457f86aa5b3c5ab.zip
pwc275 solution in go
-rw-r--r--challenge-275/pokgopun/go/ch-1.go84
-rw-r--r--challenge-275/pokgopun/go/ch-2.go88
2 files changed, 172 insertions, 0 deletions
diff --git a/challenge-275/pokgopun/go/ch-1.go b/challenge-275/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..c9524faa19
--- /dev/null
+++ b/challenge-275/pokgopun/go/ch-1.go
@@ -0,0 +1,84 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-275/
+/*#
+
+Task 1: Broken Keys
+
+Submitted by: [45]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a sentence, $sentence and list of broken keys @keys.
+
+ Write a script to find out how many words can be typed fully.
+
+Example 1
+
+Input: $sentence = "Perl Weekly Challenge", @keys = ('l', 'a')
+Output: 0
+
+Example 2
+
+Input: $sentence = "Perl and Raku", @keys = ('a')
+Output: 1
+
+Only Perl since the other word two words contain 'a' and can't be typed fully.
+
+Example 3
+
+Input: $sentence = "Well done Team PWC", @keys = ('l', 'o')
+Output: 2
+
+Example 4
+
+Input: $sentence = "The joys of polyglottism", @keys = ('T')
+Output: 2
+
+Task 2: Replace Digits
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+ "strings"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+type chars []rune
+
+func bk(st string, cs chars) int {
+ words := strings.Split(st, " ")
+ l := len(words)
+ n := l
+ var c rune
+ for l > 0 {
+ l--
+ for _, c = range cs {
+ if c >= 'A' && c <= 'Z' {
+ c += 32
+ }
+ if strings.ContainsRune(strings.ToLower(words[l]), c) {
+ n--
+ break
+ }
+ }
+ }
+ return n
+}
+
+func main() {
+ for _, data := range []struct {
+ sentence string
+ chars chars
+ count int
+ }{
+ {"Perl Weekly Challenge", chars{'l', 'a'}, 0},
+ {"Perl and Raku", chars{'a'}, 1},
+ {"Well done Team PWC", chars{'l', 'o'}, 2},
+ {"The joys of polyglottism", chars{'T'}, 2},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(bk(data.sentence, data.chars), data.count)) // blank if ok, otherwise show the differences
+ }
+}
diff --git a/challenge-275/pokgopun/go/ch-2.go b/challenge-275/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..c8b76fbf44
--- /dev/null
+++ b/challenge-275/pokgopun/go/ch-2.go
@@ -0,0 +1,88 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-275/
+/*#
+
+Task 2: Replace Digits
+
+Submitted by: [46]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given an alphanumeric string, $str, where each character is
+ either a letter or a digit.
+
+ Write a script to replace each digit in the given string with the value
+ of the previous letter plus (digit) places.
+
+Example 1
+
+Input: $str = 'a1c1e1'
+Ouput: 'abcdef'
+
+shift('a', 1) => 'b'
+shift('c', 1) => 'd'
+shift('e', 1) => 'f'
+
+Example 2
+
+Input: $str = 'a1b2c3d4'
+Output: 'abbdcfdh'
+
+shift('a', 1) => 'b'
+shift('b', 2) => 'd'
+shift('c', 3) => 'f'
+shift('d', 4) => 'h'
+
+Example 3
+
+Input: $str = 'b2b'
+Output: 'bdb'
+
+Example 4
+
+Input: $str = 'a16z'
+Output: 'abgz'
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 30th June 2024.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+func replaceDigits(str string) string {
+ bs := []byte(str)
+ var (
+ b byte
+ i int
+ )
+ for i = range len(bs) {
+ if bs[i] >= 48 && bs[i] <= 57 {
+ bs[i] = b + bs[i] - 48
+ } else {
+ b = bs[i]
+ }
+ }
+ return string(bs)
+}
+
+func main() {
+ for _, data := range []struct {
+ input, output string
+ }{
+ {"a1c1e1", "abcdef"},
+ {"a1b2c3d4", "abbdcfdh"},
+ {"b2b", "bdb"},
+ {"a16z", "abgz"},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(replaceDigits(data.input), data.output)) // blank if ok, otherwise show the differences
+ }
+}