Hi Raspi!
Ok, here comes an approach using SequenceSplit
- I am not claiming that this is in some way elegant! Using from above
list = {1, 2, 3, 8, 4, 2, 6, 7, 3, 2, 1, 9, 8, 9, 1, 3, 1, 2, 9, 2, 8, 2, 7, 7, 1, 2, 3, 4, 3, 2, 1};
we already get a nice intermediate result like so:
imr = SequenceSplit[list, p : {_, _, _} /; (Max[p] <= 5) :> Splice[{"Join", p}]]
(* Out: {"Join",{1,2,3},{8,4,2,6,7},"Join",{3,2,1},{9,8,9},"Join",{1,3,1},{2,9,2,8,2,7,7},"Join",{1,2,3},"Join",{4,3,2},{1}} *)
The string "Join"
is used here just as a marker where sublist should be joined:
SequenceReplace[imr, {a_, "Join", b_} :> Join[a, b]](* now cleaning up: *) /. "Join" -> Nothing
(* Out: {{1,2,3},{8,4,2,6,7,3,2,1},{9,8,9,1,3,1},{2,9,2,8,2,7,7,1,2,3},{4,3,2},{1}} *)
I admit that I did not do any excessive testing, so there might well be some flaw! Regards -- Henrik