Given a set = {1, 2, 3, 4, 5, 6} and a number n = 2, how can i get all the disjointed subsets like [ [{1, 2}, {3, 4}, {5, 6}], [{1,3}, {2, 4}, {5, 6}], etc..]
They have to be disjointed 3 by 3 ( 6/2 = 3 ; i.e. all numbers in the original set must be used, and the cardinal of subsets must be equal to each other)
I found this code and I kept trying to modify it in order to show only those with 2 (as an example) elements, but didn't reach an end
Any help?
import itertools
def partition(list_, k):
#partition([1, 2, 3, 4], 2) -> [[1], [2, 3, 4]], [[1, 2], [3, 4]], ..., [[1, 3], [2, 4]], [[3], [1, 2, 4]]
if k == 1: # base case 1: k == 1, just yield itself as a list
yield [list_]
elif k == len(list_): # base case 2: k == len(list_), yield each item in the list wrapped in a list
yield [[s] for s in list_]
else:
head, *tail = list_
# head, *tail = list_ # head = the first element, tail = the rest
for p in partition(tail, k-1): # case 1: head -> 1, partition(tail, k-1) -> k-1.
# head + partition(tail, k-1) -> 1+k-1 -> k
yield [[head], *p]
for p in partition(tail, k): # case 2: head -> 1, partition(tail, k) -> k.
# bloat x to [e1, e2, e3] -> [[x+e1, e2, e3], [e1, x+e2, e3], [e1, e2, x+e3]]
for i in range(len(p)):
yield p[:i] + [[head] + p[i]] + p[i+1:] # bloat head to partition(tail, k) -> k