diff options
| author | Michael Manring <michael@manring> | 2024-07-29 14:53:51 +1000 |
|---|---|---|
| committer | Michael Manring <michael@manring> | 2024-07-29 14:53:51 +1000 |
| commit | 9c694541b722a3d170491cf8c0dabb45e639634f (patch) | |
| tree | 42ad4cb41126cca51274c2cf9c7841f3ccc3ba99 | |
| parent | 3b7bebba866523c41b1631ece9c290b5c7f50a1b (diff) | |
| download | perlweeklychallenge-club-9c694541b722a3d170491cf8c0dabb45e639634f.tar.gz perlweeklychallenge-club-9c694541b722a3d170491cf8c0dabb45e639634f.tar.bz2 perlweeklychallenge-club-9c694541b722a3d170491cf8c0dabb45e639634f.zip | |
pwc280 solution in go
| -rw-r--r-- | challenge-280/pokgopun/go/ch-1.go | 77 | ||||
| -rw-r--r-- | challenge-280/pokgopun/go/ch-2.go | 86 |
2 files changed, 163 insertions, 0 deletions
diff --git a/challenge-280/pokgopun/go/ch-1.go b/challenge-280/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..ce48d76009 --- /dev/null +++ b/challenge-280/pokgopun/go/ch-1.go @@ -0,0 +1,77 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-280/ +/*# + +Task 1: Twice Appearance + +Submitted by: [53]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a string, $str, containing lowercase English letters + only. + + Write a script to print the first letter that appears twice. + +Example 1 + +Input: $str = "acbddbca" +Output: "d" + +Example 2 + +Input: $str = "abccd" +Output: "c" + +Example 3 + +Input: $str = "abcdabbb" +Output: "a" + +Task 2: Count Asterisks +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + + "github.com/google/go-cmp/cmp" +) + +func twcApr(str string) rune { + m := make(map[rune]int) + var ( + c rune + mn int + ) + for i, v := range str { + //fmt.Println(string(v), v) + if m[v] == 0 { + m[v] = -i - 1 + } else if m[v] < 0 { + m[v] = i + 1 + } + if m[v] > 0 { + if mn == 0 || mn > m[v] { + mn = m[v] + c = v + } + } + } + //fmt.Println(m) + return c +} + +func main() { + for _, data := range []struct { + input string + output rune + }{ + {"acbddbca", 'd'}, + {"abccd", 'c'}, + {"abcdabbb", 'a'}, + } { + io.WriteString(os.Stdout, cmp.Diff(twcApr(data.input), data.output)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-280/pokgopun/go/ch-2.go b/challenge-280/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..a07e74a936 --- /dev/null +++ b/challenge-280/pokgopun/go/ch-2.go @@ -0,0 +1,86 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-280/ +/*# + +Task 2: Count Asterisks + +Submitted by: [54]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a string, $str, where every two consecutive vertical bars + are grouped into a pair. + + Write a script to return the number of asterisks, *, excluding any + between each pair of vertical bars. + +Example 1 + +Input: $str = "p|*e*rl|w**e|*ekly|" +Ouput: 2 + +The characters we are looking here are "p" and "w**e". + +Example 2 + +Input: $str = "perl" +Ouput: 0 + +Example 3 + +Input: $str = "th|ewe|e**|k|l***ych|alleng|e" +Ouput: 5 + +The characters we are looking here are "th", "e**", "l***ych" and "e". + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 4th August + 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + + "github.com/google/go-cmp/cmp" +) + +func cntAtr(str string) int { + a, b := byte('*'), byte('|') + c := 0 + l := len(str) + for l > 0 { + l-- + switch str[l] { + case a: + c++ + case b: + d := len(str[:l]) + for d > 0 { + d-- + if str[d] == b { + l = d + break + } + } + } + } + return c +} + +func main() { + for _, data := range []struct { + input string + output int + }{ + {"p|*e*rl|w**e|*ekly|", 2}, + {"perl", 0}, + {"th|ewe|e**|k|l***ych|alleng|e", 5}, + } { + io.WriteString(os.Stdout, cmp.Diff(cntAtr(data.input), data.output)) // blank if ok, otherwise show the difference + } +} |
