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.
38 lines
908 B
38 lines
908 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++ {
|
|
switch {
|
|
case isAMultiple(j, in.int1) && isAMultiple(j, in.int2):
|
|
out = append(out, in.string1+in.string2)
|
|
case isAMultiple(j, in.int1):
|
|
out = append(out, in.string1)
|
|
case isAMultiple(j, in.int2):
|
|
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
|
|
}
|
|
|