combinations_of_sum moved to new itertools module
This commit is contained in:
parent
5d5e9fb8e5
commit
600d0e716c
@ -1,5 +1,5 @@
|
|||||||
from math import factorial
|
from math import factorial
|
||||||
from typing import Sized
|
from typing import Sized, Iterator
|
||||||
|
|
||||||
|
|
||||||
def len_combinations(iterable: Sized, r: int) -> int:
|
def len_combinations(iterable: Sized, r: int) -> int:
|
||||||
@ -18,3 +18,15 @@ def len_permutations(iterable: Sized, r: int) -> int:
|
|||||||
return 0
|
return 0
|
||||||
else:
|
else:
|
||||||
return factorial(n) // factorial(n - r)
|
return factorial(n) // factorial(n - r)
|
||||||
|
|
||||||
|
|
||||||
|
def 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 combinations_of_sum(total_sum - value, length - 1, min_value):
|
||||||
|
yield (value,) + permutation
|
||||||
|
|||||||
@ -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, Iterator
|
from typing import Any
|
||||||
|
|
||||||
|
|
||||||
class Cache(dict):
|
class Cache(dict):
|
||||||
@ -46,9 +46,7 @@ class Dict(dict):
|
|||||||
self[k] = Dict(self[k])
|
self[k] = Dict(self[k])
|
||||||
elif isinstance(self[k], list):
|
elif isinstance(self[k], list):
|
||||||
for i in range(len(self[k])):
|
for i in range(len(self[k])):
|
||||||
if isinstance(self[k][i], dict) and not isinstance(
|
if isinstance(self[k][i], dict) and not isinstance(self[k][i], Dict):
|
||||||
self[k][i], Dict
|
|
||||||
):
|
|
||||||
self[k][i] = Dict(self[k][i])
|
self[k][i] = Dict(self[k][i])
|
||||||
|
|
||||||
def update(self, other: dict, **kwargs):
|
def update(self, other: dict, **kwargs):
|
||||||
@ -166,18 +164,6 @@ 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