FizzBuzz Practice Problem
This data science coding problem helps you practice Control Flow & Loops, fizzbuzz, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Control Flow & Loops.
- Problem ID: 78
- Problem key: 78-fizzbuzz
- URL: https://datacrack.app/solve/78-fizzbuzz
- Difficulty: easy
- Topic: Control Flow & Loops
- Module: Python Fundamentals
Problem Statement
# 🧩 FizzBuzz
---
### 🎯 Goal
FizzBuzz is the most classic programming interview warmup. It tests whether you can combine **conditionals** and **loops** cleanly — the two most fundamental building blocks in any program.
---
### 🔍 The Rules
| Condition | Output |
|:----------|:-------|
| Number divisible by **both** 3 and 5 | `"FizzBuzz"` |
| Number divisible by **3 only** | `"Fizz"` |
| Number divisible by **5 only** | `"Buzz"` |
| Otherwise | The number as a **string** |
> ⚠️ **Order matters!** Check divisibility by 15 (both 3 and 5) **first**, otherwise you'll never reach that case.
---
### 💻 Task
Implement `fizzbuzz(n)` that returns a list of strings following the rules above.
---
### 📥 Input
- `n`: int — generate output for numbers 1 through n (inclusive)
### 📤 Output
- A list of strings of length n
---
### 🧩 Starter Code
```python
def fizzbuzz(n):
"""
Return a list of strings from 1 to n following FizzBuzz rules.
Args:
n (int): Upper bound (inclusive)
Returns:
list[str]: FizzBuzz output
"""
# 🧠 TODO: Loop from 1 to n, apply the FizzBuzz rules, collect results
pass
```
---
### 💡 Example
```python
fizzbuzz(5)
# Expected: ["1", "2", "Fizz", "4", "Buzz"]
```
---
### 🔑 Key Concepts
- `range(1, n+1)` to loop from 1 to n inclusive
- `%` (modulo) operator to check divisibility
- `str(i)` to convert a number to a string
- Check the `15` case **before** checking `3` or `5` individuallyStarter Code
def fizzbuzz(n):
"""
Return a list of strings from 1 to n following FizzBuzz rules.
Args:
n (int): Upper bound (inclusive)
Returns:
list[str]: FizzBuzz output
"""
# 🧠 TODO: Loop from 1 to n, apply the FizzBuzz rules, collect results
pass