diff options
| author | Pok <pok@goyangi> | 2025-07-21 21:10:57 +1000 |
|---|---|---|
| committer | Pok <pok@goyangi> | 2025-07-21 21:10:57 +1000 |
| commit | 6fa4b220680e4136652dbbda5472e87bff7c6147 (patch) | |
| tree | c5e97827d81554f676e609c0c2e82a9b954160e2 | |
| parent | 9db22599e35fbde3c6c48637333b2167d1914e2f (diff) | |
| download | perlweeklychallenge-club-6fa4b220680e4136652dbbda5472e87bff7c6147.tar.gz perlweeklychallenge-club-6fa4b220680e4136652dbbda5472e87bff7c6147.tar.bz2 perlweeklychallenge-club-6fa4b220680e4136652dbbda5472e87bff7c6147.zip | |
pwc331 solution in go
| -rw-r--r-- | challenge-331/pokgopun/go/ch-1.go | 58 | ||||
| -rw-r--r-- | challenge-331/pokgopun/go/ch-2.go | 105 |
2 files changed, 163 insertions, 0 deletions
diff --git a/challenge-331/pokgopun/go/ch-1.go b/challenge-331/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..eed78fe900 --- /dev/null +++ b/challenge-331/pokgopun/go/ch-1.go @@ -0,0 +1,58 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-331/ +/*# + +Task 1: Last Word + +Submitted by: [43]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a string. + + Write a script to find the length of last word in the given string. + +Example 1 + +Input: $str = "The Weekly Challenge" +Output: 9 + +Example 2 + +Input: $str = " Hello World " +Output: 5 + +Example 3 + +Input: $str = "Let's begin the fun" +Output: 3 + +Task 2: Buddy Strings +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + "regexp" + + "github.com/google/go-cmp/cmp" +) + +func lw(str string) int { + idx := regexp.MustCompile(`(\S+)\s*$`).FindStringSubmatchIndex(str) + return idx[3] - idx[2] +} + +func main() { + for _, data := range []struct { + input string + output int + }{ + {"The Weekly Challenge", 9}, + {" Hello World ", 5}, + {"Let's begin the fun", 3}, + } { + io.WriteString(os.Stdout, cmp.Diff(lw(data.input), data.output)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-331/pokgopun/go/ch-2.go b/challenge-331/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..c1a735e3bd --- /dev/null +++ b/challenge-331/pokgopun/go/ch-2.go @@ -0,0 +1,105 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-331/ +/*# + +Task 2: Buddy Strings + +Submitted by: [44]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given two strings, source and target. + + Write a script to find out if the given strings are Buddy Strings. +If swapping of a letter in one string make them same as the other then they are +`Buddy Strings`. + +Example 1 + +Input: $source = "fuck" + $target = "fcuk" +Output: true + +The swapping of 'u' with 'c' makes it buddy strings. + +Example 2 + +Input: $source = "love" + $target = "love" +Output: false + +Example 3 + +Input: $source = "fodo" + $target = "food" +Output: true + +Example 4 + +Input: $source = "feed" + $target = "feed" +Output: true + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 27th 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 input struct { + source, target string +} + +func (in input) process() bool { + l := len(in.source) + if l != len(in.target) { + return false + } + s := []byte(in.source) + slices.Sort(s) + s = slices.Compact(s) + if in.source == in.target { + if l != len(s) { + return true + } else { + return false + } + } + var swp []byte + for i := range l { + a, b := in.source[i], in.target[i] + if a != b { + swp = append(swp, a, b) + } + if len(swp) == 4 { + if swp[0] == swp[3] && swp[1] == swp[2] { + return in.source[i+1:] == in.target[i+1:] + } + } + } + return false +} + +func main() { + for _, data := range []struct { + input input + output bool + }{ + {input{"fuck", "fcuk"}, true}, + {input{"love", "love"}, false}, + {input{"fodo", "food"}, true}, + {input{"feed", "feed"}, true}, + } { + io.WriteString(os.Stdout, cmp.Diff(data.input.process(), data.output)) // blank if ok, otherwise show the difference + } +} |
