-
Notifications
You must be signed in to change notification settings - Fork 0
/
2.py
63 lines (48 loc) · 1.46 KB
/
2.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
58
59
60
61
62
63
def hashf(key):
result = 0
for i in range(len(key)):
result *= 47
result += ord(key[i]) -96
result %= 1000000411
result %=1000029
return result
class HashTable:
def __init__(self):
self.size = 1000029
self.keys = [[] for i in range(self.size)]
def add(self, key, value):
slot = hashf(key)
a=len(self.keys[slot])
for i in range(a):
if self.keys[slot][i][0] == key:
self.keys[slot][i][1] = value
return
self.keys[slot].append([key , value])
def delete(self, key):
slot = hashf(key)
a=len(self.keys[slot])
for i in range(a):
if self.keys[slot][i][0] == key:
self.keys[slot][i] , self.keys[slot][a-1] = self.keys[slot][a-1], self.keys[slot][i]
self.keys[slot].pop()
return
return
def get(self, key):
slot = hashf(key)
for i in self.keys[slot]:
if i[0]==key:
return i[1]
return 'none'
f=open('map.in', 'r')
w=open('map.out','w')
H = HashTable()
s=f.readline().split()
while s:
if s[0] == 'put':
H.add(s[1],s[2])
elif s[0]=='delete':
H.delete(s[1])
else:
print(H.get(s[1]),file = w)
s=f.readline().split()
w.close()