diff options
| author | Pok <pok@goyangi> | 2025-09-29 13:25:48 +0700 |
|---|---|---|
| committer | Pok <pok@goyangi> | 2025-09-29 13:25:48 +0700 |
| commit | c6e63c2bc0b7f40515c82bf832058d9da016babe (patch) | |
| tree | 1f31805d226c961f72fd588e4d920318b654c8cd | |
| parent | aea2dde2ff05366db4b8560d08a4a80b028e9d66 (diff) | |
| download | perlweeklychallenge-club-c6e63c2bc0b7f40515c82bf832058d9da016babe.tar.gz perlweeklychallenge-club-c6e63c2bc0b7f40515c82bf832058d9da016babe.tar.bz2 perlweeklychallenge-club-c6e63c2bc0b7f40515c82bf832058d9da016babe.zip | |
pwc341 solution in go
| -rw-r--r-- | challenge-341/pokgopun/go/ch-1.go | 88 | ||||
| -rw-r--r-- | challenge-341/pokgopun/go/ch-2.go | 86 |
2 files changed, 174 insertions, 0 deletions
diff --git a/challenge-341/pokgopun/go/ch-1.go b/challenge-341/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..14a99c38f8 --- /dev/null +++ b/challenge-341/pokgopun/go/ch-1.go @@ -0,0 +1,88 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-341/ +/*# + +Task 1: Broken Keyboard + +Submitted by: [39]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a string containing English letters only and also you are + given broken keys. + + Write a script to return the total words in the given sentence can be + typed completely. + +Example 1 + +Input: $str = 'Hello World', @keys = ('d') +Output: 1 + +With broken key 'd', we can only type the word 'Hello'. + +Example 2 + +Input: $str = 'apple banana cherry', @keys = ('a', 'e') +Output: 0 + +Example 3 + +Input: $str = 'Coding is fun', @keys = () +Output: 3 + +No keys broken. + +Example 4 + +Input: $str = 'The Weekly Challenge', @keys = ('a','b') +Output: 2 + +Example 5 + +Input: $str = 'Perl and Python', @keys = ('p') +Output: 1 + +Task 2: Reverse Prefix +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + "strings" + + "github.com/google/go-cmp/cmp" +) + +func bk(str string, keys []rune) int { + l := len(keys) + var cnt, i int + for s := range strings.SplitSeq(strings.ToLower(str), " ") { + for i = 0; i < l; i++ { + if strings.ContainsRune(s, keys[i]) { + break + } + } + if i == l { + cnt++ + } + } + return cnt +} + +func main() { + for _, data := range []struct { + str string + keys []rune + output int + }{ + {"Hello World", []rune{'d'}, 1}, + {"apple banana cherry", []rune{'a', 'e'}, 0}, + {"Coding is fun", []rune{}, 3}, + {"The Weekly Challenge", []rune{'a', 'b'}, 2}, + {"Perl and Python", []rune{'p'}, 1}, + } { + io.WriteString(os.Stdout, cmp.Diff(bk(data.str, data.keys), data.output)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-341/pokgopun/go/ch-2.go b/challenge-341/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..3a43c95343 --- /dev/null +++ b/challenge-341/pokgopun/go/ch-2.go @@ -0,0 +1,86 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-341/ +/*# + +Task 2: Reverse Prefix + +Submitted by: [40]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a string, $str and a character in the given string, + $char. + + Write a script to reverse the prefix upto the first occurrence of the + given $char in the given string $str and return the new string. + +Example 1 + +Input: $str = "programming", $char = "g" +Output: "gorpramming" + +Reverse of prefix "prog" is "gorp". + +Example 2 + +Input: $str = "hello", $char = "h" +Output: "hello" + +Example 3 + +Input: $str = "abcdefghij", $char = "h" +Output: "hgfedcbaij" + +Example 4 + +Input: $str = "reverse", $char = "s" +Output: "srevere" + +Example 5 + +Input: $str = "perl", $char = "r" +Output: "repl" + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 5th October + 2025. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + "slices" + "strings" + + "github.com/google/go-cmp/cmp" +) + +func rp(s string, r rune) string { + i := strings.IndexRune(s, r) + if i < 1 { + return s + } + prfx := []rune(s[:i+1]) + slices.Reverse(prfx) + return string(prfx) + s[i+1:] +} + +func main() { + for _, data := range []struct { + str string + char rune + output string + }{ + {"programming", 'g', "gorpramming"}, + {"hello", 'h', "hello"}, + {"abcdefghij", 'h', "hgfedcbaij"}, + {"reverse", 's', "srevere"}, + {"perl", 'r', "repl"}, + } { + io.WriteString(os.Stdout, cmp.Diff(rp(data.str, data.char), data.output)) // blank if ok, otherwise show the difference + } +} |
