Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 3 down vote accepted
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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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