diff --git a/search-Weston.py b/search-Weston.py new file mode 100644 index 0000000..d850833 --- /dev/null +++ b/search-Weston.py @@ -0,0 +1,56 @@ +""" +Example 1: +Input: nums = [-1, 0, 3, 5, 9, 12], target = 9 +Output: 4 +Explanation: 9 exists in nums and its index is 4 + +Example 2: +Input: nums = [-1, 0, 3, 5, 9, 12], target = 2 +Output: -1 +Explanation: 2 does not exist in nums so return -1 + +Example 3: +Input: nums = [-1, 0, 3, 5, 9, 12], target = 12 +Output: 5 +Explanation: 12 exists in the list and its index is 5 + +Example 4: +Input: nums = [-1, 0, 3, 5, 9, 12], target = -1 +Output: 0 +Explanation: -1 exists in the list and its index is 0 +""" + +# distance = high index - low index +# middle index = low index + (distance // 2) + +def binary_search(nums, target): + # keep track of the window -- start by assuming the entire list is the window + # need variables for low index of the window, high index of the window, middle point index of the window + low = 0 + high = len(nums) + mid = high // 2 + print(f"target: {target}") + # continously iterate until the target is found or until we determine the target cannot be found + while True: + # check the value at the midpoint against the target, stop iterating if target is found + if nums[mid] == target: + return mid + # if the value at the midpoint is greater than the target, slide the window to the left + elif nums[mid] > target: + high = mid + difference = high - low + mid = low + (difference // 2) + # otherwise slide to the right + else: + low = mid + difference = high - low + mid = low + (difference // 2) + print(f"low: {low}, mid: {mid}, high: {high}") + # stop iteration when the midpoint is equal to the low or high, because we did not find it + if mid <= low or mid >= high: + return -1 + + +nums = [-1, 0, 3, 5, 9, 12] +target = 9 +print(binary_search(nums, target)) \ No newline at end of file diff --git a/search.py b/search.py index 1c03df0..9d0ac66 100644 --- a/search.py +++ b/search.py @@ -1,3 +1,4 @@ +import math """ Example 1: Input: nums = [-1, 0, 3, 5, 9, 12], target = 9 @@ -19,3 +20,48 @@ Output: 0 Explanation: -1 exists in the list and its index is 0 """ + +nums = [-1, 0, 2, 3, 5, 9, 12] +target = 0 +# Output should be 4 +index_list = [] + +search = True +# have a loop that repeats for each step +while search: +# an equation that identifies the middle of a list + print(f"nums list: {nums}") + index = math.floor(len(nums) / 2) + print(f"middle index: {index}") + if int(len(nums)) == 1: + # print(f"nums list: {nums}") + if nums[index] == target: + output = sum(index_list) + index + print(f"{target} is found at index {output}") + search = False + else: + print("number doesn't exist") + print("return -1") + search = False + else: + # if matched, elif < or > create new list + if nums[index] == target: + output = sum(index_list) + index + print(f"{target} is found at index {output}") + search = False + elif nums[index] > target: + print(f"num: {nums[index]}") + print("move left in the list") + # index_list.append(index) + print(f"index list: {index_list}") + nums = nums[:index:] + elif nums[index] < target: + print(f"num: {nums[index]}") + index_list.append(index) + print("move right in the list") + print(f"index list: {index_list}") + nums = nums[index::] + else: + print("else statement triggered") + search = False +# repeat unless array length = 1, then it doesn't exist \ No newline at end of file diff --git a/search2.py b/search2.py new file mode 100644 index 0000000..36d6ad5 --- /dev/null +++ b/search2.py @@ -0,0 +1,32 @@ +import math + +nums = [-1, 0, 2, 3, 5, 9, 12, 29, 34, 43, 44, 59, 63, 65, 70, 71, 74, 80, 81, 88, 92, 95, 100] +target = 63 +index_list = [] + +search = True +# have a loop that repeats for each step +while search: + # an equation that identifies the middle index of a list + index = math.floor(len(nums) / 2) + # if the lenght of the list == 1, unique instructions because it's either the target number or the number doesn't exist + if int(len(nums)) == 1: + if nums[index] == target: + output = sum(index_list) + index + print(f"{target} is found at index {output}") + search = False + else: + print("number doesn't exist") + print("return -1") + search = False + else: + # if matched, elif < or > create new list + if nums[index] == target: + output = sum(index_list) + index + print(f"{target} is found at index {output}") + search = False + elif nums[index] > target: + nums = nums[:index:] # find a solution that isn't changing a list + elif nums[index] < target: + index_list.append(index) + nums = nums[index::] \ No newline at end of file