Combi is a Pythonic package for combinatorics.
Combi lets you explore spaces of permutations and combinations as if they were Python sequences, but without generating all the permutations/combinations in advance. It lets you specify a lot of special conditions on these spaces. It also provides a few more classes that might be useful in combinatorics programming.
Combi documentation: https://combi.readthedocs.org/en/stable/
Combi on GitHub: https://github.com/cool-RR/combi
Combi on PyPI: https://pypi.python.org/pypi/combi
Use PermSpace
to create a permutation space:
>>> from combi import *
>>> perm_space = PermSpace('meow')
It behaves like a sequence:
>>> len(perm_space)
24
>>> perm_space[7]
<Perm: ('e', 'm', 'w', 'o')>
>>> perm_space.index('mowe')
3
And yet the permutations are created on-demand rather than in advance.
Use CombSpace
to create a combination space, where order doesn’t
matter:
>>> comb_space = CombSpace(('vanilla', 'chocolate', 'strawberry'), 2)
>>> comb_space
<CombSpace: ('vanilla', 'chocolate', 'strawberry'), n_elements=2>
>>> comb_space[2]
<Comb, n_elements=2: ('chocolate', 'strawberry')>
>>> len(comb_space)
3
For more details, try the tutorial or see the documentation contents.
PermSpace
lets you explore a space of permutations as if it was a
Python sequence.PermSpace
will notice if you have repeating elements in your
sequence, and treat all occurences of the same value as interchangable
rather than create redundant permutations.CombSpace
lets you explore a space of combinations as if it was a
Python sequence.MapSpace
is like Python’s built-in map()
, except it’s a
sequence that allows index access.ProductSpace
is like Python’s itertools.product()
, except
it’s a sequence that allows index access.ChainSpace
is like Python’s itertools.chain()
, except
it’s a sequence that allows index access.SelectionSpace
is a space of all selections from a sequence, of all
possible lengths.Bag
class is a multiset like Python’s
collections.Counter
, except it offers far more functionality, like
more arithmetic operations between bags,
comparison between bags, and more. (It can do that
because unlike Python’s collections.Counter
, it only allows natural
numbers as keys.)FrozenBag
, OrderedBag
and
FrozenOrderedBag
are provided, which are variations on Bag
.Combi on GitHub: https://github.com/cool-RR/combi Feel free to fork and send pull requests!
There are three Combi groups, a.k.a. mailing lists:
Combi was created by Ram Rachum. I provide Development services in Python and Django.