Complete the code to calculate the initial hash value of the pattern.
pattern_hash = 0 for i in range(len(pattern)): pattern_hash = (pattern_hash * base + ord(pattern[i])) % [1]
The modulus is usually a large prime number to reduce collisions in hashing.
Complete the code to update the rolling hash when sliding the window by one character.
text_hash = (text_hash - ord(text[i]) * high_order) * base + ord(text[i + len(pattern)]) text_hash = text_hash % [1]
The modulus must be the same prime number used for initial hashing to keep hash values consistent.
Fix the error in the condition that checks if the current window matches the pattern.
if pattern_hash == text_hash and text[i:i+len(pattern)] == [1]:
We compare the current substring in text with the pattern to confirm a match after hash match.
Fill both blanks to compute the highest order multiplier and initialize the base value.
base = [1] high_order = pow(base, len(pattern) - 1, [2])
Base is usually 256 for ASCII characters, and modulus is the prime number used for hashing.
Fill all three blanks to complete the main loop that slides over the text and checks for matches.
for i in range(len(text) - len(pattern) + 1): if pattern_hash == text_hash and text[i:i+len(pattern)] == [1]: result.append(i) if i < len(text) - len(pattern): text_hash = (text_hash - ord(text[i]) * [2]) * [3] + ord(text[i + len(pattern)]) text_hash %= prime
The loop compares substring with pattern, updates hash by removing old char multiplied by high_order, multiplies by base, adds new char, and applies modulus.