Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions search-Weston.py
Original file line number Diff line number Diff line change
@@ -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))
46 changes: 46 additions & 0 deletions search.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import math
"""
Example 1:
Input: nums = [-1, 0, 3, 5, 9, 12], target = 9
Expand All @@ -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
32 changes: 32 additions & 0 deletions search2.py
Original file line number Diff line number Diff line change
@@ -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::]