1
$\begingroup$

This question already has an answer here:

Given a list of upper/lowercase characters, I'm trying to split it into subsequences that start with an uppercase and contain the next few (not necessarily positive number of) elements up until the next uppercase character.

For example, if I call a function that does this splitting action camelsplit, then I'd have

In[]:= camelsplit @ {"A", "b"}
Out[]= {{"A", "b"}}

In[]:= camelsplit @ {"A", "b", "C", "D", "e"}
Out[]= {{"A", "b"}, {"C"}, {"D", "e"}}

SequenceCases seems ideal for this, but I'm not sure how to dictate the pattern in cases where either 0 or more than 2 lowercase characters follow an uppercase character:

SequenceCases[
  {"A", "b", "C", "D", "e", "f"},
  {x_/;UpperCaseQ[x], y_/;!UpperCaseQ[y]} :> {x, y}
]

(* {{"A", "b"}, {"D", "e"}} *)

Attempting to replace y_ /; !UpperCaseQ[y] with something like y__ /; AllTrue[!UpperCaseQ /@ {y}] to handle the more-than-2 case doesn't seem to be right.

I've also tried using Subsequences, but I'm having similar trouble figuring out an appropriate Select-ion criterion to omit subsequences that ignore the condition of containing all the lowercase characters that follow the initial uppercase character:

Select[
  Subsequences[{"A", "b", "C", "D", "e", "f"}, {1, Infinity}],
  Count[#, x_ /; UpperCaseQ[x]] == 1 && UpperCaseQ @ First[#] &
]

(* {{"A"}, {"C"}, {"D"}, {"A", "b"}, {"D", "e"}, {"D", "e", "f"}} *)

Any suggestions not relying on SequenceCases/Subsequences and Select are welcome, too.

$\endgroup$

marked as duplicate by Kuba list-manipulation Nov 27 '18 at 23:12

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2
$\begingroup$
SequenceCases[{"A", "b", "C", "D", "e",  "f"}, {x_?UpperCaseQ, y___?LowerCaseQ} :> {x, y}]

{{"A", "b"}, {"C"}, {"D", "e", "f"}}

Also

SequenceCases[{"A", "b", "C", "D", "e", "f"}, 
    {x_ /; UpperCaseQ[x], y___ /; AllTrue[LowerCaseQ]@{y}} :> {x,  y}]

{{"A", "b"}, {"C"}, {"D", "e", "f"}}

You can also use Split:

Split[{"A", "b", "C", "D", "e", "f"}, LowerCaseQ[#2] &]

{{"A", "b"}, {"C"}, {"D", "e", "f"}}

$\endgroup$
  • $\begingroup$ Much appreciated! Seems I failed to notice an example of PatternTest usage in the docs for SequenceCases... $\endgroup$ – user170231 Nov 27 '18 at 23:13
  • $\begingroup$ @user170231, my pleasure. $\endgroup$ – kglr Nov 27 '18 at 23:19

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