CODING CHALLENGE · N°03

Fizz Buzz

Easy MathStringsWarm-up

The famous screening question. Print 1…n, but multiples of 3 become "Fizz", of 5 become "Buzz", and of both become "FizzBuzz". Easy — the catch is testing divisibility in the right order.

The problem

Return a list of length n where, for each i from 1 to n: it is "FizzBuzz" if i is divisible by both 3 and 5, "Fizz" if only by 3, "Buzz" if only by 5, otherwise the number as a string.

EXAMPLE 1
Input n = 5
Output ["1", "2", "Fizz", "4", "Buzz"]
EXAMPLE 2
Input n = 15
Output [..., "13", "14", "FizzBuzz"]
15 is divisible by 3 and 5
EXAMPLE 3
Input n = 3
Output ["1", "2", "Fizz"]
CONSTRAINTS
  • 1 ≤ n ≤ 10⁴
  • Every element is a string — including the plain numbers.
  • Check divisibility by 15 first (or combine the two booleans), or the FizzBuzz case never fires.
SOLVE IT YOURSELF

Your turn — write it

Edit the stub, hit Run (or ⌘/Ctrl + Enter), and watch the hidden tests. Stuck? the hints are right above and Reveal solution is one click away.

YOUR TASK

Implement fizzbuzz(n): return the list of length n following the Fizz / Buzz / FizzBuzz rules. Watch the order — test "both" before either single case.

HINTS — 4 IDEAS
  1. Loop i from 1 to n inclusive.
  2. Divisible by both 3 and 5 is the same as divisible by 15 — check that first.
  3. Otherwise check 3, then 5, then fall back to the number.
  4. Everything in the list is a string, so convert the plain numbers too.
CPython · WebAssembly