Last active
August 29, 2015 14:02
-
-
Save jesuscast/21da56505615bd107a10 to your computer and use it in GitHub Desktop.
Counts the number of times a pattern appears on a list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def count_pattern(pattern, lst): | |
lenP = len(pattern) | |
#print("lenP",lenP) | |
lenL = len(lst) | |
#print("lenL",lenL) | |
total = 0 | |
if(lenL >= lenP): | |
for i in range(0, lenL): | |
#print("repetition #:"+str(i)) | |
match = False | |
if(pattern[0]==lst[i]): | |
#print("-"+pattern[0]+"- matches in position "+str(i)) | |
#print(str(lenP+i-1)) | |
if((lenP+i-1)<lenL): | |
#print("inside the if statement lol") | |
for j in range(0,lenP): | |
#print("inside the for lol") | |
if(pattern[j]==lst[j+i]): | |
#print("inside the second if lol") | |
if(j == (lenP-1)): | |
match = True | |
continue | |
else: | |
break | |
if(match==True): | |
total +=1 | |
else: | |
total = 0 | |
return total | |
count_pattern(('a', 'b', 'a'), ('g', 'a', 'b', 'a', 'b', 'a', 'b', 'a')) | |
#3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment