-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash_chains.py
executable file
·95 lines (73 loc) · 2.52 KB
/
hash_chains.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# Hash table using chaining scheme implementation.
# Author: jerrybelmonte
class Query:
def __init__(self, query):
self.type = query[0]
if self.type == 'check':
self.ind = int(query[1])
else:
self.s = query[1]
class QueryProcessor:
_multiplier = 263
_prime = 1000000007
def __init__(self, row_count: int):
self.row_count = row_count
self.rows = dict()
for num in range(row_count):
self.rows[num] = list()
def _hash_func(self, s: str):
ans = 0
for c in reversed(s):
ans = (ans * self._multiplier + ord(c)) % self._prime
return ans % self.row_count
def add(self, s: str):
"""Inserts a string into the hash table if and only if no such string
already exists in the hash table."""
index = self._hash_func(s)
if s not in self.rows[index]:
self.rows[index].append(s)
def remove(self, s: str):
"""Removes a string from the hash table if and only if the string
already exists within the hash table."""
index = self._hash_func(s)
if s in self.rows[index]:
ndx = self.rows[index].index(s)
del self.rows[index][ndx]
def find(self, s: str):
"""Returns True if the string exists within the hash table, otherwise
returns false."""
index = self._hash_func(s)
return s in self.rows[index]
def check(self, index: int):
"""Checks and returns the list at the given row index if the list is
not empty, otherwise returns an empty string."""
if self.rows[index]:
return self.rows[index][::-1]
return ""
def read_query():
return Query(input().split())
def write_search_result(was_found):
print('yes' if was_found else 'no')
def write_chain(chain):
print(' '.join(chain))
def process_query(q_processor, query):
if query.type == 'add':
q_processor.add(query.s)
elif query.type == 'del':
q_processor.remove(query.s)
elif query.type == 'find':
write_search_result(q_processor.find(query.s))
elif query.type == "check":
chain = q_processor.check(query.ind)
if chain:
write_chain(chain)
else:
print(chain)
def process_queries(q_processor):
n = int(input())
for i in range(n):
process_query(q_processor, read_query())
if __name__ == '__main__':
bucket_count = int(input())
proc = QueryProcessor(bucket_count)
process_queries(proc)