combinatorics via __future__ generators

Phlip phlip2005 at gmail.com
Wed Nov 18 19:58:42 EST 2009


Python:

I have a quaint combinatorics problem. Before I solve it, or find a
solution among "generators", I thought y'all might like to show off
any solutions.

Given an array like this...

  [0, 4, 3]

Produce an array like this:

  [
    [0, 0, 0],
    [0, 1, 0],
    [0, 2, 0],
    [0, 3, 0],
    [0, 1, 1],
    [0, 2, 1],
    [0, 3, 1],
    [0, 1, 2],
    [0, 2, 2],
    [0, 3, 2],
]

The first array is the counts of options in 4 slots, and the second is
all combinations of indexes of each option, such that every option
associates once with every other option. The leading 0 simply
represents a slot with no options; the algorithm must preserve those.

This should be child's play for the generator package, right?

--
  Phlip
  http://zeekland.zeroplayer.com/



More information about the Python-list mailing list