Skip to content
Advertisement

Tag: combinatorics

All tuples of positive integers

How do I create a generator that will return tuples of all combinations of positive integers, example for generating triplets. Answer This code uses a similar approach to Paul Hankin’s, but it’s more general since it will generate tuples of any desired width, not just 3. output The algorithm for compositions was derived from the technique used for counting the

The Assignment Problem, a NumPy function?

Since an assignment problem can be posed in the form of a single matrix, I am wondering if NumPy has a function to solve such a matrix. So far I have found none. Maybe one of you guys know if NumPy/SciPy has an assignment-problem-solve function? Edit: In the meanwhile I have found a Python (not NumPy/SciPy) implementation at http://software.clapper.org/munkres/. Still

How do I generate all permutations of a list?

How do I generate all the permutations of a list? For example: Answer Use itertools.permutations from the standard library: Adapted from here is a demonstration of how itertools.permutations might be implemented: A couple of alternative approaches are listed in the documentation of itertools.permutations. Here’s one: And another, based on itertools.product:

Advertisement