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
];
IntegerPartitions
andPermutations
$\endgroup$