diff --git a/solutions/fizz_buzz.py b/solutions/fizz_buzz.py index 36daead22..e234075bd 100644 --- a/solutions/fizz_buzz.py +++ b/solutions/fizz_buzz.py @@ -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 diff --git a/solutions/tests/test_fizz_buzz.py b/solutions/tests/test_fizz_buzz.py index ab526fdaf..c24280a5c 100644 --- a/solutions/tests/test_fizz_buzz.py +++ b/solutions/tests/test_fizz_buzz.py @@ -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)