Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ruff formatting
Browse files Browse the repository at this point in the history
matopcheg committed Jan 13, 2025
1 parent e6d3e13 commit b84f300
Showing 2 changed files with 56 additions and 15 deletions.
20 changes: 11 additions & 9 deletions solutions/fizz_buzz.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
This module provides a function that generates a list of numbers from 1 to n
This module provides a function that generates a list of numbers from 1 to n
with specific rules for multiples of 3 and 5.
For each number in the range:
@@ -9,13 +9,13 @@
Features:
- Input validation to ensure that the input is a positive integer.
- Customizable output format (e.g., changing the words "Fizz" and "Buzz"
- Customizable output format (e.g., changing the words "Fizz" and "Buzz"
to user-defined strings).
- Ability to generate results as a string instead of a list.
Example:
>>> fizz_buzz(15)
[1, 2, 'Fizz', 4, 'Buzz', 'Fizz', 7, 8, 'Fizz', 'Buzz', 11, 'Fizz', 13,
[1, 2, 'Fizz', 4, 'Buzz', 'Fizz', 7, 8, 'Fizz', 'Buzz', 11, 'Fizz', 13,
14, 'FizzBuzz']
"""

@@ -31,7 +31,7 @@ def fizz_buzz(n: int) -> list:
n (int): The upper limit of the range (inclusive) to generate.
Returns:
list: A list containing the numbers and/or string representations according
list: A list containing the numbers and/or string representations according
to the rules.
Raises:
@@ -41,7 +41,7 @@ def fizz_buzz(n: int) -> list:
Example:
>>> fizz_buzz(10)
[1, 2, 'Fizz', 4, 'Buzz', 'Fizz', 7, 8, 'Fizz', 'Buzz']
"""
# Defensive assertions
if not isinstance(n, int):
@@ -51,11 +51,13 @@ def fizz_buzz(n: int) -> list:
results = [] # Initialize an empty list to store the results
for i in range(1, n + 1):
if i % 3 == 0 and i % 5 == 0:
results.append("FizzBuzz") # Append "FizzBuzz" for multiples of both 3 and 5
results.append(
"FizzBuzz"
) # Append "FizzBuzz" for multiples of both 3 and 5
elif i % 3 == 0:
results.append("Fizz") # Append "Fizz" for multiples of 3
results.append("Fizz") # Append "Fizz" for multiples of 3
elif i % 5 == 0:
results.append("Buzz") # Append "Buzz" for multiples of 5
results.append("Buzz") # Append "Buzz" for multiples of 5
else:
results.append(i) # Append the number itself if not a multiple of 3 or 5
results.append(i) # Append the number itself if not a multiple of 3 or 5
return results # Return the list of results
51 changes: 45 additions & 6 deletions solutions/tests/test_fizz_buzz.py
Original file line number Diff line number Diff line change
@@ -20,8 +20,21 @@ class TestFizzBuzz(unittest.TestCase):
def test_fizzbuzz_15(self):
"""Test FizzBuzz for n = 15."""
expected = [
1, 2, "Fizz", 4, "Buzz", "Fizz", 7, 8, "Fizz",
"Buzz", 11, "Fizz", 13, 14, "FizzBuzz"
1,
2,
"Fizz",
4,
"Buzz",
"Fizz",
7,
8,
"Fizz",
"Buzz",
11,
"Fizz",
13,
14,
"FizzBuzz",
]
self.assertEqual(fizz_buzz(15), expected)

@@ -60,10 +73,36 @@ def test_fizzbuzz_non_integer(self):
def test_fizzbuzz_large_number(self):
"""Test FizzBuzz for a larger number n = 30."""
expected = [
1, 2, "Fizz", 4, "Buzz", "Fizz", 7, 8, "Fizz",
"Buzz", 11, "Fizz", 13, 14, "FizzBuzz", 16,
17, "Fizz", 19, "Buzz", "Fizz", 22, 23, "Fizz",
"Buzz", 26, "Fizz", 28, 29, "FizzBuzz"
1,
2,
"Fizz",
4,
"Buzz",
"Fizz",
7,
8,
"Fizz",
"Buzz",
11,
"Fizz",
13,
14,
"FizzBuzz",
16,
17,
"Fizz",
19,
"Buzz",
"Fizz",
22,
23,
"Fizz",
"Buzz",
26,
"Fizz",
28,
29,
"FizzBuzz",
]
self.assertEqual(fizz_buzz(30), expected)

0 comments on commit b84f300

Please sign in to comment.