-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTable.py
More file actions
45 lines (35 loc) · 797 Bytes
/
HashTable.py
File metadata and controls
45 lines (35 loc) · 797 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
n, q = (int(j) for j in input().split())
A = [[] for i in range(n)]
def hash(x):
return (x % n)
def find(x):
i = hash(x)
while A[i] != []:
if A[i] == x:
return(1)
i = hash(i+1)
return(0)
def add(x):
i = hash(x)
while A[i] != [] and A[i] != 'deleted':
if A[i] == x:
return(i)
i = hash(i+1)
A[i] = x
return(i)
def remove(x):
i = hash(x)
while A[i] != []:
if A[i] == x:
A[i] = 'deleted'
return(1)
i = hash(i+1)
return(0)
for i in range(q):
word, num = (str(j) for j in input().split())
if word == 'add':
print(add(int(num)))
if word == 'find':
print(find(int(num)))
if word == 'remove':
print(remove(int(num)))