-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy path1.py
105 lines (78 loc) · 3.16 KB
/
1.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# ### Binary Search Exercise
# 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]```
# This is because the array is not sorted in order from lowest to highest.
# Once it splits the first time, it starts looking in the [1,4,6] range and doesn't find 5
# 1. Find index of all the occurances of a number from sorted list
# ```
# numbers = [1,4,6,9,11,15,15,15,17,21,34,34,56]
# number_to_find = 15
# ```
# This should return 5,6,7 as indices containing number 15 in the array
from util import time_it
@time_it
def linear_search(numbers_list, number_to_find):
for index, element in enumerate(numbers_list):
if element == number_to_find:
return index
return -1
@time_it
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:
left_index = mid_index + 1
else:
right_index = mid_index - 1
return -1
def binary_search_recursive(numbers_list, number_to_find, left_index, right_index):
if right_index < left_index:
return -1
mid_index = (left_index + right_index) // 2
if mid_index >= len(numbers_list) or mid_index < 0:
return -1
mid_number = numbers_list[mid_index]
if mid_number == number_to_find:
return mid_index
if mid_number < number_to_find:
left_index = mid_index + 1
else:
right_index = mid_index - 1
return binary_search_recursive(numbers_list, number_to_find, left_index, right_index)
#this should run the binary search, find the index, and then recursively run the search on both the right and left side
def binary_search_multiple(numbers_list, number_to_find):
index = binary_search(numbers_list,number_to_find)
result_indices = [index]
# find all indices on the left
i = index - 1
while i>=0:
if numbers_list[i] == numbers_list[index]:
result_indices.append(i)
else:
break
i = i-1
# find all indices on the right
i = index + 1
while i<len(numbers_list):
if numbers_list[i] == numbers_list[index]:
result_indices.append(i)
else:
break
i = i+1
return sorted(result_indices)
numbers_list = [12, 15, 17, 19, 21, 21, 21, 21, 24, 45, 67]
number_to_find = 21
index = binary_search_multiple(numbers_list, number_to_find)
print(f"Number found at index {index} using binary search")
numbers = [1,4,6,9,11,15,15,15,15,17,21,34,34,56]
number_to_find = 15
index = binary_search_multiple(numbers, number_to_find)
print(f"Number found at index {index} using binary search")
#Lesson: I was approaching it wrong. If something isn't working, scratch the approach.
#Lesson #2: Try the simplest solution first. Although in this case it's a bit ugly since you're just doing a linear search after your binary search