diff options
| author | Michael Manring <michael@manring> | 2022-05-04 14:06:59 +0700 |
|---|---|---|
| committer | Michael Manring <michael@manring> | 2022-05-04 14:24:08 +0700 |
| commit | e7a86e0aa13e0fb34edd4ae0cdd30d4d5f51e130 (patch) | |
| tree | be37d1c042c868f3df0473f14a971754138ac7bb /challenge-001 | |
| parent | b13444bb5307ad7c7051ace6031dd063bac3cc32 (diff) | |
| download | perlweeklychallenge-club-e7a86e0aa13e0fb34edd4ae0cdd30d4d5f51e130.tar.gz perlweeklychallenge-club-e7a86e0aa13e0fb34edd4ae0cdd30d4d5f51e130.tar.bz2 perlweeklychallenge-club-e7a86e0aa13e0fb34edd4ae0cdd30d4d5f51e130.zip | |
pwc001 solution in go
Diffstat (limited to 'challenge-001')
| -rw-r--r-- | challenge-001/pokgopun/README | 1 | ||||
| -rw-r--r-- | challenge-001/pokgopun/go/ch-1.go | 13 | ||||
| -rw-r--r-- | challenge-001/pokgopun/go/ch-2.go | 47 |
3 files changed, 61 insertions, 0 deletions
diff --git a/challenge-001/pokgopun/README b/challenge-001/pokgopun/README new file mode 100644 index 0000000000..33dfd303a4 --- /dev/null +++ b/challenge-001/pokgopun/README @@ -0,0 +1 @@ +Solution by PokGoPun diff --git a/challenge-001/pokgopun/go/ch-1.go b/challenge-001/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..41e9fb4fb6 --- /dev/null +++ b/challenge-001/pokgopun/go/ch-1.go @@ -0,0 +1,13 @@ +package main + +import ( + "fmt" + "strings" +) + +func main() { + s := "Perl Weekly Challenge" + l := "e" + fmt.Printf("%q has %q replaced with %q for %v times and resulted in %q\n", + s, l, strings.ToUpper(l), strings.Count(s, l), strings.ReplaceAll(s, l, strings.ToUpper(l))) +} diff --git a/challenge-001/pokgopun/go/ch-2.go b/challenge-001/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..fdeeab5d4c --- /dev/null +++ b/challenge-001/pokgopun/go/ch-2.go @@ -0,0 +1,47 @@ +package main + +import ( + "bufio" + "fmt" + "os" + "strconv" +) + +var ( + rseen [15]bool + r2str [15]string +) + +func main() { + w := bufio.NewWriter(os.Stdout) + n, _ := strconv.Atoi(append(os.Args, "20")[1]) + for i := 1; i <= n; i++ { + fmt.Fprintf(w, "%v ", fizzbuzz(i)) + } + w.Flush() + fmt.Print("\n") +} + +func fizzbuzz(n int) (str string) { + r := n % 15 + if rseen[r] { + str = r2str[r] + if str == "" { + return strconv.Itoa(n) + } + return str + } + rseen[r] = true + switch { + case r == 0: + str = "fizzbuzz" + case r%3 == 0: + str = "fizz" + case r%5 == 0: + str = "buzz" + default: + return strconv.Itoa(n) + } + r2str[r] = str + return str +} |
