Python: Monte Carlo Rabin-Karp search -


i trying implement monte carlo rabin-karp search in python. have far (random_prime function returns prime number less limit arguement given):

def search(pattern, text):  m = len(pattern) n = len(text) q = random_prime(m*n*n) r = (2^(m - 1)) % q f = [] x in range (0, n + 1):     f.append(0)  pfinger = 0 j in range(0, m):     f[0] = (2 * f[0]) + (int(text[j]) % q)     pfinger = (2 * pfinger) + (int(pattern[j]) % q)  = 0 while (i + m) < n:     if (f[i] == pfinger):         print "match @ position " + str(i)     f[i + 1] = (2 * (f[i] - (r * int(text[i])))) + (int(text[i + m]) % q)     += 1 

the problem is, seems match first character or characters.

e.g. if call search('01', '101110001010101'), no match.

or if call search('1', '111110110100101') match.

or if call search('0', '0000001110001010101') matches position 5.

is there wrong code causing match incorrectly?

i'm no genius think found root of issue. in line r = (2^(m - 1)) % q , knowledge ^ not correct character creating exponential function. correct character use in python ** such r = (2**(m - 1)) % q. search should print positions except last. can fixed changing < n <= n in line (while (i+m)< n:)

you code print positions although receive index error occurs once positions have been identified. can solved ignoring error once occurs.

try:

   f[i + 1] = (2 * (f[i] - (r * int(text[i])))) + (int(text[i + m]) % q)     += 1 

except:

   break 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -