diff options
| author | Michael Manring <michael@manring> | 2024-01-31 18:41:47 +1100 |
|---|---|---|
| committer | Michael Manring <michael@manring> | 2024-01-31 18:41:47 +1100 |
| commit | ca551848a174f3eec7c090ac79353d4a9e76889c (patch) | |
| tree | 1631e28e8ff9ef7156ac77b5648d4fcd6092ae59 /challenge-254 | |
| parent | c850dc1a7833eae370a271d621700564a5d3e063 (diff) | |
| download | perlweeklychallenge-club-ca551848a174f3eec7c090ac79353d4a9e76889c.tar.gz perlweeklychallenge-club-ca551848a174f3eec7c090ac79353d4a9e76889c.tar.bz2 perlweeklychallenge-club-ca551848a174f3eec7c090ac79353d4a9e76889c.zip | |
pwc254 solution in go
Diffstat (limited to 'challenge-254')
| -rw-r--r-- | challenge-254/pokgopun/go/ch-1.go | 63 | ||||
| -rw-r--r-- | challenge-254/pokgopun/go/ch-2.go | 101 |
2 files changed, 164 insertions, 0 deletions
diff --git a/challenge-254/pokgopun/go/ch-1.go b/challenge-254/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..07a6cbac87 --- /dev/null +++ b/challenge-254/pokgopun/go/ch-1.go @@ -0,0 +1,63 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-254/ +/*# + +Task 1: Three Power + +Submitted by: [47]Mohammad S Anwar + __________________________________________________________________ + + You are given a positive integer, $n. + + Write a script to return true if the given integer is a power of three + otherwise return false. + +Example 1 + +Input: $n = 27 +Output: true + +27 = 3 ^ 3 + +Example 2 + +Input: $n = 0 +Output: true + +0 = 0 ^ 3 + +Example 3 + +Input: $n = 6 +Output: false + +Task 2: Reverse Vowels +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "math" + "os" + + "github.com/google/go-cmp/cmp" +) + +func isPowerOfThree(n int) bool { + r := int(math.Pow(float64(n), 1./3)) + return r*r*r == n || (r+1)*(r+1)*(r+1) == n +} + +func main() { + for _, data := range []struct { + input int + output bool + }{ + {27, true}, + {0, true}, + {6, false}, + } { + io.WriteString(os.Stdout, cmp.Diff(isPowerOfThree(data.input), data.output)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-254/pokgopun/go/ch-2.go b/challenge-254/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..e96afd6ed7 --- /dev/null +++ b/challenge-254/pokgopun/go/ch-2.go @@ -0,0 +1,101 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-254/ +/*# + +Task 2: Reverse Vowels + +Submitted by: [48]Mohammad S Anwar + __________________________________________________________________ + + You are given a string, $s. + + Write a script to reverse all the vowels (a, e, i, o, u) in the given + string. + +Example 1 + +Input: $s = "Raku" +Output: "Ruka" + +Example 2 + +Input: $s = "Perl" +Output: "Perl" + +Example 3 + +Input: $s = "Julia" +Output: "Jaliu" + +Example 4 + +Input: $s = "Uiua" +Output: "Auiu" + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 4th February + 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + "strings" + + "github.com/google/go-cmp/cmp" +) + +func main() { + for _, data := range []struct { + input, output str + }{ + {"Raku", "Ruka"}, + {"Perl", "Perl"}, + {"Julia", "Jaliu"}, + {"Uiua", "Auiu"}, + } { + io.WriteString(os.Stdout, cmp.Diff(data.input.reverseVowels(), data.output)) // blank if ok, otherwise show the difference + } +} + +type char byte + +func (ch char) isVowel() bool { + for _, c := range []byte("aeiou") { + if char(c) == ch { + return true + } + } + return false +} + +type str string + +func (st str) vowels() str { + var b strings.Builder + for _, v := range []byte(strings.ToLower(string(st))) { + if char(v).isVowel() { + b.WriteByte(v) + } + } + return str(b.String()) +} + +func (st str) reverseVowels() str { + var b strings.Builder + vwls := st.vowels() + l := len(vwls) + for _, v := range []byte(strings.ToLower(string(st))) { + if char(v).isVowel() { + v = vwls[l-1] + l-- + } + b.WriteByte(v) + } + return str(strings.Title(b.String())) +} |
