Python and Combinatorics

Peter Otten __peter__ at web.de
Tue May 16 17:32:00 EDT 2006


Nic wrote:

> In my example I've chosen the number 3.
> How should I change the Python code in order to select another number
> (e.g. 7)?

Here is a parameterized render().

def render(w, h, suffixes="ab"):
    pairs = list(unique(range(1, h+1), 2))
    for item in unique(pairs, w):
        for suffix in repeat(*[suffixes]*w):
            yield tuple((a, b, s) for (a, b), s in izip(item, suffix))

if __name__ == "__main__":
    for item in render(3, 4, "abc"):
        print " ".join("%s%s%s" % t for t in item)

Experiment with the numbers to see the effects

Peter



More information about the Python-list mailing list