fizzbuzz: add working fizzbuzz implementation

This commit is contained in:
Bruno BELANYI 2019-11-30 11:53:54 +01:00
parent bbba7507c6
commit 6159d12cff
2 changed files with 51 additions and 4 deletions

View file

@ -2,7 +2,18 @@
def fizzbuzz(max: int = 100) -> None: def fizzbuzz(max: int = 100) -> None:
words = {
3: "fizz",
5: "buzz",
}
for i in range(1, max + 1): for i in range(1, max + 1):
out = []
for div, word in words.items():
if i % div == 0:
out.append(word)
if len(out) > 0:
print("".join(out))
else:
print(i) print(i)

View file

@ -1,7 +1,43 @@
from fizzbuzz import fizzbuzz from fizzbuzz import fizzbuzz
def test_fizzbuzz_counts_to_two(capsys): def list_output(max, capsys, expected_list):
fizzbuzz(2) fizzbuzz(max)
out, __ = capsys.readouterr() 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",
],
)