GoalScope — kmp_search (Qwen2.5-Coder-32B-Instruct) — hover a token to see the patched verbalization
↵
<code>↵
def kmp_search(text, pattern):↵
"""Knuth-Morris-Pratt string search algorithm."""↵
def compute_lps(pattern):↵
lps = [0] * len(pattern)↵
length = 0↵
i = 1↵
while i < len(pattern):↵
if pattern[i] == pattern[length]:↵
length += 1↵
lps[i] = length↵
i += 1↵
else:↵
if length != 0:↵
length = lps[length - 1]↵
else:↵
lps[i] = 0↵
i += 1↵
return lps↵
↵
lps = compute_lps(pattern)↵
i = 0↵
j = 0↵
while i < len(text):↵
if pattern[j] == text[i]:↵
i += 1↵
j += 1↵
if j == len(pattern):↵
return i - j↵
elif i < len(text) and pattern[j] != text[i]:↵
if j != 0:↵
j = lps[j - 1]↵
else:↵
i += 1↵
return -1↵
</code>