Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions fixit/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
coin,
convert,
health,
fizzbuzz,
jsonfmt,
lorem,
password,
Expand Down Expand Up @@ -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,
Expand All @@ -175,6 +181,7 @@ def run_bmi(args: argparse.Namespace) -> None:
"lorem": run_lorem,
"coin": run_coin,
"bmi": run_bmi,
"fizzbuzz": run_fizzbuzz,


}
Expand Down Expand Up @@ -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


Expand Down
26 changes: 26 additions & 0 deletions fixit/fizzbuzz.py
Original file line number Diff line number Diff line change
@@ -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
62 changes: 62 additions & 0 deletions tests/test_fizzbuzz.py
Original file line number Diff line number Diff line change
@@ -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
Loading