sprig.comb module

A collection of utils for doing combinatorics.

class sprig.comb.Combinations(s: Iterable[T], k: int)

Bases: Generic[sprig.comb.T]

Create an object that represent a collection of combinations.

Useful for slicing or sampling from series too long to iterate over with itertools.combinations.

caches: Dict[int, sprig.comb.IntervalCache[Tuple[int, int]]] = {}
static clear_cache()
gen_combinadic(index: int)Iterator[int]

Compute the index’th combinadic.

e.g. 4 = 4 choose 3 + 1 choose 2 + 0 choose 1, thus

>>> tuple(Combinations(range(5), 3).gen_combinadic(4))
(4, 1, 0)
static max_n_choose_k_below_limit(n: int, k: int, limit: int)Tuple[int, int]

Compute the largest n s.t. n choose k < limit. Return also the corresponding value of n choose k for optimization.

e.g. 5 choose 3 = 10 > 5 but 4 choose 3 = 4 < 5, thus

>>> Combinations.max_n_choose_k_below_limit(5, 3, 5)
(4, 4)
class sprig.comb.IntervalCache

Bases: Generic[sprig.comb.T]

get(item: float)T
set(lower: float, upper: float, value: T)None
sprig.comb.comb(n: int, k: int)int

Compute binomial coefficient n choose k