Advertisement
dennoh

number of matches

Jan 10th, 2020
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.48 KB | None | 0 0
  1. def match_count(text,pattern):
  2.     return text.count(pattern)
  3.  
  4. print(match_count('AABAACAADAABAABA', 'AABA'))
  5.  
  6.  
  7. def match_count2(text,pattern):
  8.     count = 0
  9.     accumulator = ''
  10.     pattern_length = len(pattern)
  11.     for char in text:
  12.         if accumulator == pattern:
  13.             count +=1
  14.         if len(accumulator) == pattern_length:
  15.             accumulator=''
  16.            
  17.         accumulator+=char
  18.     return count
  19.  
  20. print(match_count2('AABAACAADAABAABA', 'AABA'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement