2
$\begingroup$

I am trying to find a pattern that matches a list of sequences that share a common form. The sequence form is: 1 followed by zero or more nonzero integers, followed by 0. An example list is:

{1,3,2,0,1,0,1,2,0,1,1,4,0}

The closest thing I've struck on so far is this:

MatchQ[{1, 2, 0, 1, 2, 0}, {PatternSequence[1, s___ /; FreeQ[{s}, 0], 0] ..}]

which yields True in this case because the sequences are both the same. Is there a way to get the s___ to apply uniquely to each instance in the Repeated? This would solve the problem. Other suggestions are most welcome.

$\endgroup$
4
$\begingroup$
list = {1,3,2,0,1,0,1,2,0,1,1,4,0};

MatchQ[list, {PatternSequence[1, ___?(IntegerQ@# && Positive[#] &), 0] ...}]

True

This is an interesting problem. I'm not sure how to solve it using Condition rather than PatternTest.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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