@realazthat/

# 3-SUM-by-views

## No description

Files
• main.py
• generators.py
• tests.py
• three_sum_bisect.py
• three_sum_indexed.py
• three_sum_view.py
• tsum_util.py
• view.py
main.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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
```
```

import time
import sys
import random
import tests
import unittest
import three_sum_bisect
from generators import pathological_generator
from three_sum_view import solve_views
from three_sum_indexed import solve_brute
from view import to_view
import tsum_util
from functools import partial

def measure_complexity(f,g,n,K,t=None, p=tsum_util.print_null):

times = []
for n in range(1, 1000):

p('testing K=%s trials with n=%s' % (K,n,))

t0 = time.time()
for _ in range(K):
certification, problem = g(n)
solution = f(problem)
if t: assert t(solution)

dt = time.time() - t0

ddt = (dt-times[-1]) if len(times) > 0 else dt

p('tested K trials with n=%s, K=%s, dt=%.4fs, dt/K=%.4fs, ddt=%.4fs' % (n,K,dt,dt/K, ddt))
times.append(dt)
return times

def bisect_main():
random.seed(1)

numeric_max_bits = 8
sample_count = 1000
sample_length = 10

samples = [
pathological_generator(
Nbits=numeric_max_bits,
n=sample_length,
certify=random.random() > 0.5)
for _ in range(sample_count)
]

# samples = [
#((0, 2, 1), [29, 41, 12]),
# (None, [37, 17, 37]),
# ((1, 0, 2), [50, 216, 266])
# ((1, 0, 0), [31, 0, 31])
# ]

outfile = open('outfile.txt', 'w')

heavy = True
fancy = True
p = tsum_util.print_null
p = partial(print,file=outfile)
for sample_index,(triplet, I) in enumerate(samples):
print ('-')
print ('sample index:',sample_index)
print ('triplet, I:', repr((triplet, I)))
certify = triplet is not None

I,J,K = I,I,I

if certify:
i,j,k = triplet
ivalue, jvalue, kvalue = I[i], J[j], K[k]
assert ivalue + jvalue == kvalue

triplets = three_sum_bisect.solve(I,J,K,p=p,heavy=heavy,choose='largest')

print ('I:', I)
print ('certify:', certify)
print ('triplet:', triplet)
print ('triplets:', triplets)
print ('resolve(triplets):', tsum_util.resolve(I,J,K, triplets))
print ('len(triplets):', len(triplets))
print ('triplets:', triplets)
if heavy:
expected_triplets = solve_brute(I,J,K)
print ('expected_triplets:', expected_triplets)
print ('resolve(expected_triplets):', tsum_util.resolve(I,J,K, expected_triplets))
assert expected_triplets == triplets
assert tsum_util.resolve(I,J,K, expected_triplets) == tsum_util.resolve(I,J,K, triplets)

if certify:
assert triplet in triplets

triplets = tsum_util.resolve(I,J,K, triplets)

for ivalue,jvalue,kvalue in triplets:
assert ivalue + jvalue == kvalue

def bisect_complexity_main():
random.seed(1)

heavy = False
p = tsum_util.print_null
def f(problem):
I,J,K = problem
solution = three_sum_bisect.solve(I,J,K,p=p,heavy=heavy,choose='largest')
return solution
numeric_max_bits = 8
def g(n):
certification,V = pathological_generator(
Nbits=numeric_max_bits,
n=n,
certify=random.random() > 0.5)
I,J,K = V,V,V
problem = I,J,K
return (certification,problem)

times = measure_complexity(f,g,n=100,K=100,p=print)
print (times)

# bisect_main()
bisect_complexity_main()

def views_main():
random.seed(1)

numeric_max_bits = 64
sample_count = 1
sample_length = 100

samples = [
pathological_generator(
Nbits=numeric_max_bits,
n=sample_length,
certify=random.random() > 0.5)
for _ in range(sample_count)
]
# samples = [
#   (
#     (2, 4, 3),
#     [4079209076, 3965891272, 3648514451, 7266853563, 3618339112]
#   ),
# (None, [8, 3, 25, 24, 19]),
#   ((1, 0, 2), [2, 18, 20, 29, 7]),
#   ((0, 8, 6), [7912, 46315, 7913, 1191, 38166, 47604, 28381, 20061, 20469, 7911])
# ((4, 6, 8), [35632, 33120, 113244, 113242, 8549, 113243, 13162, 51739, 21711, 57736])
# ((0, 2, 3), [6, 12, 22, 28]),
# ((0, 0, 1), [14, 28, 15, 14]),
# ((4, 7, 6), [22, 0, 69, 72, 226, 174, 296, 70]),
# ]

# unittest.main(module='tests')

outfile = open('outfile.txt', 'w')

heavy = True
fancy = True
p = tsum_util.print_null
p = partial(print,file=outfile)
for sample_index,(triplet, I) in enumerate(samples):
print ('-')
print ('sample index:',sample_index)
print ('triplet, I:', repr((triplet, I)))
certify = triplet is not None

I,J,K = I,I,I

if certify:
i,j,k = triplet
ivalue, jvalue, kvalue = I[i], J[j], K[k]
assert ivalue + jvalue == kvalue

# print (to_view(I,heavy=heavy).all_str())
# break;

print ('I:',I)
print ('to_view(I).collect():',to_view(I,heavy=heavy).collect())
print ('index(I):',tsum_util.index(I))
assert (to_view(I,heavy=heavy).collect() == tsum_util.index(I))
triplets = solve_views(to_view(I,heavy=heavy),
to_view(J,heavy=heavy),
to_view(K,heavy=heavy),
heavy=heavy, p=p, expected_case='mmm',
fancy=fancy)

print ('I:', I)
print ('certify:', certify)
print ('triplet:', triplet)
print ('resolve(triplet):', tsum_util.resolve(I,J,K, triplets))
print ('len(triplets):', len(triplets))
print ('triplets:', triplets)
if heavy:
expected_triplets = solve_brute(I,J,K)
print ('resolve(expected_triplets):', tsum_util.resolve(I,J,K, expected_triplets))
assert expected_triplets == triplets
assert tsum_util.resolve(I,J,K, expected_triplets) == tsum_util.resolve(I,J,K, triplets)

if certify:
assert triplet in triplets

triplets = tsum_util.resolve(I,J,K, triplets)

for ivalue,jvalue,kvalue in triplets:
assert ivalue + jvalue == kvalue

# random.seed(0)

# N = 1 << 24
# M = 50

# I = ([random.randint(1, N) for _ in range(M)])

# N_I = max([x.bit_length() for x in I])

# I_str = list(map(lambda x: bin(x)[2:].zfill(N_I), I))

# I_str = list(map(lambda x: x[::-1], sorted(map(lambda x: x[::-1], I_str))))

# print ('\n'.join(I_str))

# print (numpy.zeros(shape=(50,50), dtype='bool'))
```