← Back to course
Code Crew G5 / Week 12 / Wednesday
3/6
Week 12 Β· Ship it

Wednesday

Find it fast
// Build, test and graduate
⏱ about 15 min

Wednesday: Find it fast

The fleet's supply list will grow to hundreds of items. Rocket wants searching to stay quick.

"Linear search would check them one by one," he says. "We can do better."

Raven points to week 7 in her notebook. "What do you notice that binary search needs?"

"A sorted list!" Rocket says. "And sorted(inventory) gives us the names in order."

Nova sparkles. "Here is a hint. Check the middle, then keep the half that could hold the target."

Part 4: find an item fast

# Part 4: find an item fast
def find_item(names, target):
    low = 0
    high = len(names) - 1
    while low <= high:
        mid = (low + high) ____ 2
        if names[mid] == target:
            return mid
        elif names[mid] < target:
            low = ____
        else:
            high = mid - 1
    return -1
First blank: divide and drop the remainder
Second blank: the target is right of mid, so low becomes

Test drive

inventory = {
    "water": 40,
    "seeds": 12,
    "lamps": 3,
    "maps": 7,
    "rope": 2,
    "tools": 15,
}

def find_item(names, target):
    low = 0
    high = len(names) - 1
    while low <= high:
        mid = (low + high) // 2
        if names[mid] == target:
            return mid
        elif names[mid] < target:
            low = mid + 1
        else:
            high = mid - 1
    return -1

names = sorted(inventory)
print(names)
print(find_item(names, "seeds"))
print(find_item(names, "tea"))
Output:
['lamps', 'maps', 'rope', 'seeds', 'tools', 'water']
3
-1
Using the same names list, what does find_item(names, "water") return?
WHY THIS EXERCISEfind_item returns the spot number in the sorted list.
THINK ABOUT FIND_ITEM.
  • Read the question.
  • Tap your answer.
Why do we call sorted(inventory) first?
Which test checks the edge case?

Practice lab: find five names

def find_item(names, target):
    low = 0
    high = len(names) - 1
    while low <= high:
        mid = (low + high) // 2
        if names[mid] == target:
            return mid
        elif names[mid] < target:
            low = mid + 1
        else:
            high = mid - 1
    return -1

names = ["fuel", "maps", "rope", "tape", "wire"]
print(find_item(names, "fuel"))
print(find_item(names, "wire"))
print(find_item(names, "glue"))
What does the first print line show?
What does the second print line show?
What does the third print line show?
How many checks does it take to find rope?
FIND CHECK
  • Read the question.
  • Tap your answer.
Is "maps" < "rope" True or False?
What is the first mid spot for these 5 names?
Why not use linear search for hundreds of supplies?
StatementTrue or false?
names is sorted A to Z.?
find_item returns the name it found.?
Searching for glue ends when low passes high.?
find_item is the same code as week 7.?
WHY THIS EXERCISEThe tracker reuses the binary search you already know.
What does find_item(sorted(inventory), "lamps") return?
WHY THIS EXERCISEsorted(inventory) starts with lamps, in spot 0.

Part 4 is in! Tomorrow we count the crew with recursion and add more supplies.

← Tuesday