Last active
December 16, 2015 10:48
-
-
Save gogsbread/5422461 to your computer and use it in GitHub Desktop.
Fault table for KMP algorithm
This file contains 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 construct_fault_table(pattern): | |
fault_table = [] | |
fault_table.extend((0,0) # indexes 0 & 1 are always '0' | |
j=0 # poniter to the prefix | |
for i in xrange(2,len(pattern),1): | |
if pattern[i] == pattern[j]: | |
fault_table.append(fault_table[i-1]+1) | |
j+=1 | |
else: | |
j=0 # reset j to the start of the pattern if it breaks | |
fault_table.append(0) | |
return fault_table | |
print construct_fault_table('ABABE') | |
print construct_fault_table('ABABAC') | |
print construct_fault_table('ABACAB') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment