NEW: itertools.len_permutations() and itertools.len_combinations() -> get the amount of results the respective itertools function would yield
This commit is contained in:
parent
1e43a2ff0d
commit
4d1b075086
20
src/tools/itertools.py
Normal file
20
src/tools/itertools.py
Normal 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)
|
||||
Loading…
Reference in New Issue
Block a user