set partitioning

Michael Ekstrand michael at elehack.net
Tue May 2 01:17:40 CEST 2006


On Mon, May 01, 2006 at 03:42:53PM -0700, hymort at hotmail.com wrote:
> Not quite what I'm looking for.  I would like a list of all partitions
> with each partition having k or less elements, not just one instance.

def partition(S, k):
    parts = []
    ct = 0
    cp = []
    for elem in S:
        if ct > k:
	    parts.append(cp)
	    cp = []
	    ct = 0
	cp.append(elem)
	ct += 1
    parts.append(cp)
    return parts

> > If this was a take home exam problem,
> > you should be ashamed of yourself!
> >    -- Aaron Watters

- Michael

-- 
mouse, n: a device for pointing at the xterm in which you want to type.
                -- Fortune
Visit me on the Web: http://www.elehack.net



More information about the Python-list mailing list