Simple implementation of LBC's fizzbuzz API server
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
939 B

package fizzbuzz
import "fmt"
// inputs represent the inputs received by the fizzbuzz server
type inputs struct {
string1, string2 string
int1, int2, limit int
}
// generateFizzbuzz creates the fizzbuzz-like output related to the current input
// It is lenient with invalid inputs (nil input, <1 limits, empty strings or nul integers, ...)
func (in *inputs) generateFizzbuzz() []string {
out := []string{}
if in == nil {
return out
}
for j := 1; j <= in.limit; j++ {
multipleOf1, multipleOf2 := isAMultiple(j, in.int1), isAMultiple(j, in.int2)
switch {
case multipleOf1 && multipleOf2:
out = append(out, in.string1+in.string2)
case multipleOf1:
out = append(out, in.string1)
case multipleOf2:
out = append(out, in.string2)
default:
out = append(out, fmt.Sprint(j))
}
}
return out
}
func isAMultiple(toTest, divisor int) bool {
if divisor == 0 {
return false
}
return (toTest % divisor) == 0
}