From f7d1fde5b77b9f162118d025c4f7a9ee9a4ff6ea Mon Sep 17 00:00:00 2001 From: Stefan Harmuth Date: Tue, 26 Dec 2023 12:49:37 +0100 Subject: [PATCH] NEW: itertools.len_combinations_of_sum() --- src/tools/itertools.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/tools/itertools.py b/src/tools/itertools.py index d89b172..4835464 100644 --- a/src/tools/itertools.py +++ b/src/tools/itertools.py @@ -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)