Skip to content

Instantly share code, notes, and snippets.

@mandykoh
Last active February 14, 2019 11:47
Show Gist options
  • Select an option

  • Save mandykoh/99ccec7dc017dfd67d664377df668b40 to your computer and use it in GitHub Desktop.

Select an option

Save mandykoh/99ccec7dc017dfd67d664377df668b40 to your computer and use it in GitHub Desktop.
FizzBuzz With Regexes
package main
import (
"fmt"
"regexp"
"strconv"
"strings"
)
func main() {
parts := ""
for i := 0; i < 100; i++ {
parts += "," + strconv.Itoa(i + 1)
}
mark := func(n int, label string) {
r := fmt.Sprintf(`(,\w+){%d}(,\w+)`, n - 1)
parts = regexp.MustCompile(r).ReplaceAllStringFunc(parts,
func(s string) string {
return s[:strings.LastIndex(s, ",")] + "," + label
})
}
mark(3, "Fizz")
mark(5, "Buzz")
mark(15, "FizzBuzz")
fmt.Println(strings.Join(strings.Split(parts[1:], ","), "\n"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment