while i < ALPHABET_LEN: delta1[i] = patternlen i += 1
i = 0 while i < patternlen-1: delta1[ord(p[i])] = patternlen-1 - i i += 1 return delta1
deffind(s, p): # find first occurrence of p in s n = len(s) m = len(p) delta1 = make_delta1(p) skip = delta1[ord(p[m-1])] i = 0 while i <= n-m: if s[i+m-1] == p[m-1]: # (boyer-moore) # potential match if s[i:i+m-1] == p[:m-1]: return i if s[i+m] notin p: i = i + m + 1# (sunday) else: i = i + skip # (horspool) else: # skip if s[i+m] notin p: i = i + m + 1# (sunday) else: i = i + 1 return -1# not found
if __name__ == '__main__': print find("HERE IS A SILMPLE EXAMPLE", "EXAMPLE")