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 i elements[j+1]: + tmp = elements[j] + elements[j] = elements[j+1] + elements[j+1] = tmp + swapped = True + + if not swapped: + break + +def bubble_sort_by_key(elements, key): + size = len(elements) + + for i in range(size-1): + swapped = False + for j in range(size-1-i): + if elements[j][key] > elements[j+1][key]: + tmp = elements[j] + elements[j] = elements[j+1] + elements[j+1] = tmp + swapped = True + + if not swapped: + break + + +elements = [5,9,2,1,67,34,88,34] +elements = [1,2,3,4,2] +elements = ["mona", "dhaval", "aamir", "tina", "chang"] + +bubble_sort(elements) +print(elements) + +elements2 = [ { 'name': 'kathy', 'transaction_amount': 200, 'device': 'vivo'}, + { 'name': 'dhaval', 'transaction_amount': 400, 'device': 'google pixel'}, + { 'name': 'aamir', 'transaction_amount': 800, 'device': 'iphone-8'}, + { 'name': 'mona', 'transaction_amount': 1000, 'device': 'iphone-10'}, + ] +bubble_sort_by_key(elements2,key='transaction_amount') +print(elements2) diff --git a/util.py b/util.py new file mode 100644 index 0000000..2a7d2d5 --- /dev/null +++ b/util.py @@ -0,0 +1,9 @@ +import time +def time_it(func): + def wrapper(*args, **kwargs): + start = time.time() + result = func(*args,**kwargs) + end = time.time() + print(func.__name__ +" took " + str((end-start)*1000) + " mil sec") + return result + return wrapper \ No newline at end of file pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy