MCPcopy Create free account
hub / github.com/subbarayudu-j/TheAlgorithms-Python / get_failure_array

Function get_failure_array

strings/knuth_morris_pratt.py:36–53  ·  view source on GitHub ↗

Calculates the new index we should go to if we fail a comparison :param pattern: :return:

(pattern)

Source from the content-addressed store, hash-verified

34
35
36def get_failure_array(pattern):
37 """
38 Calculates the new index we should go to if we fail a comparison
39 :param pattern:
40 :return:
41 """
42 failure = [0]
43 i = 0
44 j = 1
45 while j < len(pattern):
46 if pattern[i] == pattern[j]:
47 i += 1
48 elif i > 0:
49 i = failure[i-1]
50 continue
51 j += 1
52 failure.append(i)
53 return failure
54
55
56if __name__ == '__main__':

Callers 2

kmpFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected