MCPcopy
hub / github.com/TheAlgorithms/Python / get_failure_array

Function get_failure_array

strings/knuth_morris_pratt.py:45–62  ·  view source on GitHub ↗

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

(pattern: str)

Source from the content-addressed store, hash-verified

43
44
45def get_failure_array(pattern: str) -> list[int]:
46 """
47 Calculates the new index we should go to if we fail a comparison
48 :param pattern:
49 :return:
50 """
51 failure = [0]
52 i = 0
53 j = 1
54 while j < len(pattern):
55 if pattern[i] == pattern[j]:
56 i += 1
57 elif i > 0:
58 i = failure[i - 1]
59 continue
60 j += 1
61 failure.append(i)
62 return failure
63
64
65if __name__ == "__main__":

Callers 2

knuth_morris_prattFunction · 0.85

Calls 1

appendMethod · 0.45

Tested by

no test coverage detected