Task
Given a set of n
unique elements and a multiset l
of positive numbers that add up to n
, find all the ways of partitioning those unique elements into disjoint sets with sizes given by the elements of l
.
(A multiset is a set that allows repeated elements)
The point here is that we are taking everything in the output as sets, so order doesn't matter in any place.
This may be simpler to understand if you check some test cases below.
Input
The multiset l
of positive integers that add up to n
. Note that the elements in l
may appear repeatedly. This might be given as
- a list
l = [1, 1, 2, 2, 3]
- arguments to a function
f(1, 1, 2, 2, 3)
- or any other sensible way of representing a multiset
You may assume the representation of l
is ordered.
You may also take a list/set/collection of n
distinct integers, or you may generate it depending on the sum of the elements in l
. If you generate it, you may use any n
distinct integers for the set to be partitioned but your remaining code should work for any set of n
integers. The set you use must be specified in your answer. Suggestions include:
- a range starting in
0
:[0, 1, ..., n-1]
- a range starting in
1
:[1, 2, ..., n]
Output
The different ways in which your collection can be split in sets with the given sizes. Your output should be unambiguous.
Reference implementation
You can find a vanilla python reference implementation here or you can try it online.
Test cases
I am using the set 0, ..., n-1
when the list adds up to n
. In the TIO link, that list is given explicitly to my function.
After the ->
, each line represents one possible way of splitting the set.
[1, 1, 1, 1, 1, 1, 1, 1] -> [
[[0], [1], [2], [3], [4], [5], [6], [7]]
]
[3] -> [
[[0, 1, 2]]
]
[1, 2] -> [
[[0], [1, 2]]
[[1], [0, 2]]
[[2], [0, 1]]
]
[2, 2] -> [
[[0, 1], [2, 3]]
[[0, 2], [1, 3]]
[[0, 3], [1, 2]]
]
[1, 1, 2] -> [
[[0], [1], [2, 3]]
[[0], [2], [1, 3]]
[[0], [3], [1, 2]]
[[1], [2], [0, 3]]
[[1], [3], [0, 2]]
[[2], [3], [0, 1]]
]
[1, 2, 2] -> [
[[0], [1, 2], [3, 4]]
[[0], [1, 3], [2, 4]]
[[0], [1, 4], [2, 3]]
[[1], [0, 2], [3, 4]]
[[1], [0, 3], [2, 4]]
[[1], [0, 4], [2, 3]]
[[2], [0, 1], [3, 4]]
[[2], [0, 3], [1, 4]]
[[2], [0, 4], [1, 3]]
[[3], [0, 1], [2, 4]]
[[3], [0, 2], [1, 4]]
[[3], [0, 4], [1, 2]]
[[4], [0, 1], [2, 3]]
[[4], [0, 2], [1, 3]]
[[4], [0, 3], [1, 2]]
]
[2, 2, 2] -> [
[[0, 1], [2, 3], [4, 5]]
[[0, 1], [2, 4], [3, 5]]
[[0, 1], [2, 5], [3, 4]]
[[0, 2], [1, 3], [4, 5]]
[[0, 2], [1, 4], [3, 5]]
[[0, 2], [1, 5], [3, 4]]
[[0, 3], [1, 2], [4, 5]]
[[0, 3], [1, 4], [2, 5]]
[[0, 3], [1, 5], [2, 4]]
[[0, 4], [1, 2], [3, 5]]
[[0, 4], [1, 3], [2, 5]]
[[0, 4], [1, 5], [2, 3]]
[[0, 5], [1, 2], [3, 4]]
[[0, 5], [1, 3], [2, 4]]
[[0, 5], [1, 4], [2, 3]]
]
[1, 2, 3] -> [
[[0], [1, 2], [3, 4, 5]]
[[0], [1, 3], [2, 4, 5]]
[[0], [1, 4], [2, 3, 5]]
[[0], [1, 5], [2, 3, 4]]
[[0], [2, 3], [1, 4, 5]]
[[0], [2, 4], [1, 3, 5]]
[[0], [2, 5], [1, 3, 4]]
[[0], [3, 4], [1, 2, 5]]
[[0], [3, 5], [1, 2, 4]]
[[0], [4, 5], [1, 2, 3]]
[[1], [0, 2], [3, 4, 5]]
[[1], [0, 3], [2, 4, 5]]
[[1], [0, 4], [2, 3, 5]]
[[1], [0, 5], [2, 3, 4]]
[[1], [2, 3], [0, 4, 5]]
[[1], [2, 4], [0, 3, 5]]
[[1], [2, 5], [0, 3, 4]]
[[1], [3, 4], [0, 2, 5]]
[[1], [3, 5], [0, 2, 4]]
[[1], [4, 5], [0, 2, 3]]
[[2], [0, 1], [3, 4, 5]]
[[2], [0, 3], [1, 4, 5]]
[[2], [0, 4], [1, 3, 5]]
[[2], [0, 5], [1, 3, 4]]
[[2], [1, 3], [0, 4, 5]]
[[2], [1, 4], [0, 3, 5]]
[[2], [1, 5], [0, 3, 4]]
[[2], [3, 4], [0, 1, 5]]
[[2], [3, 5], [0, 1, 4]]
[[2], [4, 5], [0, 1, 3]]
[[3], [0, 1], [2, 4, 5]]
[[3], [0, 2], [1, 4, 5]]
[[3], [0, 4], [1, 2, 5]]
[[3], [0, 5], [1, 2, 4]]
[[3], [1, 2], [0, 4, 5]]
[[3], [1, 4], [0, 2, 5]]
[[3], [1, 5], [0, 2, 4]]
[[3], [2, 4], [0, 1, 5]]
[[3], [2, 5], [0, 1, 4]]
[[3], [4, 5], [0, 1, 2]]
[[4], [0, 1], [2, 3, 5]]
[[4], [0, 2], [1, 3, 5]]
[[4], [0, 3], [1, 2, 5]]
[[4], [0, 5], [1, 2, 3]]
[[4], [1, 2], [0, 3, 5]]
[[4], [1, 3], [0, 2, 5]]
[[4], [1, 5], [0, 2, 3]]
[[4], [2, 3], [0, 1, 5]]
[[4], [2, 5], [0, 1, 3]]
[[4], [3, 5], [0, 1, 2]]
[[5], [0, 1], [2, 3, 4]]
[[5], [0, 2], [1, 3, 4]]
[[5], [0, 3], [1, 2, 4]]
[[5], [0, 4], [1, 2, 3]]
[[5], [1, 2], [0, 3, 4]]
[[5], [1, 3], [0, 2, 4]]
[[5], [1, 4], [0, 2, 3]]
[[5], [2, 3], [0, 1, 4]]
[[5], [2, 4], [0, 1, 3]]
[[5], [3, 4], [0, 1, 2]]
]
[1, 1, 4] -> [
[[0], [1], [2, 3, 4, 5]]
[[0], [2], [1, 3, 4, 5]]
[[0], [3], [1, 2, 4, 5]]
[[0], [4], [1, 2, 3, 5]]
[[0], [5], [1, 2, 3, 4]]
[[1], [2], [0, 3, 4, 5]]
[[1], [3], [0, 2, 4, 5]]
[[1], [4], [0, 2, 3, 5]]
[[1], [5], [0, 2, 3, 4]]
[[2], [3], [0, 1, 4, 5]]
[[2], [4], [0, 1, 3, 5]]
[[2], [5], [0, 1, 3, 4]]
[[3], [4], [0, 1, 2, 5]]
[[3], [5], [0, 1, 2, 4]]
[[4], [5], [0, 1, 2, 3]]
]
This is the fifth and final challenge of the RGS Golfing Showdown. If you want to participate in the competition, you have 96 hours to submit your eligible answers. Remember there is still 300 reputation in prizes! (See 6 of the rules)
Also, as per section 4 of the rules in the linked meta post, the "restricted languages" for this third challenge are only Sledgehammer, J and Mathematica so submissions in these languages are not eligible for the final prize. But they can still be posted!!
Final prize
If you want to be considered for the final prize, at the end of your answer to this challenge please add a link to the eligible submissions you want to be considered for your final score, as well as the scores those answers got! This way it will be slightly easier for me to keep track of everything. Thanks!
This is still a regular code-golf challenge, so enjoy!
[[0],[1],[2],[3],[4],[5],[7],[6]]
valid? \$\endgroup\$l
isn't of interest. I say we can assume the representation ofl
is ordered. \$\endgroup\$