-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbinary-search.py
57 lines (49 loc) · 1.36 KB
/
binary-search.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
def binary_search(A, target):
A = str(A)
lo = 0
hi = len(A)
index = None
while lo <= hi:
mid = round(lo + (hi - lo) / 2)
mid_ele = int(A[mid])
if mid_ele == target:
index = mid
break
if mid_ele < target:
lo = mid + 1
else:
hi = mid - 1
if index is not None:
return index
return "Not Found!"
def shipWithinDays(weights, D):
left, right = max(weights), sum(weights)
while left < right:
mid, need, cur = (left + right) / 2, 1, 0
for w in weights:
if cur + w > mid:
need += 1
cur = 0
cur += w
if need > D:
left = mid + 1
else:
right = mid
return round(left)
def binary_search_with_recursion(A, target):
low = A[0]
high = A[-1]
mid = (low + high) // 2
try:
if mid == target:
return A.index(mid)
if mid < target:
return binary_search_with_recursion(A[mid + 1: high], target)
if mid > target:
return binary_search_with_recursion(A[low: mid - 1], target)
except:
return "Not Found!"
if __name__ == "__main__":
# print(binary_search(5567889, 8))
# print(shipWithinDays([3,2,2,4,1,4],3))
print(binary_search_with_recursion([2, 2, 2, 4, 1, 4], 3))