Skip to content

Instantly share code, notes, and snippets.

@benjic
Created April 4, 2015 23:53
Show Gist options
  • Save benjic/3c6c2cd06d61cd9268f6 to your computer and use it in GitHub Desktop.
Save benjic/3c6c2cd06d61cd9268f6 to your computer and use it in GitHub Desktop.
package raindrops
import "fmt"
func Convert(num int) string {
return ConvertCompare(num, "A")
}
func ConvertCompare(num int, compare string) string {
// We build a map of the language
// We have to preserve order so slices are required
words := []string{"Pling", "Plang", "Plong"}
factors := []int{3, 5, 7}
if compare == "A" {
return ConvertFactorMapA(num, words, factors)
} else {
return ConvertFactorMapB(num, words, factors)
}
}
func ConvertFactorMapA(num int, words []string, factors []int) string {
var speak string
// Determine if given num is composed of any of the factors
for i, factor := range factors {
if num%factor == 0 {
speak += fmt.Sprintf(words[i])
}
}
// If string was not modified by map, emit num
if len(speak) == 0 {
speak += fmt.Sprintf("%d", num)
}
return speak
}
func ConvertFactorMapB(num int, words []string, factors []int) string {
var speak string
// Determine if given num is composed of any of the factors
for i, factor := range factors {
if num%factor == 0 {
speak += fmt.Sprintf(words[i])
}
}
// If string was not modified by map, emit num
if speak == "" {
speak += fmt.Sprintf("%d", num)
}
return speak
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment