diff options
| author | Pok <pok@goyangi> | 2025-07-08 00:30:18 +1000 |
|---|---|---|
| committer | Pok <pok@goyangi> | 2025-07-08 00:30:18 +1000 |
| commit | fbe98362987fc353e0d39127b4d502ab3b040656 (patch) | |
| tree | 2d5ca19dc3588588188e5bc91a727fba5341a1b7 | |
| parent | b4c18b7b77aaae65d55b5155d29131dccaf54ecb (diff) | |
| download | perlweeklychallenge-club-fbe98362987fc353e0d39127b4d502ab3b040656.tar.gz perlweeklychallenge-club-fbe98362987fc353e0d39127b4d502ab3b040656.tar.bz2 perlweeklychallenge-club-fbe98362987fc353e0d39127b4d502ab3b040656.zip | |
pwc329 solution in go
| -rw-r--r-- | challenge-329/pokgopun/go/ch-1.go | 74 | ||||
| -rw-r--r-- | challenge-329/pokgopun/go/ch-2.go | 96 |
2 files changed, 170 insertions, 0 deletions
diff --git a/challenge-329/pokgopun/go/ch-1.go b/challenge-329/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..1469d78265 --- /dev/null +++ b/challenge-329/pokgopun/go/ch-1.go @@ -0,0 +1,74 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-329/ +/*# + +Task 1: Counter Integers + +Submitted by: [46]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a string containing only lower case English letters and + digits. + + Write a script to replace every non-digit character with a space and + then return all the distinct integers left. + +Example 1 + +Input: $str = "the1weekly2challenge2" +Output: 1, 2 + +2 is appeared twice, so we count it one only. + +Example 2 + +Input: $str = "go21od1lu5c7k" +Output: 21, 1, 5, 7 + +Example 3 + +Input: $str = "4p3e2r1l" +Output: 4, 3, 2, 1 + +Task 2: Nice String +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + "regexp" + "strconv" + "strings" + + "github.com/google/go-cmp/cmp" +) + +type input string + +func (in input) process() []int { + var out []int + seen := make(map[string]struct{}) + for _, v := range strings.Split(strings.Trim(regexp.MustCompile(`\D+`).ReplaceAllString(string(in), " "), " "), " ") { + if _, ok := seen[v]; !ok { + seen[v] = struct{}{} + n, _ := strconv.Atoi(v) + out = append(out, n) + } + } + return out +} + +func main() { + for _, data := range []struct { + input input + output []int + }{ + {"the1weekly2challenge2", []int{1, 2}}, + {"go21od1lu5c7k", []int{21, 1, 5, 7}}, + {"4p3e2r1l", []int{4, 3, 2, 1}}, + } { + io.WriteString(os.Stdout, cmp.Diff(data.input.process(), data.output)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-329/pokgopun/go/ch-2.go b/challenge-329/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..f7df4ef034 --- /dev/null +++ b/challenge-329/pokgopun/go/ch-2.go @@ -0,0 +1,96 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-329/ +/*# + +Task 2: Nice String + +Submitted by: [47]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a string made up of lower and upper case English letters + only. + + Write a script to return the longest substring of the give string which + is nice. A string is nice if, for every letter of the alphabet that the + string contains, it appears both in uppercase and lowercase. + +Example 1 + +Input: $str = "YaaAho" +Output: "aaA" + +Example 2 + +Input: $str = "cC" +Output: "cC" + +Example 3 + +Input: $str = "A" +Output: "" + +No nice string found. + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 13th July 2025. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + "slices" + + "github.com/google/go-cmp/cmp" +) + +type str string + +func (s str) process() str { + var out str + l := len(s) - 1 + i := l + start := l - i + for i > 0 { + i-- + idx := l - i + a, b := s[idx], s[idx-1] + d := max(a, b) - min(a, b) + if d == 0 || d == 32 { + continue + } else { + cdd := s[start:idx] + if len(cdd) > len(out) && len(slices.Compact([]byte(cdd))) > 1 { + out = cdd + } + start = idx + } + } + if i == 0 { + cdd := s[start:] + if len(cdd) > len(out) && len(slices.Compact([]byte(cdd))) > 1 { + out = cdd + } + } + return out +} + +func main() { + for _, data := range []struct { + input, output str + }{ + {"YaaAho", "aaA"}, + {"YaaAhoooo", "aaA"}, + {"YaaAhoOOo", "oOOo"}, + {"YaaAhooO", "aaA"}, + {"cC", "cC"}, + {"cc", ""}, + {"A", ""}, + } { + io.WriteString(os.Stdout, cmp.Diff(data.input.process(), data.output)) // blank if ok, otherwise show the difference + } +} |
