Python 3.09 Developing Algorithms

This notebook covers all three Popcorn Hacks, the four-question MCQ, and the Safe Passage Heals supply-restock Homework Hack.

Popcorn Hack 1: Translate Pseudocode to Python

The loop adds 3 + 5 + 2 + 4 for a total of 14, then divides by four values for an average of 3.5.

Code Runner Challenge

3.09 Popcorn 1 - Translate pseudocode into Python

View IPYNB Source
# CODE_RUNNER: 3.09 Popcorn 1 - Translate pseudocode into Python
# Step 1: store the volunteer hours in a list.
hours = [3, 5, 2, 4]

# Step 2: start the running total at zero.
total = 0

# Step 3: visit each value and add it to the running total.
for h in hours:
    total = total + h

# Step 4: divide by the number of values to compute the average.
average = total / len(hours)

# Step 5: display the required results.
print(total)
print(average)
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Popcorn Hack 2: Predict, Then Fix

Prediction before fixing: 1, because count > capacity counts only 15 and incorrectly excludes the boundary value 12.
Fix: change > to >=, which correctly counts both full_count workshops and prints 2.

Code Runner Challenge

3.09 Popcorn 2 - Fix the full_count-workshop counter

View IPYNB Source
# CODE_RUNNER: 3.09 Popcorn 2 - Fix the full_count-workshop counter
# The boundary matters: exactly 12 signed up is also full_count.
registration_counts = [12, 7, 15, 9]
capacity = 12

# Count every workshop at or above capacity.
full_count = 0
for count in registration_counts:
    if count >= capacity:
        full_count = full_count + 1

print("Full workshops:", full_count)
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Popcorn Hack 3: Modify a Counter into a Collector

Instead of only counting matches, this algorithm collects the names of workshops with fewer than 12 people signed up.

Code Runner Challenge

3.09 Popcorn 3 - Modify the counter into a collector

View IPYNB Source
# CODE_RUNNER: 3.09 Popcorn 3 - Modify the counter into a collector
workshops = ["Self-Defense", "Parenting Skills", "Fitness", "Self-Sufficiency"]
registration_counts = [12, 7, 15, 9]
capacity = 12

# Start with an empty collector for workshop names.
available_workshops = []

# Compare each workshop's signup count with the capacity.
for i in range(len(workshops)):
    if registration_counts[i] < capacity:
        available_workshops.append(workshops[i])

# Display both the number of matches and the collected names.
print("Workshops with open seats:", len(available_workshops))
print(available_workshops)
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

MCQ 3.09

Answers: C, B, C, B
Score: 4/4

  1. C — 4: the unconditional increment runs three times, plus one extra increment for the $120 gift.
  2. B — top_b: .sort() changes the original list in place, so it has a side effect.
  3. C — 10: the boundary value is where < 10 and <= 10 disagree.
  4. B — Modified an existing algorithm: the loop and condition stay the same while the tracked result changes.

Homework Hack: Supply Closet Restock Report

This combines three known algorithms: collect shortages, sum them with a loop, and find the maximum shortage. Items exactly at target are not included.

Code Runner Challenge

3.09 Homework - Safe Passage Heals supply restock report

View IPYNB Source
# CODE_RUNNER: 3.09 Homework - Safe Passage Heals supply restock report

items = ["Toothbrushes", "Blankets", "Socks", "Shampoo", "Notebooks"]
in_stock = [40, 8, 25, 5, 30]
target = [30, 20, 25, 15, 30]

# Step 1: COLLECT every item below target and its shortage amount.
restock_items = []
restock_amounts = []
for i in range(len(items)):
    if in_stock[i] < target[i]:
        restock_items.append(items[i])
        restock_amounts.append(target[i] - in_stock[i])

# Step 2: SUM the shortages with a loop instead of sum().
restock_total = 0
for amount in restock_amounts:
    restock_total = restock_total + amount

# Step 3: FIND THE MAX shortage while keeping its matching list position.
largest_shortage_index = 0
if len(restock_amounts) > 0:
    for i in range(1, len(restock_amounts)):
        if restock_amounts[i] > restock_amounts[largest_shortage_index]:
            largest_shortage_index = i

# Step 4: COMBINE the results into the final report, including the full_county-stocked case.
if len(restock_items) == 0:
    print("Fully stocked!")
else:
    print("Restock needed:")
    for i in range(len(restock_items)):
        print(restock_items[i] + ": need " + str(restock_amounts[i]))
    print("Total items needed:", restock_total)
    print("Most urgent: " + restock_items[largest_shortage_index] + " (short by " + str(restock_amounts[largest_shortage_index]) + ")")
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Expected Homework Output

Restock needed:
Blankets: need 12
Shampoo: need 10
Total items needed: 22
Most urgent: Blankets (short by 12)