Created
December 20, 2014 11:10
-
-
Save mdamien/0b0950529843078e8c87 to your computer and use it in GitHub Desktop.
pattern matching
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 iterate_by_chunk(word, chunk_length): | |
i = 0 | |
while i < len(word): | |
yield word[i:i+chunk_length] | |
i += chunk_length | |
def check(word, pattern, len_pattern_word, i_offset): | |
"""check if the word match the pattern""" | |
pattern_length = len(pattern)*len_pattern_word | |
corresp = {} | |
piece_to_match = word[i_offset:i_offset+pattern_length] | |
for i, letter in enumerate(iterate_by_chunk(piece_to_match, len_pattern_word)): | |
pattern_letter = pattern[i] | |
if pattern_letter in corresp: | |
if corresp[pattern_letter] != letter: | |
return False,piece_to_match,{} | |
else: | |
corresp[pattern_letter] = letter | |
return True, piece_to_match, corresp | |
def find_pattern(word, pattern): | |
""" | |
Check if word contain a pattern such as 'aabbcc' or 'abbac' anywhere in the word | |
NOTE: Each token have the same length (e.g: if len(a) = 2 then len(b) = 2 | |
""" | |
len_pattern_word = 1 | |
pattern_length = len(pattern) | |
while pattern_length < len(word): | |
for i in range(0,len(word)-pattern_length+1): | |
result, piece, corresp = check(word, pattern, len_pattern_word, i) | |
if result: | |
print('do match') | |
print(piece, corresp) | |
return True | |
len_pattern_word += 1 | |
pattern_length = len(pattern)*len_pattern_word | |
return False | |
pattern = 'aabbcc' | |
word = '++a-a-b-b-c-c---' | |
check_pattern(word, pattern) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment