-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathN_SUM.py
181 lines (117 loc) · 2.74 KB
/
N_SUM.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
#!usr/env/bin python
import numpy
from collections import Set
data = numpy.genfromtxt('/home/damian/Dropbox/Algorithms/Sum2N_Data.txt')
data.sort()
data = list(data)
data_set = set(data)
'''
nums_summed = set([])
# just do a double for loop
def find_min_index(x,end=1000000):
Parameters
==========
x : int
>= 0
end : int
<= 1000000
Return
======
int : the smallest index such that the sum of data[x] and data[returned_index] is >= -10000
if x == end:
return corresponding_index
number = data[x]
rest_of_data = data[x:end]
y = (x + end) // 2
x_plus_y = number + data[y]
if x_plus_y < -10000:
return find_min_index((y+1),end)
elif x_plus_y > -10000:
if lowest_valid_sum is None or x_plus_y < lowest_valid_sum:
global lowest_valid_sum
lowest_valid_sum = x_plus_y
global corresponding_index
corresponding_index = y
return find_min_index(x,y)
else:
return y
for x in range(1000000):
print "x =",x
# using binary search, find the smallest y s.t. x + y >= -10000, then keep going until you hit a y s.t. x + y >= 10000
global lowest_valid_sum
lowest_valid_sum = None
global corresponding_index
corresponding_index = None
y = find_min_index(x)
if y is None:
continue
print "start y =",y
while y < 1000000 and data[x] + data[y] <= 10000 :
number = data[x] + data[y]
if number not in nums_summed:
nums_summed.add(number)
y += 1
print "end y =",y
print(len(nums_summed))
'''
'''
def search_neighbors(index):
Parameters
==========
index: int
Returns
=======
boolean : does the index have identical neighbors
val = data[index]
below = index - 1
above = index + 1
if data[below] == val or data[above] == val:
return True
else:
return False
def has_partner(index, x, t, start=0, end=1000000):
Parameters
==========
index : int
the index of x
x : int
t : int
Return
======
boolean : whether or not a partner exists at an index other than index
if start == end:
return False
mid = (start + end) // 2
if data[mid] == t - x:
if mid != index:
return True
else:
search_neighbors(index)
elif data[mid] > t - x :
return has_partner(index,x,t,start,mid)
else :
return has_partner(index,x,t,(mid+1),end)
amount_found = 0
for t in range(-10000,10001):
print t
# start a beginning of data, look for y = t - x
for index in range(1000000):
x = data[index]
found_partner = has_partner(index, x, t)
if found_partner:
amount_found += 1
break
'''
for t in range(-10000,10001):
print t
for index in range(1000000): # linear scan too slow, try binary search
x = data[index]
y = t - x
if y in data_set:
if y != x:
amount_found += 1
break
elif data.count(y) > 1:
amount_found += 1
break
print amount_found