Skip to content

Instantly share code, notes, and snippets.

@elight

elight/raindrops.go

Created Jun 17, 2014
Embed
What would you like to do?
package raindrops
import (
"bytes"
"fmt"
)
func Convert(v int) string {
fs := findRelevantFactorsFor(v)
return formatFactorsOf(v, fs)
}
func findRelevantFactorsFor(v int) (factors []int) {
for _, factor := range []int{3, 5, 7} {
if v % factor == 0 {
factors = append(factors, factor)
}
}
return
}
var dictFactorToStr = map[int]string{
3: "Pling",
5: "Plang",
7: "Plong",
}
func formatFactorsOf(v int, factors []int) string {
if len(factors) == 0 {
return fmt.Sprintf("%d", v)
} else {
var buf bytes.Buffer
for _, factor := range factors {
buf.WriteString(dictFactorToStr[factor])
}
return buf.String()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment