Is there a function that gives all the ways a list can be divided into a specified number of non-overlapping pieces, where the order is maintained?
I can do this with Table
. For instance, if the list is:
test={a,b,c,d}
...and I want to see all the ways it can be divided into 2 pieces, this works:
l = Length@test;
Table[{Take[test, i], Take [test, i - l]}, {i, 1, l - 1}];
%//MatrixForm
Just to clarify: If $n=2$, the output I'm looking for would be all the pairs of sublists that, when concatenated, give the original list. E.g., Join[{a},{b,c,d}]===Join[{a,b},{c,d}]===Join[{a,b,c},{d}]==={a,b,c,d}
. If $n=3$, it would be all the groups of three sublists that, when concatenated, would give the original list. And so on.
While the Table
syntax can be extended to any $n$, I'm wondering if there is a function that gives this more directly—something like (making up a function) Sublists[list, n]
. Initially Partition
came to mind, but I'm interested in specifying the number of sublists, rather than their lengths.
This seems to be asking a somewhat different question: Partition list into $n$ sublists
Tuples[test, 2]
? If you wan to keep subsets with unique entriesDeleteDuplicates@Union[#]&/@Tuples[test,2]
I do not understand the output you show above, For example where isb,c
? $\endgroup${b,c}
iftest
were divided into two pieces such that they are non-overlapping & keep the same order. You would only see that iftest
were divided into three pieces:{{a}, {b,c}, {d}}
. [You could see that if you dividedtest
into two pieces this way:{{a,d},{b,c}}
. But that doesn't meet the criterion of maintaining the original order.] Imagine a directional Toblerone bar with 4 "triangles" (so three break points) and you needed to split that bar into exactly two pieces, cutting it only at the break points. I'm asking for all the ways you could do it. $\endgroup$DeleteDuplicates@Union[#] & /@ Tuples[test, 2]
would be for an entirely different problem. Note that, if $n=2$, the output I'm looking for would be all the pairs of sublists that, when concatenated, give the original list. E.g.,Join[{a},{b,c,d}] ===Join[{a,b},{c,d}]===Join[{a,b,c},{d}]==={a,b,c,d}
. I'll add that clarification to my post. $\endgroup$