Let's say I have a set with n=3 elements: [a,b,c]
Using combinatorics we know that this set has 8 subsets with j elements:
[∅], [a], [b], [c], [a,b], [a,c], [b,c], [a,b,c]
Now what I want to do using Python is print all of the permutations of these 8 subsets with the constraints being that there must be exactly 3 subsets for each permutation (empty subsets are fine), all elements must be used, and alphabetical order must be maintained within each subset (alphabetical order does not need to be maintained outside of the subset--e.g. [c],[a],[b]
is fine).
I have attempted something like this:
x=1
y=3
for i in set(permutations(chain.from_iterable(set_n))):
while x<=3:
permuted = sorted([i[:y], i[x:]])
x = x+1
y = y-1
print permuted
Where set_n
is my set of n elements, but this of course only gives permutations of two subsets and only a single permutation of those two subsets. It's also not maintaining alphabetical order within the subsets.