fizzbuzz: always order fizzbuzzer output words

This commit is contained in:
Bruno BELANYI 2019-11-30 12:08:18 +01:00
parent a48f7877d9
commit f9a9ffaaf5
2 changed files with 24 additions and 1 deletions

View file

@ -6,7 +6,7 @@ def fizzbuzzer(words: Dict[int, str]) -> Callable[[int], None]:
def _fun(max: int) -> None: def _fun(max: int) -> None:
for i in range(1, max + 1): for i in range(1, max + 1):
out = [] out = []
for div, word in words.items(): for div, word in sorted(words.items()):
if i % div == 0: if i % div == 0:
out.append(word) out.append(word)
if len(out) > 0: if len(out) > 0:

View file

@ -68,3 +68,26 @@ def test_can_foobarbazz_customization(capsys):
"foobarbazz", "foobarbazz",
], ],
) )
def test_can_foobarbazz_customization_regardless_of_dict_order(capsys):
foobarbazz = fizzbuzzer({4: "bazz", 3: "bar", 2: "foo"})
list_output(
function=foobarbazz,
max=12,
capsys=capsys,
expected_list=[
1,
"foo",
"bar",
"foobazz",
5,
"foobar",
7,
"foobazz",
"bar",
"foo",
11,
"foobarbazz",
],
)