NEW: itertools.len_permutations() and itertools.len_combinations() -> get the amount of results the respective itertools function would yield

This commit is contained in:
Stefan Harmuth 2023-12-25 11:24:12 +01:00
parent 1e43a2ff0d
commit 4d1b075086

20
src/tools/itertools.py Normal file
View File

@ -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)