# Seeking a way to generate sequential partitions of a list using built in or Combinatorica functions [duplicate]

This code generates all "sequential partitions" of a list:

testlist = {a, b, c, d, e};
w = Length[testlist];
breakpoints = Map[Join[#, {w}] &, Subsets[Range[w - 1]]];
partitionfrombreakpoints[breakpointlist_] :=
Prepend[Map[
Take[testlist, {breakpointlist[[#]] + 1,
breakpointlist[[# + 1]]}] &,
Range[Length[breakpointlist] - 1]],
Take[testlist, {1, breakpointlist[[1]]}]];
Print[Grid[Map[partitionfrombreakpoints, breakpoints]]];


Like so:

{a,b,c,d,e}
{a} {b,c,d,e}
{a,b}   {c,d,e}
{a,b,c} {d,e}
{a,b,c,d}   {e}
{a} {b} {c,d,e}
{a} {b,c}   {d,e}
{a} {b,c,d} {e}
{a,b}   {c} {d,e}
{a,b}   {c,d}   {e}
{a,b,c} {d} {e}
{a} {b} {c} {d,e}
{a} {b} {c,d}   {e}
{a} {b,c}   {d} {e}
{a,b}   {c} {d} {e}
{a} {b} {c} {d} {e}


I have tried to use Partitions, SetPartitions, Compositions and Permutations to achieve the same result more elegantly, but without success. Can anyone help, please ?

• Your question implies you have a working code, but the code in the question is full of errors, can you please edit your question? – rhermans Nov 6 '15 at 16:18
• My apologies, rhermans. The code seems to work fine for me. I have just added the output. – Simon Nov 6 '15 at 16:19
• Does it work for you with a fresh kernel? what are you expecting from Subsets[4]? – rhermans Nov 6 '15 at 16:20
• I have edited the code, replacing Subsets[n] by Subsets[Range[n]], so it now no longer depends on the Combinatorica package. – Simon Nov 6 '15 at 16:54
• Also see: (8528) – Mr.Wizard Dec 4 '15 at 0:52

InternalPartitionRagged[{a, b, c, d, e}, #] & /@
Flatten[Permutations /@ IntegerPartitions[5], 1]


About InternalPartitionRagged: I have read about it here.

• Thank you very much Alexey, and also @rhermans, march and eldo ! – Simon Nov 6 '15 at 17:20

Possible partitions for a list with 5 elements:

    n = 5;
Union@Select[Tuples[#, Length@#], Total@# == n &] & /@
IntegerPartitions[n] // Sort // MatrixForm


• Using integer partitions is clever (+1)! Now, how to use the resulting list to break up {a, b, c, d, e} into its list partitions? – march Nov 6 '15 at 16:46
• @march, see this. – J. M.'s ennui Nov 6 '15 at 17:37
• @J.M. Thanks. I'd seen that before (complete with many upvotes) but had completely forgotten about it :) – march Nov 6 '15 at 18:02

## Option 1

f[list_] :=
With[{part =
Flatten[Permutations /@ IntegerPartitions[Length[list]], 1]},
Table[
First@Last@
Reap[FoldList[(Sow[First[#]]; Last[#]) &@*TakeDrop, list, p]]
, {p, part}]
]


f[{a, b, c, d, e}] // MatrixForm


## Option 2

PartitionRagged[vec_, lens_] :=

• Strange, try replacing by Composition[(Sow[First[#]]; Last[#]) & , TakeDrop]. The thing is that v9 has a different take on Composition or Compose – rhermans Nov 6 '15 at 17:24