NEW tools.list_combinations_of_sum() - returns all possible tuples with len [length}, containing integers summing up to {total_sum}
This commit is contained in:
parent
1a111488ba
commit
ffcc1e14c0
@ -4,7 +4,7 @@ import os.path
|
|||||||
import sys
|
import sys
|
||||||
from fishhook import hook
|
from fishhook import hook
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
from typing import Any
|
from typing import Any, Iterator
|
||||||
|
|
||||||
|
|
||||||
class Cache(dict):
|
class Cache(dict):
|
||||||
@ -166,6 +166,18 @@ def cache(func):
|
|||||||
return new_func
|
return new_func
|
||||||
|
|
||||||
|
|
||||||
|
def list_combinations_of_sum(total_sum: int, length: int = None, min_value: int = 0) -> Iterator[tuple[int]]:
|
||||||
|
if length is None:
|
||||||
|
length = total_sum
|
||||||
|
|
||||||
|
if length == 1:
|
||||||
|
yield (total_sum,)
|
||||||
|
else:
|
||||||
|
for value in range(min_value, total_sum + 1):
|
||||||
|
for permutation in list_combinations_of_sum(total_sum - value, length - 1, min_value):
|
||||||
|
yield (value,) + permutation
|
||||||
|
|
||||||
|
|
||||||
@hook(list)
|
@hook(list)
|
||||||
def intersection(self, *args) -> list:
|
def intersection(self, *args) -> list:
|
||||||
ret = set(self).intersection(*args)
|
ret = set(self).intersection(*args)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user