Partition Problem

Donovan Hide donovanhide at bigfoot.com
Mon Jul 16 12:46:01 EDT 2001


Hi,
    thanks for the input, newsgroups are great! After scratching my head
last night, I came up with a non-recursive algorithm which works for
specific values of k (6 in the following example), as follows:

from math import *

def P(n,k):
    res=[]
    for a in range (n-k+1,int(ceil(n/k)-1),-1):
        for b in range(a,0,-1):
            for c in range(b,0,-1):
                for d in range(c,0,-1):
                    for e in range(d,0,-1):
                        for f in range(e,0,-1):
                            if (a+b+c+d+e+f)==n:
                                res.append([a,b,c,d,e,f])

    return res

a=P(20,6)
for x in range(0,len(a)):
    print a[x]

[15, 1, 1, 1, 1, 1]
[14, 2, 1, 1, 1, 1]
[13, 3, 1, 1, 1, 1]
......
......
[4, 4, 4, 4, 2, 2]
[4, 4, 4, 3, 3, 2]
[4, 4, 3, 3, 3, 3]

    I'm sure it is possible to make this recursive, but I'm going to explore
Alex's, Emile's and Arne's versions first.
    Another aspect of this problem I'm trying to do, is to find all the
permutations of 20 and also 17. Is there a module available fo such standard
combinatoric questions? Emile mentions Tim's klistcombs, is this a snippet
from a library of functions?
    Thanks for all the help, it's making learning python really interesting.
My ultimate aim is to prototype an animation doping system for the
traditional practice of line-testing. I work for an animation company and
there is a serious dearth of effective, simple software out there for this
purpose. Any comments or experience of image file handling with Python? PIL
and ImageMagick look like the obvious routes to follow. More specifically,
has anyone linked python to the vidcap API of MS Windows. This would be
useful for capturing frames of animation through a video camera.
    Anyway,
    a pint is waiting for all of you in your pub of choice (so long as it is
in London!)
    Cheers,
    Donovan Hide.






More information about the Python-list mailing list