Skip to content

Instantly share code, notes, and snippets.

@gogsbread
Last active December 16, 2015 10:48
Show Gist options
  • Save gogsbread/5422461 to your computer and use it in GitHub Desktop.
Save gogsbread/5422461 to your computer and use it in GitHub Desktop.
Fault table for KMP algorithm
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