From 94637486435b6c577bf8d72271e9a38031ed1a96 Mon Sep 17 00:00:00 2001 From: Ethan Sequeira Date: Thu, 19 Nov 2026 18:22:38 +0000 Subject: [PATCH] Add fizzbuzz CLI command (closes #93) --- fixit/cli.py | 10 +++++++ fixit/fizzbuzz.py | 26 ++++++++++++++++++ tests/test_fizzbuzz.py | 62 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 fixit/fizzbuzz.py create mode 100644 tests/test_fizzbuzz.py diff --git a/fixit/cli.py b/fixit/cli.py index 049d01b..8a99931 100644 --- a/fixit/cli.py +++ b/fixit/cli.py @@ -14,6 +14,7 @@ coin, convert, health, + fizzbuzz, jsonfmt, lorem, password, @@ -158,6 +159,11 @@ def run_bmi(args: argparse.Namespace) -> None: print(f"{bmi:.1f} {category}") +def run_fizzbuzz(args: argparse.Namespace) -> None: + """Print FizzBuzz values from 1 to n.""" + for item in fizzbuzz.fizzbuzz(args.n): + print(item) + COMMAND_TABLE = { "wc": run_wc, "convert": run_convert, @@ -175,6 +181,7 @@ def run_bmi(args: argparse.Namespace) -> None: "lorem": run_lorem, "coin": run_coin, "bmi": run_bmi, + "fizzbuzz": run_fizzbuzz, } @@ -270,6 +277,9 @@ def build_parser() -> argparse.ArgumentParser: bmi.add_argument("--kg", type=finite_float, required=True, help="weight in kilograms") bmi.add_argument("--cm", type=finite_float, required=True, help="height in centimeters") + fb = sub.add_parser("fizzbuzz", help="print FizzBuzz sequence up to n") + fb.add_argument("n", type=int, help="upper limit (must be at least 1)") + return parser diff --git a/fixit/fizzbuzz.py b/fixit/fizzbuzz.py new file mode 100644 index 0000000..142239f --- /dev/null +++ b/fixit/fizzbuzz.py @@ -0,0 +1,26 @@ +"""FizzBuzz sequence generator.""" + +from __future__ import annotations + + +def fizzbuzz(n: int) -> list[str]: + """Return the FizzBuzz sequence from 1 to n inclusive. + + Multiples of 3 and 5 become "FizzBuzz", multiples of 3 become "Fizz", + multiples of 5 become "Buzz", and other numbers become their string form. + Raises ValueError if n is less than 1. + """ + if n < 1: + raise ValueError("n must be at least 1") + + result: list[str] = [] + for number in range(1, n + 1): + if number % 3 == 0 and number % 5 == 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 \ No newline at end of file diff --git a/tests/test_fizzbuzz.py b/tests/test_fizzbuzz.py new file mode 100644 index 0000000..2997541 --- /dev/null +++ b/tests/test_fizzbuzz.py @@ -0,0 +1,62 @@ +import pytest + +from fixit.cli import main +from fixit.fizzbuzz import fizzbuzz + + +def test_fizzbuzz_normal_fifteen(): + assert fizzbuzz(15) == [ + "1", + "2", + "Fizz", + "4", + "Buzz", + "Fizz", + "7", + "8", + "Fizz", + "Buzz", + "11", + "Fizz", + "13", + "14", + "FizzBuzz", + ] + + +def test_fizzbuzz_single_element(): + assert fizzbuzz(1) == ["1"] + + +def test_fizzbuzz_rejects_zero(): + with pytest.raises(ValueError, match="at least 1"): + fizzbuzz(0) + + +def test_fizzbuzz_rejects_negative(): + with pytest.raises(ValueError, match="at least 1"): + fizzbuzz(-5) + + +def test_fizzbuzz_command_prints_lines(capsys): + code = main(["fizzbuzz", "5"]) + captured = capsys.readouterr() + + assert code == 0 + assert captured.out.splitlines() == ["1", "2", "Fizz", "4", "Buzz"] + assert captured.err == "" + + +def test_fizzbuzz_command_rejects_less_than_one(capsys): + code = main(["fizzbuzz", "0"]) + captured = capsys.readouterr() + + assert code == 1 + assert "at least 1" in captured.err + + +def test_fizzbuzz_command_rejects_non_integer(): + with pytest.raises(SystemExit) as exc_info: + main(["fizzbuzz", "abc"]) + + assert exc_info.value.code == 2 \ No newline at end of file