Popcorn Hack 1
a) The length of numList must be even b) The list numList must not contain any duplicate values c) The values in numList must be in sorted order d) The value of target must not be equal to -1
Correct Answer: C
Explanation:
Binary search works by dividing the list in half repeatedly. In order for this to work, the list must be sorted. If the list isn’t sorted, binary search won’t know where to look next and could give incorrect results.
Popcorn Hack 2
a) Binary search takes more time on average than linear search
b) Binary search cannot be used on unsorted lists without modifications
c) Binary search always returns the first occurrence of the target
d) Binary search can only be used on lists with unique values
Correct Answer: B
Explanation
Binary search works by repeatedly dividing a sorted list in half. If the list isn’t sorted, the logic breaks and you could miss the target entirely. That’s why sorting is needed before using binary search.
Popcorn Hack 3
def binary_search(arr, target):
left = 0
right = len(arr) - 1
while left <= right:
mid = (left + right) // 2
if arr[mid] == target:
return mid # Target found, return index
elif arr[mid] < target:
left = mid + 1 # Target is in the right half
else:
right = mid - 1 # Target is in the left half
return -1 # Target not found
# Example usage
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
print(binary_search(letters, 'c')) # Output: 2
2
Homework Hack
import pandas as pd
# Load the dataset
data = pd.read_csv("school_supplies.csv")
# Drop rows with missing values
data_cleaned = data.dropna()
# Sort the data by 'Price'
data_sorted = data_cleaned.sort_values(by="Price")
# Extract sorted prices as a list
price_list = data_sorted["Price"].tolist()
# Preview the sorted data and row counts
print("First few rows of sorted data:")
print(data_sorted.head())
print("Original row count:", len(data))
print("Cleaned row count:", len(data_cleaned))
# Binary search function to find a price
def binary_search(arr, target):
left = 0
right = len(arr) - 1
while left <= right:
mid = (left + right) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
left = mid + 1
else:
right = mid - 1
return -1
search_prices = [1.25, 6.49, 10.00]
for price in search_prices:
index = binary_search(price_list, price)
if index != -1:
print(f"Price ${price} found at index {index} in the sorted list.")
else:
print(f"Price ${price} not found in the list.")
First few rows of sorted data:
Product Price
5 Eraser 0.50
14 Paper Clips 0.89
2 Pencil 0.99
9 Glue Stick 1.25
1 Pen 1.50
Original row count: 15
Cleaned row count: 15
Price $1.25 found at index 3 in the sorted list.
Price $6.49 found at index 12 in the sorted list.
Price $10.0 not found in the list.
How it Works
The program uses Pandas to read and cleanse a dataset by removing rows that contain missing values. It then sorts the data based on price and creates a list of the sorted prices. A binary search function is used to quickly find individual price values and reports whether each was found or not.