FizzBuzz
Write fizzbuzz(n) that returns a list of strings for the numbers from 1 to n, in order:
"FizzBuzz"if the number is divisible by both 3 and 5,"Fizz"if it is divisible by 3 only,"Buzz"if it is divisible by 5 only,- otherwise the number itself as a string.
For n = 0 return an empty list.
Understand the problem
Check the rules on single numbers.
What does the list contain at position 15 (the number 15)?
15 is divisible by both 3 and 5, so the answer is "FizzBuzz", one string. It is not "Fizz" followed by "Buzz".
A friend checks "divisible by 3" first, then "divisible by 5", then "divisible by 15". What does the friend's code give for 15?
"FizzBuzz""Fizz""15"
The first check that is true wins. 15 is divisible by 3, so the friend returns "Fizz" and never reaches the 15 check. The most specific rule (both) must be checked first.
What type are the items for the numbers that are not Fizz or Buzz?
- Integers, like
1 - Strings, like
"1"
The task says a list of strings. "1" and 1 look alike when printed but are different values, and the tests compare them exactly.
Write the solution
Build and return the list.
def fizzbuzz(n):
# Write your code here.
pass
Hint 1: a question to ask yourself
Which of the four cases is the most specific, and so must be tested first?
Hint 2: the shape of the answer
Loop over range(1, n + 1). Inside, an if / elif / elif / else chain. Append the result to a list.
Reflect
Solution
def fizzbuzz(n):
result = []
for number in range(1, n + 1):
if number % 15 == 0:
result.append("FizzBuzz")
elif number % 3 == 0:
result.append("Fizz")
elif number % 5 == 0:
result.append("Buzz")
else:
result.append(str(number))
return result
Another way: build the word, fall back to the number
def fizzbuzz(n):
result = []
for number in range(1, n + 1):
word = ("Fizz" if number % 3 == 0 else "") + ("Buzz" if number % 5 == 0 else "")
result.append(word or str(number))
return result
Another way: a comprehension
def fizzbuzz(n):
return ["FizzBuzz" if i % 15 == 0 else "Fizz" if i % 3 == 0 else "Buzz" if i % 5 == 0 else str(i)
for i in range(1, n + 1)]
What this tests: range, %, if/elif order, building a list, and str().
The second version has a real advantage. If the interviewer adds a rule ("Bazz for multiples of 7"), you add one more part to the word. In the first version you would need new combined cases (3 and 7, 5 and 7, all three): the number of branches explodes.
Cost: O(n) time, O(n) space for the result.
What an interviewer might ask next: Add a rule: multiples of 7 give "Bazz". How does each of your versions cope?
VISIBLE = 3
CASES = [
((5,), ['1', '2', 'Fizz', '4', 'Buzz'], 'the first five'),
((15,), ['1', '2', 'Fizz', '4', 'Buzz', 'Fizz', '7', '8', 'Fizz', 'Buzz', '11', 'Fizz', '13', '14', 'FizzBuzz'], 'up to the first FizzBuzz'),
((0,), [], 'nothing to list'),
((1,), ['1'], 'just one'),
((3,), ['1', '2', 'Fizz'], 'ends on Fizz'),
((10,), ['1', '2', 'Fizz', '4', 'Buzz', 'Fizz', '7', '8', 'Fizz', 'Buzz'], 'ends on Buzz'),
((16,), ['1', '2', 'Fizz', '4', 'Buzz', 'Fizz', '7', '8', 'Fizz', 'Buzz', '11', 'Fizz', '13', '14', 'FizzBuzz', '16'], 'just past FizzBuzz'),
]