-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.py
57 lines (43 loc) · 876 Bytes
/
hash.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
hashTable = []
m = 100
def init():
for i in range(m):
hashTable.append(dict())
def search(key):
hkey = h(key)
bucket = hashTable[hkey]
if key in bucket:
return bucket[key]
else:
return -1
def delete(key):
hkey = h(key)
bucket = hashTable[hkey]
if key in bucket:
del bucket[key]
return True
else:
return False
def insert(key, value):
for i in range(m):
j = h(key)
hashTable[j][key] = value
return key
def h(key):
a = 1233
b = 153
c = key
if type(c) == type('str'):
c = stringToAsccii(c)
return round((a * c + b) % m, 0)
def stringToAsccii(s):
sum = 0
for i in [ord(c) for c in s]:
sum += i
return sum
init()
insert('hi', 12)
insert('his', 15)
print(search('hi'))
print(delete('hi'))
print(search('hi'))