#NEWLINE# def kmp_table( word ) : #NEWLINE# #INDENT# "Build the 'partial match' table of the Knuth-Morris-Pratt algorithm.\n\n    Note: This is applicable to strings or\n    quantum circuits represented as tuples.\n    " #NEWLINE# pos = 2 #NEWLINE# cnd = 0 #NEWLINE# table = list ( ) #NEWLINE# table . append ( ( - 1 ) ) #NEWLINE# table . append ( 0 ) #NEWLINE# while ( pos < len ( word ) ) : #NEWLINE# #INDENT# if ( word [ ( pos - 1 ) ] == word [ cnd ] ) : #NEWLINE# #INDENT# cnd = ( cnd + 1 ) #NEWLINE# table . append ( cnd ) #NEWLINE# pos = ( pos + 1 ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( cnd > 0 ) : #NEWLINE# #INDENT# cnd = table [ cnd ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# table . append ( 0 ) #NEWLINE# pos = ( pos + 1 ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# return table