NEW: itertools.len_combinations_of_sum()

This commit is contained in:
Stefan Harmuth 2023-12-26 12:49:37 +01:00
parent 600d0e716c
commit f7d1fde5b7

View File

@ -1,4 +1,4 @@
from math import factorial
from math import factorial, comb
from typing import Sized, Iterator
@ -30,3 +30,12 @@ def combinations_of_sum(total_sum: int, length: int = None, min_value: int = 0)
for value in range(min_value, total_sum + 1):
for permutation in combinations_of_sum(total_sum - value, length - 1, min_value):
yield (value,) + permutation
def len_combinations_of_sum(total_sum: int, length: int = None, min_value: int = 0) -> int:
"""
How many options will combinations_of_sum(total_sum, length) yield?
No idea how to factor in min_value, yet, so if using min_value, the answer will always be too high
"""
return comb(total_sum + length - 1, total_sum)