From 4d1b075086cf59c10c8d8c5fdf7cea133818203a Mon Sep 17 00:00:00 2001 From: Stefan Harmuth Date: Mon, 25 Dec 2023 11:24:12 +0100 Subject: [PATCH] NEW: itertools.len_permutations() and itertools.len_combinations() -> get the amount of results the respective itertools function would yield --- src/tools/itertools.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/tools/itertools.py diff --git a/src/tools/itertools.py b/src/tools/itertools.py new file mode 100644 index 0000000..8e36214 --- /dev/null +++ b/src/tools/itertools.py @@ -0,0 +1,20 @@ +from math import factorial +from typing import Sized + + +def len_combinations(iterable: Sized, r: int) -> int: + """How many options will itertools.combinations(iterable, r) yield?""" + n = len(iterable) + if r > n: + return 0 + else: + return factorial(n) // factorial(r) // factorial(n - r) + + +def len_permutations(iterable: Sized, r: int) -> int: + """How many options will itertools.permutations(iterable, r) yield?""" + n = len(iterable) + if r > n: + return 0 + else: + return factorial(n) // factorial(n - r)