Iterator tools¶
The iterator tools itertools module especially regroups several combinatoric iterators
which can be useful.
Exercise (easy)
In the project assignment exercise, the student picking order matters. The first to pick is certain to get his preferred choice, the second is certain to get at least his second preferred, etc Create a generator which yields every picking order possible.
Correction
src¶
1def yield_all_possible_choosers_order(
2 choosers: Choosers,
3) -> Generator[Choosers]:
4 all_possible_chooser_order = itertools.permutations(choosers)
5 yield from all_possible_chooser_order
tests¶
1def test_yield_all_possible_choosers_order() -> None:
2 choosers: Choosers = (10, 11, 12)
3 assert set(yield_all_possible_choosers_order(choosers)) == {
4 (10, 11, 12),
5 (10, 12, 11),
6 (11, 10, 12),
7 (11, 12, 10),
8 (12, 10, 11),
9 (12, 11, 10),
10 }