forked from mikeizbicki/binary_search
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_search.py
More file actions
202 lines (179 loc) · 4.83 KB
/
binary_search.py
File metadata and controls
202 lines (179 loc) · 4.83 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
198
199
200
201
202
#!/bin/python3
def find_smallest_positive(xs):
'''
Assume that xs is a list of numbers sorted from LOWEST to HIGHEST.
Find the index of the smallest positive number.
If no such index exists, return `None`.
HINT:
This is essentially the binary search algorithm from class,
but you're always searching for 0.
>>> find_smallest_positive([-3, -2, -1, 0, 1, 2, 3])
4
>>> find_smallest_positive([1, 2, 3])
0
>>> find_smallest_positive([-3, -2, -1]) is None
True
'''
left = 0
right = len(xs) - 1
if len(xs) == 0:
return None
if xs[right] < 1:
return None
if xs[left] > 0 or len(xs) == 1:
return left
def go(left,right):
mid = (right+left)//2
if right-left == 1:
return right
if xs[mid] == 0:
return mid+1
if xs[mid] > 0:
right = mid - 1
if xs[mid] < 0:
left = mid + 1
return go(left ,right)
return go(left,right)
def count_repeats(xs, x):
'''
Assume that xs is a list of numbers sorted from HIGHEST to LOWEST,
and that x is a number.
Calculate the number of times that x occurs in xs.
HINT:
Use the following three step procedure:
1) use binary search to find the lowest index with a value >= x
2) use binary search to find the lowest index with a value < x
3) return the difference between step 1 and 2
I highly recommend creating stand-alone functions for steps 1 and 2
that you can test independently.
>>> count_repeats([5, 4, 3, 3, 3, 3, 3, 3, 3, 2, 1], 3)
7
>>> count_repeats([3, 2, 1], 4)
0
'''
if len(xs) == 0:
return 0
if len(xs) == 1 and xs[0] == x:
return 1
if len(xs) == 1 and xs[0] != x:
return 0
if xs[0] == xs[len(xs)-1]:
return len(xs)
def left_index(xs,x):
l = 0
r = len(xs) - 1
if xs[l] == x:
print('FIRST ELEM leftmost index = ',l)
return 0
def go_left(l,r):
mid = (l+r)//2
#print('left = ', l,'mid = ', mid , 'right = ',r)
if r - l <= 1:
#print('leftmost index = ',mid)
if xs[l] == x:
return l
if xs[r] == x:
return r
return None
if xs[mid] > x:
l = mid
if xs[mid] <= x:
r = mid
return go_left(l,r)
return go_left(l,r)
def right_index(xs,x):
l = 0
r = len(xs) - 1
if xs[r] == x:
#print('FIRST ELEM rightmost index = ',r)
return r
def go_right(l,r):
mid = (l+r)//2
#print('left = ',l, 'mid = ', mid , 'right = ',r)
if r - l <= 1:
#print('rightmost index = ',mid)
if xs[l] == x:
return l
if xs[r] == x:
return r
return None
if xs[mid] >= x:
l = mid
if xs[mid] < x:
r = mid
return go_right(l,r)
return go_right(l,r)
left = left_index(xs,x)
right = right_index(xs,x)
if left == None or right == None:
return 0
dif = right - left
if dif <= 1 and (xs[left] == x or xs[right] == x):
#print('\n\n Final left = ', left, 'Final right = ', right, 'Difference = ', dif, 'Final return value = ', 1)
return 1
#print('\n\n Final left = ', left, 'Final right = ', right, 'Difference = ', dif)
return (dif + 1)
def argmin(f, lo, hi, epsilon=1e-3):
'''
Assumes that f is an input function that takes a float as input and returns a float with a unique global minimum,
and that lo and hi are both floats satisfying lo < hi.
Returns a number that is within epsilon of the value that minimizes f(x) over the interval [lo,hi]
HINT:
The basic algorithm is:
1) The base case is when hi-lo < epsilon
2) For each recursive call:
a) select two points m1 and m2 that are between lo and hi
b) one of the 4 points (lo,m1,m2,hi) must be the smallest;
depending on which one is the smallest,
you recursively call your function on the interval [lo,m2] or [m1,hi]
>>> argmin(lambda x: (x-5)**2, -20, 20)
5.000040370009773
>>> argmin(lambda x: (x-5)**2, -20, 0)
-0.00016935087808430278
'''
'''
while hi - lo < epsilon:
m1 = (hi - lo)//3
m2 = 2*(hi - lo)//3
if f(lo) <= f(m1):
hi = m1
else:
lo = m1
if f(m2) >= f(hi):
lo = m2
else:
hi = m2
return hi
'''
print('starts running')
if abs(hi - lo) < epsilon:
#print('First if')
return lo
def go(lo, hi):
#print('into loop')
m1 = lo + (hi - lo)/3
m2 = lo + 2*((hi - lo)/3)
if abs(hi-lo) < epsilon:
# print(' F(hi) = ',f(hi), 'F(lo) = ', f(lo))
# print('returning FIRST PLACE = ', (hi+lo)/2)
return (hi+lo)/2
#print(' M1 = ',m1,' M2 ', m2)
#print('Low = ', lo,'f(lo) = ',f(lo), 'M1 = ', m1,'f(m1) =',f(m1), 'M2 = ',m2,'f(m2) = ', f(m2), 'High = ',hi,'f(hi) = ',f(hi))
if f(m2) < f(m1):
lo = m1
# print('\n\nHigh changed to m1\n Low = ',lo,' High = ', hi)
#else:
# lo = m1
# print('\n\nLow changed to m1\n Low = ',lo,' M1 = ', m1)
if f(m2) > f(m1):
hi = m2
# print('\n\nLow changed to m2 \n Low = ',lo,' High = ', hi)
#else:
# hi = m2
# print('\n\nHigh changed to m2\n Low = ',lo,' High = ', hi)
#if abs(f(m2)-f(m1)) < epsilon:
# print('returning SECOND PLACE = ', f(lo))
# return f(m1)
#print('Go again')
return go(lo, hi)
return go(lo, hi)