Processing math: 100%
Ask Your Question
0

dividing range by combinations

asked 5 years ago

parkjr gravatar image

The following is an example:

temp = range(5)
L = [2,1,2]

Then we know that

5C2×3C1×2C2=30

We can get every combinations as following:

result = []
for A in Combinations(temp, L[0]):
    temp = list(set(temp) - set(A))
    for B in Combinations(temp, L[1]):
        temp = list(set(temp) - set(B))
        result += [[A,B, temp]]
        temp = list(set(range(5))- set(A))
    temp = range(5)

Then result is

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

The problem is that 'How can I get the general case?'

temp = range(k)

L = [e1, e2, , en ], where ni=1ei=k

I don't know the size of list L.

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
0

answered 4 years ago

dan_fulea gravatar image

Here is a possible recursive (rather lazy) implementation without using specialized iter tools.

def mySplitIntoPieces(S, L):
    """S is a list of "elements", 
    L is a list of positive integers with sum(L) = len(S),
    we want to return the list of all tuples
    [ S0, S1, S2, ... ]
    where S0, S1, S2, ... are a partition of S and 
    len(S0) = L[0],
    len(S1) = L[1],
    len(S2) = L[2],
    and so on.
    """

    if len(S) != sum(L):
        return

    if len(L) == 0:
        return [ S ]    # the partition of S in one piece

    return [ [ S0, ] + furtherPieces
             for S0 in Combinations( S, L[0] )
             for furtherPieces in mySplitIntoPieces( list(set(S).difference(S0)), L[1:] ) ]

Let us test the functionality:

sage: for pieces in mySplitIntoPieces( [0..4], [2,1,2] ): 
....:     print(pieces)                                                                                                                                                                               
[[0, 1], [2], [3, 4]]
[[0, 1], [3], [2, 4]]
[[0, 1], [4], [2, 3]]
[[0, 2], [1], [3, 4]]
[[0, 2], [3], [1, 4]]
[[0, 2], [4], [1, 3]]
[[0, 3], [1], [2, 4]]
[[0, 3], [2], [1, 4]]
[[0, 3], [4], [1, 2]]
[[0, 4], [1], [2, 3]]
[[0, 4], [2], [1, 3]]
[[0, 4], [3], [1, 2]]
[[1, 2], [0], [3, 4]]
[[1, 2], [3], [0, 4]]
[[1, 2], [4], [0, 3]]
[[1, 3], [0], [2, 4]]
[[1, 3], [2], [0, 4]]
[[1, 3], [4], [0, 2]]
[[1, 4], [0], [2, 3]]
[[1, 4], [2], [0, 3]]
[[1, 4], [3], [0, 2]]
[[2, 3], [0], [1, 4]]
[[2, 3], [1], [0, 4]]
[[2, 3], [4], [0, 1]]
[[2, 4], [0], [1, 3]]
[[2, 4], [1], [0, 3]]
[[2, 4], [3], [0, 1]]
[[3, 4], [0], [1, 2]]
[[3, 4], [1], [0, 2]]
[[3, 4], [2], [0, 1]]
sage:
Preview: (hide)
link

Comments

You showed me the perfect answer!!! Thanks:)

parkjr gravatar imageparkjr ( 4 years ago )

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 5 years ago

Seen: 696 times

Last updated: Apr 13 '20