I need to find all compositions of an integer L wherein all parts do not exсeed l and parts less then l cound not be neighbor. Here is my code
test[arr0_, ll0_] :=
Module[{arr = arr0, ll = ll0},
result = True;
For[i = 1, i < Length[arr], i++,
If[arr[[i]] < ll && arr[[i + 1]] < ll, result = False]];
result
];
frag[L0_, l0_] :=
Module[{L = L0, l = l0},
part = IntegerPartitions[L, 2*Ceiling[L/l] + 1, Range[l]];
part = Flatten[Map[Permutations[#] &, part], 1];
Select[part, test[#, l] &]
];
frag[9, 3]
Output: {{3, 3, 3}, {3, 2, 3, 1}, {3, 1, 3, 2}, {2, 3, 3, 1}, {2, 3, 1, 3}, {1, 3, 3, 2}, {1, 3, 2, 3}, {1, 3, 1, 3, 1}}
I guess Mathematica allows to solve this problem much more easier
{1,2,1,2,1,2}
? $\endgroup$