Generate Number Sequence with Divisibility Rules
StringsAuto-graded · 15 test casesLast updated
Tests basic control flow, modular arithmetic, and string formatting by transforming numbers according to divisibility conditions.
Create a function that generates a sequence of strings from 1 through n, where each position follows specific transformation rules based on divisibility:
- When a number is evenly divisible by 3, output "Fizz"
- When a number is evenly divisible by 5, output "Buzz"
- When a number is evenly divisible by both 3 and 5, output "FizzBuzz"
- Otherwise, output the number itself as a string
Return the complete sequence as a list of strings in ascending order.
Constraints
1 <= n <= 10^4- All numbers must be represented as strings in the output
- The sequence must include all integers from 1 to
ninclusive
Examples
Example 1
Input: number_sequence(5)
Output: ['1', '2', 'Fizz', '4', 'Buzz']
Example 2
Input: number_sequence(15)
Output: ['1', '2', 'Fizz', '4', 'Buzz', 'Fizz', '7', '8', 'Fizz', 'Buzz', '11', 'Fizz', '13', '14', 'FizzBuzz']
Hints
Hint 1
Check divisibility by 15 before checking 3 or 5 individually to handle the combined case correctly
Hint 2
You can also build the output string incrementally by concatenating 'Fizz' and 'Buzz' separately based on each condition
Hint 3
Remember that the result must contain strings, not integers, so convert numbers to strings when no divisibility rule applies
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Iterate through each integer from 1 to n and apply transformation rules based on divisibility tests. The key insight is recognizing that when a number is divisible by both 3 and 5, it must also be divisible by their product (15), since 3 and 5 are coprime.
The solution uses an if-elif chain that prioritizes checking divisibility by 15 first. This ordering is critical: if we checked divisibility by 3 before checking for 15, numbers like 15, 30, and 45 would incorrectly return "Fizz" and never reach the combined case.
For each number in the range:
- First check if it's divisible by 15 (both 3 and 5) → append "FizzBuzz"
- Otherwise check if it's divisible by 3 → append "Fizz"
- Otherwise check if it's divisible by 5 → append "Buzz"
- If none of the above, convert the number to a string and append it
This approach guarantees that each number maps to exactly one output string, and the sequence maintains ascending order since we iterate through the range sequentially.
Why It Works
The modulo operator % gives the remainder when dividing two numbers. A remainder of zero means the division is exact, indicating divisibility. Since 3 and 5 share no common factors other than 1, a number divisible by both must be divisible by their least common multiple, which is 15.
By checking the most specific condition first (divisibility by 15) and using elif for the other conditions, we ensure mutual exclusivity: each number triggers exactly one branch. The final else catches all numbers that don't match any divisibility rule.
Complexity
Time Complexity: O(n)
We perform a single pass through the range from 1 to n, executing a constant number of operations for each number (at most three modulo operations and one append).
Space Complexity: O(n)
The output list contains exactly n strings, which is required by the problem specification. No additional auxiliary space grows with input size.
Solution
from typing import List
def number_sequence(n: int) -> List[str]:
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