From 6159d12cff5be3e564924ef5d04490dd029a0c02 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Sat, 30 Nov 2019 11:53:54 +0100 Subject: [PATCH] fizzbuzz: add working fizzbuzz implementation --- fizzbuzz/fizzbuzz.py | 13 +++++++++++- fizzbuzz/test_fizzbuzz.py | 42 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/fizzbuzz/fizzbuzz.py b/fizzbuzz/fizzbuzz.py index 9452166..fd586c0 100755 --- a/fizzbuzz/fizzbuzz.py +++ b/fizzbuzz/fizzbuzz.py @@ -2,8 +2,19 @@ def fizzbuzz(max: int = 100) -> None: + words = { + 3: "fizz", + 5: "buzz", + } for i in range(1, max + 1): - print(i) + out = [] + for div, word in words.items(): + if i % div == 0: + out.append(word) + if len(out) > 0: + print("".join(out)) + else: + print(i) if __name__ == "__main__": diff --git a/fizzbuzz/test_fizzbuzz.py b/fizzbuzz/test_fizzbuzz.py index 30b304d..7e2a75c 100644 --- a/fizzbuzz/test_fizzbuzz.py +++ b/fizzbuzz/test_fizzbuzz.py @@ -1,7 +1,43 @@ from fizzbuzz import fizzbuzz -def test_fizzbuzz_counts_to_two(capsys): - fizzbuzz(2) +def list_output(max, capsys, expected_list): + fizzbuzz(max) out, __ = capsys.readouterr() - assert out == "1\n2\n" + assert out == "\n".join(map(lambda x: str(x), expected_list)) + "\n" + + +def test_fizzbuzz_counts_to_two(capsys): + list_output(2, capsys, [1, 2]) + + +def test_fizzbuzz_shows_fizz_on_three(capsys): + list_output(3, capsys, [1, 2, "fizz"]) + + +def test_fizzbuzz_shows_buzz_on_five(capsys): + list_output(5, capsys, [1, 2, "fizz", 4, "buzz"]) + + +def test_fizzbuzz_shows_fizzbuzz_on_fifteen(capsys): + list_output( + 15, + capsys, + [ + 1, + 2, + "fizz", + 4, + "buzz", + "fizz", + 7, + 8, + "fizz", + "buzz", + 11, + "fizz", + 13, + 14, + "fizzbuzz", + ], + )