Created
June 17, 2014 15:35
-
-
Save elight/62de38fbb58bf2ed0e34 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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