From 804c8df0cd0eb0d832459651d6e664d3db73d1f5 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 8 Oct 2025 00:45:16 +0000 Subject: [PATCH] Refactor FizzBuzz to use generator-based implementation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Replace single-number fizzbuzz function with fizzbuzz_generator - Generator yields results for 1 to limit, providing memory efficiency - Update tests to work with generator using helper function - Add specific test for generator behavior - Maintain all existing functionality while improving performance 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- examples/fizzbuzz.py | 35 +++++++++++----------- tests/test_fizzbuzz.py | 67 +++++++++++++++++++++++++++--------------- 2 files changed, 61 insertions(+), 41 deletions(-) diff --git a/examples/fizzbuzz.py b/examples/fizzbuzz.py index 980bd11..f8219cf 100644 --- a/examples/fizzbuzz.py +++ b/examples/fizzbuzz.py @@ -2,39 +2,38 @@ """FizzBuzz example for Claude Code SDK.""" -def fizzbuzz(n: int) -> str: +def fizzbuzz_generator(limit: int): """ - Classic FizzBuzz implementation. + Generator-based FizzBuzz implementation for memory efficiency. - Returns "Fizz" for multiples of 3, "Buzz" for multiples of 5, + Yields "Fizz" for multiples of 3, "Buzz" for multiples of 5, "FizzBuzz" for multiples of both 3 and 5, and the number as a string otherwise. Args: - n: The number to check + limit: The upper limit of the sequence (inclusive) - Returns: - str: The FizzBuzz result for the number + Yields: + str: The FizzBuzz result for each number from 1 to limit """ - if n % 15 == 0: - return "FizzBuzz" - elif n % 3 == 0: - return "Fizz" - elif n % 5 == 0: - return "Buzz" - else: - return str(n) + for i in range(1, limit + 1): + output = "" + if i % 3 == 0: + output += "Fizz" + if i % 5 == 0: + output += "Buzz" + yield output or str(i) def print_fizzbuzz(limit: int = 100) -> None: """ - Print FizzBuzz sequence from 1 to limit. + Print FizzBuzz sequence from 1 to limit using generator. Args: limit: The upper limit of the sequence (inclusive) """ - print(f"=== FizzBuzz (1 to {limit}) ===") - for i in range(1, limit + 1): - print(fizzbuzz(i)) + print(f"=== FizzBuzz Generator (1 to {limit}) ===") + for result in fizzbuzz_generator(limit): + print(result) if __name__ == "__main__": diff --git a/tests/test_fizzbuzz.py b/tests/test_fizzbuzz.py index 627011e..0df123a 100644 --- a/tests/test_fizzbuzz.py +++ b/tests/test_fizzbuzz.py @@ -1,6 +1,13 @@ """Tests for FizzBuzz example.""" -from examples.fizzbuzz import fizzbuzz +from examples.fizzbuzz import fizzbuzz_generator + + +def get_fizzbuzz_result(n: int) -> str: + """Helper function to get FizzBuzz result for a specific number.""" + # Generate results up to n and return the nth result + results = list(fizzbuzz_generator(n)) + return results[n - 1] class TestFizzBuzz: @@ -8,32 +15,32 @@ class TestFizzBuzz: def test_fizzbuzz_regular_numbers(self): """Test that regular numbers return the number as a string.""" - assert fizzbuzz(1) == "1" - assert fizzbuzz(2) == "2" - assert fizzbuzz(4) == "4" - assert fizzbuzz(7) == "7" - assert fizzbuzz(8) == "8" + assert get_fizzbuzz_result(1) == "1" + assert get_fizzbuzz_result(2) == "2" + assert get_fizzbuzz_result(4) == "4" + assert get_fizzbuzz_result(7) == "7" + assert get_fizzbuzz_result(8) == "8" def test_fizzbuzz_multiples_of_3(self): """Test that multiples of 3 return 'Fizz'.""" - assert fizzbuzz(3) == "Fizz" - assert fizzbuzz(6) == "Fizz" - assert fizzbuzz(9) == "Fizz" - assert fizzbuzz(12) == "Fizz" + assert get_fizzbuzz_result(3) == "Fizz" + assert get_fizzbuzz_result(6) == "Fizz" + assert get_fizzbuzz_result(9) == "Fizz" + assert get_fizzbuzz_result(12) == "Fizz" def test_fizzbuzz_multiples_of_5(self): """Test that multiples of 5 return 'Buzz'.""" - assert fizzbuzz(5) == "Buzz" - assert fizzbuzz(10) == "Buzz" - assert fizzbuzz(20) == "Buzz" - assert fizzbuzz(25) == "Buzz" + assert get_fizzbuzz_result(5) == "Buzz" + assert get_fizzbuzz_result(10) == "Buzz" + assert get_fizzbuzz_result(20) == "Buzz" + assert get_fizzbuzz_result(25) == "Buzz" def test_fizzbuzz_multiples_of_15(self): """Test that multiples of 15 return 'FizzBuzz'.""" - assert fizzbuzz(15) == "FizzBuzz" - assert fizzbuzz(30) == "FizzBuzz" - assert fizzbuzz(45) == "FizzBuzz" - assert fizzbuzz(60) == "FizzBuzz" + assert get_fizzbuzz_result(15) == "FizzBuzz" + assert get_fizzbuzz_result(30) == "FizzBuzz" + assert get_fizzbuzz_result(45) == "FizzBuzz" + assert get_fizzbuzz_result(60) == "FizzBuzz" def test_fizzbuzz_edge_cases(self): """Test edge cases.""" @@ -56,11 +63,25 @@ class TestFizzBuzz: "FizzBuzz", ] - for i, expected_value in enumerate(expected, 1): - assert fizzbuzz(i) == expected_value + # Test using the generator for the sequence + results = list(fizzbuzz_generator(len(expected))) + for i, expected_value in enumerate(expected): + assert results[i] == expected_value def test_fizzbuzz_larger_numbers(self): """Test larger numbers to ensure pattern holds.""" - assert fizzbuzz(99) == "Fizz" # 99 = 3 * 33 - assert fizzbuzz(100) == "Buzz" # 100 = 5 * 20 - assert fizzbuzz(150) == "FizzBuzz" # 150 = 15 * 10 + assert get_fizzbuzz_result(99) == "Fizz" # 99 = 3 * 33 + assert get_fizzbuzz_result(100) == "Buzz" # 100 = 5 * 20 + assert get_fizzbuzz_result(150) == "FizzBuzz" # 150 = 15 * 10 + + def test_fizzbuzz_generator_behavior(self): + """Test that the generator produces the correct sequence.""" + # Test that we get a generator + gen = fizzbuzz_generator(5) + assert hasattr(gen, "__iter__") + assert hasattr(gen, "__next__") + + # Test the first 5 values + expected = ["1", "2", "Fizz", "4", "Buzz"] + results = list(fizzbuzz_generator(5)) + assert results == expected