Problem 170605 · easy · Level 01 Prerequisites & Setup

FizzBuzz List

lists · conditionals · for loop · modulo

Write a function fizzbuzz(n) that returns a list of n strings for the numbers 1 to n:

  • "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 example "7")

Examples

Input:  n = 5
Output: ["1", "2", "Fizz", "4", "Buzz"]
Input:  n = 15
Output: ["1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz"]
Input:  n = 0
Output: []

Constraints

  • 0 <= n <= 100
  • Every element of the returned list must be a string, including the plain numbers.

Goals

  • Build a list by appending one item per loop iteration
  • Order if/elif branches so the most specific condition is checked first
  • Convert a number to a string with str()
Starting Python…