3
$\begingroup$
list =
  {{"d", 10}, {"a", 10}, {"a", 12}, {"c", 8}, {"z", 6}, 
   {"a", 6}, {"z", 12}, {"d", 15}, {"b", 14}, {"b", 13}};

With Split the expected result is easy to get,

Cases[{_, __}] @ Split[list, #1[[-1]] <= #2[[-1]] &]

{{{"d", 10}, {"a", 10}, {"a", 12}}, {{"z", 6}, {"a", 6}, {"z", 12}, {"d", 15}}}

but the shortest solution using sequence-functions I found needs a detour via Takelist

p = Length /@ SequenceCases[Last /@ list, x_ /; LessEqual @@ x]

{3, 1, 4, 1, 1}

Cases[{_, __}] @ TakeList[list, p]

{{{"d", 10}, {"a", 10}, {"a", 12}}, {{"z", 7}, {"a", 8}, {"z", 12}, {"d", 15}}}

My question

Is there a shorter, more direct way using SequenceCases, SequenceSplit or SequencePosition ?

$\endgroup$

2 Answers 2

8
$\begingroup$
SequenceCases[list, x : {_, __} /; LessEqual @@ x[[All, -1]]]
$\endgroup$
4
$\begingroup$

Using SequenceCases and OrderedQ:

SequenceCases[list, x_ /; OrderedQ@Map[Last]@x] /. {_} -> Nothing

{{{"d", 10}, {"a", 10}, {"a", 12}}, {{"z", 6}, {"a", 6}, {"z",
12}, {"d", 15}}}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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