+50 XP
instructions09 / 63
taught in: control flow
Easycontrol-flownumbersstrings~15m+50 XP
FizzBuzz
The smallest interesting program in the world, and the reason it survives as an exercise: it needs three branches, and the order you write them in decides whether it is right.
Go's if takes a bool and nothing else. There is no truthiness, so if n is
a compile error and if n != 0 is what you meant. The condition needs no
parentheses, and the braces are never optional:
if n%3 == 0 {
return "Fizz"
} else if n%5 == 0 {
return "Buzz"
}
% is the remainder operator. In Go the result takes the sign of the left
operand, so -7 % 3 is -1 and 7 % -3 is 1. That only matters here because
n % 3 == 0 is true for negative multiples too, which is what the tests expect.
Task
Finish the three functions in main.go.
FizzBuzz(n int) stringclassifies one number:"FizzBuzz"when it divides by both 3 and 5,"Fizz"for 3,"Buzz"for 5, and otherwise the number's digits as a string.FizzBuzz(7)is"7".FizzBuzzUpTo(n int) []stringreturnsFizzBuzz(1)throughFizzBuzz(n)in order. Annbelow 1 gives a slice with no elements.IsLeapYear(y int) boolapplies the Gregorian rule: divisible by 4, except divisible by 100, except divisible by 400.2000is a leap year and1900is not.
Hints
- Check the combined case first.
15 % 3is0, so aFizzbranch above theFizzBuzzbranch answers"Fizz"and the right answer never gets a turn.n%15 == 0andn%3 == 0 && n%5 == 0are the same test; pick the one you find clearer. - Turning an
intinto its digits isstrconv.Itoa(n).string(n)compiles and is wrong: it produces the character with that code point.fmt.Sprint(n)works too and costs more. strconvis not imported yet. Add it; an unused import is a compile error, which is why the starter does not carry one you might not need.- Build the slice with
appendon a startingout := []string{}. The tests only check the length, sonilwould pass too, but an empty non-nil slice is the friendlier thing to hand back. - The leap-year rule is three tests in a specific order: 400 first, then 100, then 4. Written in any other order it is either wrong or much longer.