I was wondering if there is an easier way to find a pattern within an array?
Say i'm looking for one of the patterns in the a given array: A) Smile, Frown, Smile, Frown, etc B) Smile, Angry, Frown, Smile, Angry, Frown, etc C) Smile, Smile, Smile
Now say the array that is given matches pattern A as such:
Angry, Angry, Angry, Smile, Frown, Smile, Frown, Smile, Frown, Angry, Frown, Angry, Frown, Smile
The highlighted section is the section which matches with pattern A and the section I want to store away in a list.
Right now I have something like this:
For each element in the array
check to see if element is smile
if element is smile, check to see if next element is frown
if element is smile and next element is frown - store away in a list
set a boolean saying we've found pattern A
if the boolean value is false and we did not find the smile frown pattern
For each element in the array
check to see if element is smile
if element is smile, check to see if next element is angry,
is next element is angry, check to see if next next element is frown
if element is smile, next element is angry, next next element is frown - store away in a list
set a boolean saying we've found pattern B
if boolean value is false for both finding pattern A and pattern B search for pattern C
Is there a better approach to this? I feel like this is overall bad....
Strings like in your example? Or something else? 3) Do you want to be as fast as possible or should the code be as readable and understandable as possible?