3
$\begingroup$

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 ?

$\endgroup$
  • 1
    $\begingroup$ Your question implies you have a working code, but the code in the question is full of errors, can you please edit your question? $\endgroup$ – rhermans Nov 6 '15 at 16:18
  • $\begingroup$ My apologies, rhermans. The code seems to work fine for me. I have just added the output. $\endgroup$ – Simon Nov 6 '15 at 16:19
  • 1
    $\begingroup$ Does it work for you with a fresh kernel? what are you expecting from Subsets[4]? $\endgroup$ – rhermans Nov 6 '15 at 16:20
  • 1
    $\begingroup$ I have edited the code, replacing Subsets[n] by Subsets[Range[n]], so it now no longer depends on the Combinatorica package. $\endgroup$ – Simon Nov 6 '15 at 16:54
  • 1
    $\begingroup$ Also see: (8528) $\endgroup$ – Mr.Wizard Dec 4 '15 at 0:52
4
$\begingroup$
Internal`PartitionRagged[{a, b, c, d, e}, #] & /@
         Flatten[Permutations /@ IntegerPartitions[5], 1]

About Internal`PartitionRagged: I have read about it here.

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

Possible partitions for a list with 5 elements:

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

enter image description here

$\endgroup$
  • 1
    $\begingroup$ 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? $\endgroup$ – march Nov 6 '15 at 16:46
  • $\begingroup$ @march, see this. $\endgroup$ – J. M. will be back soon Nov 6 '15 at 17:37
  • $\begingroup$ @J.M. Thanks. I'd seen that before (complete with many upvotes) but had completely forgotten about it :) $\endgroup$ – march Nov 6 '15 at 18:02
3
$\begingroup$

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}]
  ]

Mathematica graphics

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

Mathematica graphics

Option 2

PartitionRagged[vec_, lens_] := 
 MapThread[vec[[#1 ;; #2]] &, 
  With[{a = Accumulate[lens]}, {a - lens + 1, a}]]

f2[list_] := 
 PartitionRagged[list, #] & /@ 
  Flatten[Permutations /@ IntegerPartitions[Length[list]], 1]
$\endgroup$
  • 1
    $\begingroup$ Thank you rhermans. The code is generating errors for me. Syntax::sntxf: "(Sow[First[#]];Last[#])&@" cannot be followed by "*TakeDrop". $\endgroup$ – Simon Nov 6 '15 at 17:14
  • $\begingroup$ Did you try with a fresh kernel? What is the error message? $\endgroup$ – rhermans Nov 6 '15 at 17:16
  • 1
    $\begingroup$ Yes, I tried with a fresh kernel. The @ is highlighted and the first error message is Syntax::sntxf: "(Sow[First[#]];Last[#])&@" cannot be followed by "*TakeDrop". $\endgroup$ – Simon Nov 6 '15 at 17:17
  • $\begingroup$ Strange, try replacing by Composition[(Sow[First[#]]; Last[#]) & , TakeDrop]. The thing is that v9 has a different take on Composition or Compose $\endgroup$ – rhermans Nov 6 '15 at 17:24
  • $\begingroup$ Thank you, I will certainly look into this if I get a chance. Meanwhile I'll go with Alexey's answer. Many thanks again for your help. $\endgroup$ – Simon Nov 7 '15 at 12:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.