forked from codebasics/data-structures-algorithms-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_search_exercise_solution.py
More file actions
62 lines (50 loc) · 2.18 KB
/
binary_search_exercise_solution.py
File metadata and controls
62 lines (50 loc) · 2.18 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
54
55
56
57
58
59
60
61
62
############ BINARY SEARCH EXERCISE SOLUTION: CODEBASICS YOUTUBE CHANNEL ####################
################### PROBLEM 1 #######################
# When I try to find number 5 in below list using binary search, it doesn't work and returns me -1 index. Why is that?
# numbers = [1,4,6,9,10,5,7]
# Answer: It is because the list is not sorted! Binary search requires that list has to be sorted
################### PROBLEM 2 #######################
# Problem: Find index of all the occurances of a number from sorted list
# Solution here tries to find an index of a number using binary search. Now since the list is sorted,
# it can do left and right scan from the initial index to find all occurances of a given element
# This method is not most efficient and I want you to figure out even a better way of doing it. In
# any case below method is still effective
def binary_search(numbers_list, number_to_find):
left_index = 0
right_index = len(numbers_list) - 1
mid_index = 0
while left_index <= right_index:
mid_index = (left_index + right_index) // 2
mid_number = numbers_list[mid_index]
if mid_number == number_to_find:
return mid_index
if mid_number < number_to_find: # this means number is in right hand side of the list
left_index = mid_index + 1
else: # number to find is on left hand side of the list
right_index = mid_index - 1
return -1
def find_all_occurances(numbers, number_to_find):
index = binary_search(numbers, number_to_find)
indices = [index]
# find indices on left hand side
i = index-1
while i >=0:
if numbers[i] == number_to_find:
indices.append(i)
else:
break
i = i - 1
# find indices on right hand side
i = index + 1
while i<len(numbers):
if numbers[i] == number_to_find:
indices.append(i)
else:
break
i = i + 1
return sorted(indices)
if __name__ == '__main__':
numbers = [1,4,6,9,11,15,15,15,17,21,34,34,56]
number_to_find = 15
indices = find_all_occurances(numbers, number_to_find)
print(f"Indices of occurances of {number_to_find} are {indices}")