Consider the following list:
{"END", "END", "a", "b", "END", "p", "q", "END", "t", "END"}
I want to combine elements that precede the marker element "END"
; I also wish to include the marker with the combined elements. Here's the desired output:
{{"END"}, {"END"}, {"a", "b", "END"}, {"p", "q", "END"}, {"t", "END"}}
I'm looking for an elegant way of performing this using Mathematica's in built functions, without needing to use procedural programming (though it would be easy to do so).
Here's a way of doing it that doesn't quite work, due to it not handling the two consecutive markers at the start. Even if it does work, I'm not sure if it would be very efficient or elegant:
Flatten /@
SequenceCases[
SplitBy[{"END", "END", "a", "b", "END", "p", "q", "END", "t",
"END"}, # === "END" &], {_, {"END"}}]
(* Outputs: {{"a","b","END"},{"p","q","END"},{"t","END"}} *)