-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary-search.py
More file actions
executable file
·43 lines (35 loc) · 850 Bytes
/
binary-search.py
File metadata and controls
executable file
·43 lines (35 loc) · 850 Bytes
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
# -*- coding: utf-8 -*-
"""
Created on Wed Dec 18 14:41:12 2019
@author: USER
"""
# SOLVED!
"""
1, 2, 3, 4, 5, 6, 7
"""
def binary_search(l, num):
start = 0
end = len(l)-1
while start < end:
mid = (end + start)//2
if l[mid] == num:
return True
elif num > l[mid]:
start = mid+1
else:
end = mid
return False
def binary_recur(l, num):
half = len(l) // 2
if len(l) == 1 and l[half] != num:
return False
elif l[half]==num:
return True
else:
left = l[:half]
right = l[half:]
return binary_recur(left, num) or binary_recur(right, num)
if __name__ == "__main__":
l = [1, 2, 3, 4, 5, 6, 7, 9]
# print(binary_recur(l, 10))
print(binary_search(l, -7))