-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathHash Function.py
More file actions
27 lines (22 loc) · 850 Bytes
/
Hash Function.py
File metadata and controls
27 lines (22 loc) · 850 Bytes
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
"""
In data structure Hash, hash function is used to convert a string(or any other type) into an integer smaller than hash
size and bigger or equal to zero. The objective of designing a hash function is to "hash" the key as unreasonable as
possible. A good hash function can avoid collision as less as possible. A widely used hash function algorithm is using
a magic number 33
"""
__author__ = 'Danyang'
class Solution:
def hashCode(self, key, HASH_SIZE):
"""
:param key: A String you should hash
:param HASH_SIZE: An integer
:return an integer
"""
w = 1
ret = 0
for i in xrange(len(key)-1, -1, -1):
ret = (ret+ord(key[i])*w)%HASH_SIZE
w = (w*33)%HASH_SIZE
return ret
if __name__ == "__main__":
assert Solution().hashCode("abcd", 100) == 78