- Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbinary.py
More file actions
Latest commit
39 lines (30 loc) · 1.22 KB
/
Copy pathbinary.py
File metadata and controls
39 lines (30 loc) · 1.22 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
importmath
defbinary_search_1(unknown, diapason_floor, diapason_ceil, limit_of_search=None):
"""
:param unknown: the number what we search
:param diapason_floor: floor of slice
:param diapason_ceil: ceil of slice
:param limit_of_search: limit of steps of search
:return:
"""
diapason=list(range(diapason_floor, diapason_ceil+1))
counter=0
current_number=round(len(diapason)/2)
whileTrue:
counter+=1
ifunknown==current_number:
print('We found your number, is : ', current_number)
print('Number of steps : ', counter)
break
diapason_len=round(len(diapason)/2)
current_number=diapason[diapason_len]
# If the unknown number is less than the current
ifunknown>diapason[diapason_len]:
diapason=diapason[diapason_len:]
# If the unknown number is higher than the current
elifunknown<diapason[diapason_len]:
diapason=diapason[:diapason_len]
iflimit_of_searchisnotNoneandcounter==limit_of_search:
print('Maximum step limit exceeded : {limit}'.format(limit=limit_of_search))
break
binary_search_1(10, 0, 100, 200)