2
$\begingroup$

Consider a list with all different entries of length n, i.e. for n=6:

list = {1,2,3,4,5,6};

I would like to have a function fun[lst_,m_] that given this list will return a list of lists containing all possible ways to cut list into m adjacent sublists, such that the length of each sublist is odd:

fun[list,0]

False

fun[list,2]

{ {{1,2,3,4,5},{6}} , {{1,2,3},{4,5,6}} , {{1},{2,3,4,5,6}} }

fun[list,4]

{ {{1,2,3},{4},{5},{6}} , {{1},{2,3,4},{5},{6}} , {{1},{2},{3,4,5},{6}} , {{1},{2},{3},{4,5,6}} }

fun[list,6]

{ {{1},{2},{3},{4},{5},{6}} }

Again, notice that the resulting list contains lists that divide list into an even number m of sublists, each of which has odd length.

Is there a quick way to implement this in Mathematica?

EDIT

Due to popular demand, behold my monstrosity:

fun[lst_, m_] := Module[{prt,odd, tmp, tmp2, prm, pt},
   prt = IntegerPartitions[Length[lst], {m}];
   tmp = {};
   Do[
    odd = True;
    Do[
     If[EvenQ[prt[[i, j]]], odd = False;];
     , {j, 1, Length[prt[[i]]]}];
    If[odd, AppendTo[tmp, prt[[i]]];];
    , {i, 1, Length[prt]}];
   tmp2 = {};
   Do[
    prm = Permutations[tmp[[i]]] // DeleteDuplicates;
    Do[
     pt = {};
     Do[
      AppendTo[pt, lst[[Total[prm[[j, 1 ;; q - 1]]] + 1 ;; Total[prm[[j, 1 ;; q]]]]]];
      , {q, 1, Length[prm[[j]]]}];
     AppendTo[tmp2, pt];
     , {j, 1, Length[prm]}];
    , {i, 1, Length[tmp]}];
   tmp2
   ];
$\endgroup$
  • 1
    $\begingroup$ Please show at least one example of what you have tried thus far. $\endgroup$ – Edmund May 2 '17 at 20:58
  • 1
    $\begingroup$ probably you want to use IntegerPartitions and Permutations $\endgroup$ – george2079 May 2 '17 at 21:00
  • 1
    $\begingroup$ @Edmund I did warn you that it would not be pretty! ^^ See the edit of my question. $\endgroup$ – Kagaratsch May 2 '17 at 21:47
  • $\begingroup$ Yes. It is less-than ideal. :-) $\endgroup$ – Edmund May 2 '17 at 23:53
6
$\begingroup$
ClearAll[fun]
fun[lst_, m_] := Module[{lengths = Join @@ (Permutations /@ 
       IntegerPartitions[Length@lst, {m}, Range[1, Length@lst, 2]])}, 
  Internal`PartitionRagged[lst, #] & /@ lengths]


fun[Range[6], 0]

{}

fun[Range[6], 2]

{{{1, 2, 3, 4, 5}, {6}}, {{1}, {2, 3, 4, 5, 6}}, {{1, 2, 3}, {4, 5, 6}}}

fun[Range[6], 4]

{{{1, 2, 3}, {4}, {5}, {6}}, {{1}, {2, 3, 4}, {5}, {6}}, {{1}, {2}, {3, 4, 5}, {6}}, {{1}, {2}, {3}, {4, 5, 6}}}

fun[Range[6], 6]

{{{1}, {2}, {3}, {4}, {5}, {6}}}

$\endgroup$
  • 1
    $\begingroup$ This is amazingly short, thank you! $\endgroup$ – Kagaratsch May 2 '17 at 21:48
  • 1
    $\begingroup$ @Kagaratsch, thank you for the accept. $\endgroup$ – kglr May 2 '17 at 22:11
  • $\begingroup$ what should I change if I want to switch m from even to odd? $\endgroup$ – Kagaratsch May 2 '17 at 23:01
  • 1
    $\begingroup$ @Kagaratsch, if you still want to have each sublist to have an odd length, you don't have to change anything (m could be even or odd). If you want to have each sublist to have even length, use Range[2, Length@lst, 2] instead of Range[1, Length@lst, 2]. $\endgroup$ – kglr May 2 '17 at 23:10
  • $\begingroup$ Elegant... +1.. $\endgroup$ – David G. Stork May 2 '17 at 23:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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