-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarysearch.py
More file actions
53 lines (34 loc) · 1.19 KB
/
binarysearch.py
File metadata and controls
53 lines (34 loc) · 1.19 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
"""Using a binary search, find val in a range of 1-100. Return # of guesses.
Construct a list of 1-100 (inclusive). Write a binary search that searches
for val in that list (val will always be a number between 1 and 100).
Return the number of searches it took to find val. For a proper binary search
of 1-100, this should never be more than 7.
>>> binary_search(50)
1
>>> binary_search(25)
2
>>> binary_search(75)
2
>>> binary_search(31) <= 7
True
>>> max([binary_search(i) for i in range(1, 101)])
7
"""
def binary_search(val):
"""Using binary search, find val in range 1-100. Return # of guesses."""
assert 0 < val < 101, "Val must be between 1-100"
num_guesses = 0
to_check = range(0, 101)
while to_check:
mid_index = len(to_check)/2
num_guesses += 1
if to_check[mid_index] == val:
return num_guesses
if to_check[mid_index] > val:
to_check = to_check[:mid_index]
else:
to_check = to_check[(mid_index):]
if __name__ == '__main__':
import doctest
if doctest.testmod().failed == 0:
print "\n*** ALL TESTS PASSED. YOU'RE TERRIFIC AT THIS!\n"