-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinarysearch.py
45 lines (41 loc) · 1.14 KB
/
binarysearch.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
#binary search questions
def cieling_of_number(list1,target):
start = 0
end = len(list1)-1
while(start<=end):
mid = (start + end)//2
if(list1[mid] == target):
return list1[mid]
elif (list1[mid]<target):
start = mid +1
elif(list1[mid]>target):
end = mid-1
print(list1[start:end+1])
return list1[start]
def binary_search(list1,target):
start = 0
end = len(list1)-1
while(start<=end):
mid = (start + end)//2
if(list1[mid] == target):
return list1[mid]
elif (list1[mid]<target):
start = mid +1
elif(list1[mid]>target):
end = mid-1
print(list1[start:end+1])
return -1
def floor_of_number(list1,target):
start = 0
end = len(list1)-1
while(start<=end):
mid = (start + end)//2
if(list1[mid] == target):
return list1[mid]
elif (list1[mid]<target):
start = mid +1
elif(list1[mid]>target):
end = mid-1
print(list1[start:end+1])
return list1[end]
print(floor_of_number([12,13,17,18,24],15))