What is said works, I believe...see the docstring

def kbin(l, k, ordered=True):
    """
    Return sequence ``l`` partitioned into ``k`` bins.
    If ordered is True then the order of the items in the
    flattened partition will be the same as the order of the
    items in ``l``; if False, all permutations of the items will
    be given; if None, only unique permutations for a given
    partition will be given.

    Examples
    ========

    >>> from sympy.utilities.iterables import kbin
    >>> for p in kbin(range(3), 2):
    ...     print p
    ...
    [[0], [1, 2]]
    [[0, 1], [2]]
    >>> for p in kbin(range(3), 2, ordered=False):
    ...     print p
    ...
    [(0,), (1, 2)]
    [(0,), (2, 1)]
    [(1,), (0, 2)]
    [(1,), (2, 0)]
    [(2,), (0, 1)]
    [(2,), (1, 0)]
    [(0, 1), (2,)]
    [(0, 2), (1,)]
    [(1, 0), (2,)]
    [(1, 2), (0,)]
    [(2, 0), (1,)]
    [(2, 1), (0,)]
    >>> for p in kbin(range(3), 2, ordered=None):
    ...     print p
    ...
    [[0], [1, 2]]
    [[1], [2, 0]]
    [[2], [0, 1]]
    [[0, 1], [2]]
    [[1, 2], [0]]
    [[2, 0], [1]]

    """
    from sympy.utilities.iterables import partitions, permutations
    def rotations(seq):
        for i in range(len(seq)):
            yield seq
            seq.append(seq.pop(0))
    if ordered is None:
        func = rotations
    else:
        func = permutations
    for p in partitions(len(l), k):
        if sum(p.values()) != k:
            continue
        for pe in permutations(p.keys()):
            rv = []
            i = 0
            for part in pe:
                for do in range(p[part]):
                    j = i + part
                    rv.append(l[i: j])
                    i = j
            if ordered:
                yield rv
            else:
                template = [len(i) for i in rv]
                for pp in func(l):
                    rvp = []
                    ii = 0
                    for t in template:
                        jj = ii + t
                        rvp.append(pp[ii: jj])
                        ii = jj
                    yield rvp

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sympy?hl=en.

Reply via email to