Content-Length: 7176 | pFad | http://github.com/codebasics/data-structures-algorithms-python/pull/90.diff
thub.com diff --git a/1.py b/1.py new file mode 100644 index 0000000..860e313 --- /dev/null +++ b/1.py @@ -0,0 +1,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 iFetched URL: http://github.com/codebasics/data-structures-algorithms-python/pull/90.diff
Alternative Proxies: