Repository navigation
Expand file tree
/
Copy pathhash_chains_test.py
More file actions
197 lines (156 loc) · 6.66 KB
/
Copy pathhash_chains_test.py
File metadata and controls
197 lines (156 loc) · 6.66 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# python3
import random
import time
import string
import os
import psutil
process = psutil.Process(os.getpid())
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, bucket_count):
self.bucket_count = bucket_count
# store all strings in one list
self.elems = {}
self.elemsList = []
self.results = []
def _hash_func(self, s):
ans = 0
for c in reversed(s):
ans = (ans * self._multiplier + ord(c)) % self._prime
return ans % self.bucket_count
def write_search_result(self, was_found):
self.results.append('yes' if was_found else 'no')
def write_chain(self, chain):
self.results.append(' '.join(chain))
def read_query(self):
return Query(input().split())
def process_query_naive(self, query):
if query.type == "check":
# use reverse order, because we append strings to the end
self.write_chain(cur for cur in reversed(self.elemsList) if self._hash_func(cur) == query.ind)
else:
try:
ind = self.elemsList.index(query.s)
except ValueError:
ind = -1
if query.type == 'find':
self.write_search_result(ind != -1)
elif query.type == 'add':
if ind == -1:
self.elemsList.append(query.s)
else:
if ind != -1:
self.elemsList.pop(ind)
def process_query(self, query):
if query.type == "check":
# use reverse order, because we append strings to the end
values = self.elems.get(query.ind)
if values:
self.write_chain(reversed(values))
else:
self.write_chain([])
else:
if query.type == 'find':
wasFound = False
values = self.elems.get(self._hash_func(query.s))
if values:
for value in values:
if value == query.s:
wasFound = True
self.write_search_result(wasFound)
elif query.type == 'add':
values = self.elems.get(self._hash_func(query.s))
if values:
exists = False
for value in values:
if value == query.s:
exists = True
if not exists:
values.append(query.s)
else:
values = [query.s]
self.elems[self._hash_func(query.s)] = values
elif query.type == 'del':
values = self.elems.get(self._hash_func(query.s))
if values:
for idx, value in enumerate(values):
if value == query.s:
del values[idx]
def process_queries(self, queryCount, queries):
for i in range(queryCount):
self.process_query(Query(queries[i].split()))
return self.results
def process_queries_naive(self, queryCount, queries):
for i in range(queryCount):
self.process_query_naive(Query(queries[i].split()))
return self.results
def generate_random_query(stringLength, bucketCount):
r = random.randint(0, 3)
if r == 0:
s = generate_random_string(stringLength)
query = "add " + s
if r == 1:
s = generate_random_string(stringLength)
query = "del " + s
if r == 2:
s = generate_random_string(stringLength)
query = "find " + s
if r == 3:
i = random.randint(0, bucketCount -1)
query = "check " + str(i)
return query
def generate_random_string(length):
s = ""
for i in range(length):
s = s + str(random.choice(string.ascii_letters))
return s
if __name__ == '__main__':
bucketsCount = 5
queryCount = 12
queries = ["add world", "add Hell0", "check 4", "find World", "find world", "del world", "check 4", "del Hell0", "add luck", "add GooD", "check 2", "del good"]
proc = QueryProcessor(bucketsCount)
procNaive = QueryProcessor(bucketsCount)
results = proc.process_queries(queryCount, queries)
resultsNaive = procNaive.process_queries_naive(queryCount, queries)
print("Case 1 Correct: " + str(results == resultsNaive))
bucketsCount = 4
queryCount = 8
queries = ["add test", "add test", "find test", "del test", "find test", "find Test", "add Test", "find Test"]
proc = QueryProcessor(bucketsCount)
procNaive = QueryProcessor(bucketsCount)
results = proc.process_queries(queryCount, queries)
resultsNaive = procNaive.process_queries_naive(queryCount, queries)
print("Case 2 Correct: " + str(results == resultsNaive))
bucketsCount = 3
queryCount = 12
queries = ["check 0", "find help", "add help", "add del", "add add", "find add", "find del", "del del", "find del", "check 0", "check 1", "check 2"]
proc = QueryProcessor(bucketsCount)
procNaive = QueryProcessor(bucketsCount)
results = proc.process_queries(queryCount, queries)
resultsNaive = procNaive.process_queries_naive(queryCount, queries)
print("Case 3 Correct: " + str(results == resultsNaive))
queryCount = 100000
bucketsCount = random.randint(queryCount/5, queryCount)
print("Buckets count: " + str(bucketsCount))
gStart = time.time()
queries = [generate_random_query(15, bucketsCount) for i in range(queryCount)]
gEnd = time.time()
print("Time to generate " + str(queryCount) + " queries: " + str(gEnd-gStart))
proc = QueryProcessor(bucketsCount)
start = time.time()
results = proc.process_queries(queryCount, queries)
end = time.time()
print("Stress Test Timely: " + str(end - start < 7) + " " + str(end - start))
memoryUsage = process.memory_info().rss * 1e-6
print("Memory efficient: " + str(memoryUsage < 512) + " " + str(memoryUsage))
procNaive = QueryProcessor(bucketsCount)
resultsNaive = procNaive.process_queries_naive(queryCount, queries)
print("Stress Test Correct: " + str(results == resultsNaive))