-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path02-fizzbuzz.py
More file actions
40 lines (34 loc) · 1.26 KB
/
02-fizzbuzz.py
File metadata and controls
40 lines (34 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"""
Challenge: FizzBuzz
Difficulty: Beginner
Concepts: loops, conditionals, modulo operator, list building
Time: 15 minutes
Given an integer n, return a list of strings from 1 to n where:
- Multiples of 3 are replaced with "Fizz"
- Multiples of 5 are replaced with "Buzz"
- Multiples of both 3 and 5 are replaced with "FizzBuzz"
- All other numbers are converted to their string representation
Examples:
>>> fizzbuzz(5)
['1', '2', 'Fizz', '4', 'Buzz']
>>> fizzbuzz(15)[-1]
'FizzBuzz'
"""
def fizzbuzz(n: int) -> list[str]:
"""Return FizzBuzz sequence from 1 to n. Implement this function."""
# Hint: Check divisibility by 15 (both) before checking 3 or 5 individually.
pass
# --- Tests (do not modify) ---
if __name__ == "__main__":
# Test 1: Small range
assert fizzbuzz(5) == ["1", "2", "Fizz", "4", "Buzz"], "Small range failed"
# Test 2: FizzBuzz at 15
result = fizzbuzz(15)
assert result[14] == "FizzBuzz", "FizzBuzz at 15 failed"
assert result[2] == "Fizz", "Fizz at 3 failed"
assert result[4] == "Buzz", "Buzz at 5 failed"
# Test 3: Single element
assert fizzbuzz(1) == ["1"], "Single element failed"
# Test 4: Length check
assert len(fizzbuzz(20)) == 20, "Length check failed"
print("All tests passed!")