From fe0d61cd477336a58fe0cc0b2c279a33eba55708 Mon Sep 17 00:00:00 2001 From: andrewbantly Date: Fri, 12 May 2023 18:44:22 -0700 Subject: [PATCH 1/3] algorithm built, deliverable complete --- search.py | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ search2.py | 41 +++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 search2.py diff --git a/search.py b/search.py index 1c03df0..81e8e44 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,65 @@ 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 + elif int(len(nums)) == 2: + # print(f"nums list: {nums}") + 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") + 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: + # 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 + 1):] + 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..dcc626d --- /dev/null +++ b/search2.py @@ -0,0 +1,41 @@ +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 = 92 +index_list = [] + +search = True +# have a loop that repeats for each step +while search: +# an equation that identifies the middle of a list + index = math.floor(len(nums) / 2) + 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 + elif int(len(nums)) == 2: + 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):] + elif nums[index] < target: + index_list.append(index) + nums = nums[index::] + 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 + 1):] + elif nums[index] < target: + index_list.append(index) + nums = nums[index::] \ No newline at end of file From 7e1f8fba40b1513965fe3e77a198a887cfdd707b Mon Sep 17 00:00:00 2001 From: andrewbantly Date: Fri, 12 May 2023 18:52:39 -0700 Subject: [PATCH 2/3] refactored solution --- search.py | 19 +------------------ search2.py | 17 ++++------------- 2 files changed, 5 insertions(+), 31 deletions(-) diff --git a/search.py b/search.py index 81e8e44..9d0ac66 100644 --- a/search.py +++ b/search.py @@ -43,23 +43,6 @@ print("number doesn't exist") print("return -1") search = False - elif int(len(nums)) == 2: - # print(f"nums list: {nums}") - 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") - 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: # if matched, elif < or > create new list if nums[index] == target: @@ -71,7 +54,7 @@ print("move left in the list") # index_list.append(index) print(f"index list: {index_list}") - nums = nums[:(index + 1):] + nums = nums[:index:] elif nums[index] < target: print(f"num: {nums[index]}") index_list.append(index) diff --git a/search2.py b/search2.py index dcc626d..87e45eb 100644 --- a/search2.py +++ b/search2.py @@ -1,14 +1,15 @@ 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 = 92 +target = 63 index_list = [] search = True # have a loop that repeats for each step while search: -# an equation that identifies the middle of a list + # 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 @@ -18,16 +19,6 @@ print("number doesn't exist") print("return -1") search = False - elif int(len(nums)) == 2: - 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):] - elif nums[index] < target: - index_list.append(index) - nums = nums[index::] else: # if matched, elif < or > create new list if nums[index] == target: @@ -35,7 +26,7 @@ print(f"{target} is found at index {output}") search = False elif nums[index] > target: - nums = nums[:(index + 1):] + nums = nums[:index:] elif nums[index] < target: index_list.append(index) nums = nums[index::] \ No newline at end of file From a75f006e8a9dc044f8afb5f179511430c662e5af Mon Sep 17 00:00:00 2001 From: andrewbantly Date: Mon, 15 May 2023 10:15:41 -0700 Subject: [PATCH 3/3] deliverable review with Weston --- search-Weston.py | 56 ++++++++++++++++++++++++++++++++++++++++++++++++ search2.py | 2 +- 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 search-Weston.py 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/search2.py b/search2.py index 87e45eb..36d6ad5 100644 --- a/search2.py +++ b/search2.py @@ -26,7 +26,7 @@ print(f"{target} is found at index {output}") search = False elif nums[index] > target: - nums = nums[:index:] + 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