Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

dividing range by combinations

The following is an example:

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

Then we know that

$_5\rm C_2 \times _3\rm C_1 \times _2\rm C_2 = 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 = [$e_1$, $e_2$, $\cdots$, $e_n$ ], where $\sum_{i=1} ^n e_i = k$

I don't know the size of list L.