-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash_functions.py
37 lines (25 loc) · 886 Bytes
/
hash_functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# Generates a sequence of universal hash functions.
# (( ax + b ) mod p ) mod m
from os import P_ALL
def universal_hash_functions(a_vals, b_vals, p, m):
if not (len(a_vals) == len(b_vals)):
return None
hash_functions = []
for i in range(len(a_vals)):
a = a_vals[i]
b = b_vals[i]
lambda_f = lambda x: (((a*x) + b) % p) % m
hash_functions.append(lambda_f)
#print(lambda_f)
return hash_functions
def polynomial_hash_functions(p_vals, q_vals, m):
if not (len(p_vals) == len(q_vals)):
return None
hash_functions = []
for i in range(len(p_vals)):
p = p_vals[i]
q = q_vals[i]
#print(p, q, m)
lambda_f = lambda str=str, p=p, q=q, m=m: sum([ord(str[i]) * (p**i) for i in range(len(str))]) % m
hash_functions.append(lambda_f)
return hash_functions