aboutsummaryrefslogtreecommitdiff
path: root/challenge-273
diff options
context:
space:
mode:
authorMichael Manring <michael@manring>2024-06-11 18:17:22 +1000
committerMichael Manring <michael@manring>2024-06-11 18:17:22 +1000
commit7f4455ab3e39bd208a6a1f6449c2d702f19df091 (patch)
treeaad4eee2d2fe45a23ddeb00ee1baf686e40b7349 /challenge-273
parent292b6158c007e8600f9fdefc113e52f3a14f21dc (diff)
downloadperlweeklychallenge-club-7f4455ab3e39bd208a6a1f6449c2d702f19df091.tar.gz
perlweeklychallenge-club-7f4455ab3e39bd208a6a1f6449c2d702f19df091.tar.bz2
perlweeklychallenge-club-7f4455ab3e39bd208a6a1f6449c2d702f19df091.zip
pwc273 solution in go
Diffstat (limited to 'challenge-273')
-rw-r--r--challenge-273/pokgopun/go/ch-1.go86
-rw-r--r--challenge-273/pokgopun/go/ch-2.go77
2 files changed, 163 insertions, 0 deletions
diff --git a/challenge-273/pokgopun/go/ch-1.go b/challenge-273/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..5e9355844b
--- /dev/null
+++ b/challenge-273/pokgopun/go/ch-1.go
@@ -0,0 +1,86 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-273/
+/*#
+
+Task 1: Percentage of Character
+
+Submitted by: [52]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a string, $str and a character $char.
+
+ Write a script to return the percentage, nearest whole, of given
+ character in the given string.
+
+Example 1
+
+Input: $str = "perl", $char = "e"
+Output: 25
+
+Example 2
+
+Input: $str = "java", $char = "a"
+Output: 50
+
+Example 3
+
+Input: $str = "python", $char = "m"
+Output: 0
+
+Example 4
+
+Input: $str = "ada", $char = "a"
+Output: 67
+
+Example 5
+
+Input: $str = "ballerina", $char = "l"
+Output: 22
+
+Example 6
+
+Input: $str = "analitik", $char = "k"
+Output: 13
+
+Task 2: B After A
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+func poc(str string, chr rune) int {
+ res := 0
+ for _, v := range str {
+ if v == chr {
+ res++
+ }
+ }
+ res = 1000 * res / len(str)
+ if res%10 < 5 {
+ return res / 10
+ }
+ return 1 + res/10
+}
+
+func main() {
+ for _, data := range []struct {
+ str string
+ chr rune
+ p int
+ }{
+ {"perl", 'e', 25},
+ {"java", 'a', 50},
+ {"python", 'm', 0},
+ {"ada", 'a', 67},
+ {"ballerina", 'l', 22},
+ {"analitik", 'k', 13},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(poc(data.str, data.chr), data.p)) // blank if ok, otherwise show the differences
+ }
+}
diff --git a/challenge-273/pokgopun/go/ch-2.go b/challenge-273/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..79ba8a20ff
--- /dev/null
+++ b/challenge-273/pokgopun/go/ch-2.go
@@ -0,0 +1,77 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-273/
+/*#
+
+Task 2: B After A
+
+Submitted by: [53]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a string, $str.
+
+ Write a script to return true if there is at least one b, and no a
+ appears after the first b.
+
+Example 1
+
+Input: $str = "aabb"
+Output: true
+
+Example 2
+
+Input: $str = "abab"
+Output: false
+
+Example 3
+
+Input: $str = "aaa"
+Output: false
+
+Example 4
+
+Input: $str = "bbb"
+Output: true
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 16th June 2024.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+func aAfterB(str string) bool {
+ for i, v := range str {
+ if v == 'b' {
+ for _, c := range str[i+1:] {
+ if c == 'a' {
+ return false
+ }
+ return true
+ }
+ }
+ }
+ return false
+}
+
+func main() {
+ for _, data := range []struct {
+ str string
+ res bool
+ }{
+ {"aabb", true},
+ {"abab", false},
+ {"aaa", false},
+ {"bbb", true},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(aAfterB(data.str), data.res)) // blank if ok, otherwise show the differences
+ }
+}