-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcoroutines-left.py
105 lines (97 loc) · 2.55 KB
/
coroutines-left.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
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env python3
def bin_search(S, g):
lo, hi = 0, len(g)
while hi - lo > 1:
mid = (lo + hi) // 2
was_censored = yield {g[:mid]}.union(S)
if was_censored:
hi = mid
else:
lo = mid
return hi
def bisect_left(S, g, after):
lo, hi = 0, len(g)
while hi - lo > 1:
mid = (lo + hi) // 2
was_censored = yield {g[mid:] + after}.union(S)
if not was_censored:
hi = mid
else:
lo = mid
return lo
def comp_aware_bin_split(s):
C = set()
j = len(s)
while True:
i = yield from bin_search(C, s)
j = min(i - 1, j - 1)
while j > 0:
was_censored = yield {s[:i-1], s[j:i]}.union(C)
if was_censored:
break
else:
j = j - 1
C = {s[j:i]}.union(C)
if j > 0:
s = s[:i-1]
else:
s = ""
if not s:
break
was_censored = yield C
if was_censored:
break
return C
def comp_aware_bin_split_2(s):
C = set()
j = len(s)
while True:
i = yield from bin_search(C, s)
diff = 1
j = min(i - 1, j - 1)
while j > 0:
was_censored = yield {s[:i-1], s[j:i]}.union(C)
if was_censored:
break
else:
j -= diff
diff *= 2
diff //= 2
s_1 = s[:i]
k = max(j, 0)
j = k + (yield from bisect_left({s_1[:-1]}.union(C),
s_1[k:j+diff],
s_1[j+diff:]))
C = {s[j:i]}.union(C)
if j > 0:
s = s[:i-1]
else:
s = ""
if not s:
break
was_censored = yield C
if was_censored:
break
return C
def isolate(isolator, sim):
was_censored = None
while True:
try:
test = isolator.send(was_censored)
except StopIteration as e:
return e.value
was_censored = is_censored(test, sim)
def is_censored(test, sim):
separator = '\x00' # will be platform specific
return sim.send(separator.join(test))
def main():
from simulator import Simulator
sim = Simulator()
for art in sim.get_articles():
isolator = comp_aware_bin_split(art)
kw = isolate(isolator, sim)
sim.report_found_keyword(kw)
print(sim.query_log[sim.this_article])
print(sim.queries / len(sim.articles))
if __name__ == "__main__":
main()