This is a creation in Article, where the information may have evolved or changed.
Topic
Write A program this outputs the string representation of numbers from 1 to N.
Multiples of three it should output "Fizz" instead of the number and for the multiples of the five output "Buzz". For numbers which is multiples of both three and five output "Fizzbuzz".
Example:
n = 15,
Return:
[
"1",
"2",
"Fizz",
"4",
"Buzz",
"Fizz",
"7",
"8",
"Fizz",
"Buzz",
"11",
"Fizz",
"13",
"14",
"Fizzbuzz"
]
Code
Fizzbuzz.go
package _412_Fizz_Buzzimport "strconv"func FizzBuzz(n int) []string { var ret []string var i int for i = 1; i <= n; i++ { if 0 == i % 15 { ret = append(ret, "FizzBuzz") } else if 0 == i % 3 { ret = append(ret, "Fizz") } else if 0 == i % 5 { ret = append(ret, "Buzz") } else { ret = append(ret, strconv.Itoa(i)) } } return ret}
Test
Fizzbuzz_test.go
Package _412_fizz_buzzimport "Testing" func arraystringequal (Want, ret []string) bool {len1: = Len (Want) Len2: = Len (ret) if len1! = Len2 {return false} for I: = 0; i < len1; i++ {if want[i]! = Ret[i] {return false}} return True}func Testfizzbuzz (t *testing. T) {input: = want: = []string{"1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "One", "Fizz", "13", "+", "Fizzbuzz"} RET: = Fizzbuzz (input) Equal: = Arraystringequal (Want, ret) If equal {T.LOGF ("pass")} else {T.errorf ("fail, want%+v, get%+v", Want, Ret)}}