Python 3.09: Developing Algorithms HW
Popcorn Hack 1: Translate Pseudocode to Python
Prediction: 3 + 5 + 2 + 4 = 14, and 14 / 4 = 3.5. Each step has a comment showing the pseudocode it came from.
Code Runner Challenge
3.09 Popcorn 1 - Translate the pseudocode into Python
View IPYNB Source
# CODE_RUNNER: 3.09 Popcorn 1 - Translate the pseudocode into Python
# Step 1: make the hours list (hours ← [3, 5, 2, 4])
hours = [3, 5, 2, 4]
# Step 2: start total at 0 (total ← 0)
total = 0
# Step 3: FOR EACH h IN hours -> for h in hours: add each h to total
for h in hours:
total = total + h
# Step 4: LENGTH(hours) -> len(hours): compute the average
average = total / len(hours)
# Step 5: DISPLAY -> print: total first, then average
print(total)
print(average)
Actual output:
Code Runner Challenge
3.09 Popcorn 2 - Predict, then fix the full-workshop counter
View IPYNB Source
14
3.5
Popcorn Hack 2: Predict, Then Fix
Prediction for the original code: 1. Only Fitness (15) passed count > capacity, because Self-Defense has exactly 12, and 12 > 12 is False.
Original output: Full workshops: 1, which matches my prediction and is wrong, since 2 workshops are full.
Fix: change > to >= so the boundary value (exactly 12 of 12) counts as full.
Code Runner Challenge
3.09 Popcorn 3 - Modify the counter into a collector
View IPYNB Source
# CODE_RUNNER: 3.09 Popcorn 2 - Predict, then fix the full-workshop counter
signed_up = [12, 7, 15, 9] # Self-Defense, Parenting Skills, Fitness, Self-Sufficiency
capacity = 12
full = 0
for count in signed_up:
# FIX: > became >=. With >, a workshop at EXACTLY 12 of 12 wasn't counted as full.
if count >= capacity:
full = full + 1
print("Full workshops:", full)
# My prediction before running the original: 1 (only Fitness, because 12 > 12 is False)
Actual output:
Code Runner Challenge
3.09 Homework - Safe Passage Heals supply restock report
View IPYNB Source
Full workshops: 2
Popcorn Hack 3: Modify the Counter Into a Collector
Modify: same loop and same if as a counter, but count = count + 1 becomes open_workshops.append(...), and len() still gives the count.
Code Runner Challenge
3.09 Homework Test - fully stocked closet
View IPYNB Source
# CODE_RUNNER: 3.09 Popcorn 3 - Modify the counter into a collector
workshops = ["Self-Defense", "Parenting Skills", "Fitness", "Self-Sufficiency"]
signed_up = [12, 7, 15, 9] # signed_up[0] goes with workshops[0], and so on
capacity = 12
open_workshops = [] # COLLECT names instead of counting
for i in range(len(workshops)): # i = 0, 1, 2, 3
# Same loop and if as a counter; only what we track changed (append instead of +1).
# < (not <=) so Self-Defense, at exactly 12 of 12, is NOT open.
if signed_up[i] < capacity:
open_workshops.append(workshops[i])
# len() gives the count, so the collector still answers "how many?"
print("Workshops with open seats:", len(open_workshops))
print(open_workshops)
Actual output:
Workshops with open seats: 2
['Parenting Skills', 'Self-Sufficiency']
Self-Defense is correctly left out, because it’s at exactly 12 and 12 < 12 is False.
MCQ 3.09
MCQ 3.09: ___/4 (paste your score line from the lesson’s MCQ check here)
Homework Hack: Supply Closet Restock Report
Combines three patterns: collect the items below target, sum the shortages with a loop, and find the max shortage by tracking its position. The collector’s result then decides whether to print “Fully stocked!” or the report. There’s a comment above every block.
# CODE_RUNNER: 3.09 Homework - Safe Passage Heals supply restock report
items = ["Toothbrushes", "Blankets", "Socks", "Shampoo", "Notebooks"]
in_stock = [40, 8, 25, 5, 30] # in the closet now
target = [30, 20, 25, 15, 30] # what staff want on hand
# Step 1: COLLECT items below target, and how many of each are needed.
# < (not <=) so Socks, at exactly 25 of 25, is not counted as short.
short_items = []
short_amounts = []
for i in range(len(items)):
if in_stock[i] < target[i]:
short_items.append(items[i])
short_amounts.append(target[i] - in_stock[i])
# Step 2: SUM the shortages with a running total (no sum()).
total_needed = 0
for amount in short_amounts:
total_needed = total_needed + amount
# Step 3: FIND THE MAX shortage. Track the position, not just the value,
# so the matching item name can be looked up in short_items.
# Only runs when something is short, because short_amounts[0] would fail on an empty list.
if len(short_amounts) > 0:
max_index = 0
for i in range(len(short_amounts)):
if short_amounts[i] > short_amounts[max_index]:
max_index = i
# Step 4: COMBINE: the collector's result decides which report prints.
if len(short_items) == 0:
print("Fully stocked!")
else:
print("Restock needed:")
for i in range(len(short_items)):
print(f"{short_items[i]}: need {short_amounts[i]}")
print(f"Total items needed: {total_needed}")
print(f"Most urgent: {short_items[max_index]} (short by {short_amounts[max_index]})")
Actual output:
Restock needed:
Blankets: need 12
Shampoo: need 10
Total items needed: 22
Most urgent: Blankets (short by 12)
This matches the expected output exactly. Socks (25 of 25) is not listed, because 25 < 25 is False, which is the boundary again.
Test: Fully Stocked Closet
Same algorithm, with every item at or above its target, to prove the “Fully stocked!” branch works.
# CODE_RUNNER: 3.09 Homework Test - fully stocked closet
items = ["Toothbrushes", "Blankets", "Socks", "Shampoo", "Notebooks"]
in_stock = [40, 20, 25, 15, 30] # TEST: every item at or above target
target = [30, 20, 25, 15, 30] # what staff want on hand
# Step 1: COLLECT items below target, and how many of each are needed.
# < (not <=) so Socks, at exactly 25 of 25, is not counted as short.
short_items = []
short_amounts = []
for i in range(len(items)):
if in_stock[i] < target[i]:
short_items.append(items[i])
short_amounts.append(target[i] - in_stock[i])
# Step 2: SUM the shortages with a running total (no sum()).
total_needed = 0
for amount in short_amounts:
total_needed = total_needed + amount
# Step 3: FIND THE MAX shortage. Track the position, not just the value,
# so the matching item name can be looked up in short_items.
# Only runs when something is short, because short_amounts[0] would fail on an empty list.
if len(short_amounts) > 0:
max_index = 0
for i in range(len(short_amounts)):
if short_amounts[i] > short_amounts[max_index]:
max_index = i
# Step 4: COMBINE: the collector's result decides which report prints.
if len(short_items) == 0:
print("Fully stocked!")
else:
print("Restock needed:")
for i in range(len(short_items)):
print(f"{short_items[i]}: need {short_amounts[i]}")
print(f"Total items needed: {total_needed}")
print(f"Most urgent: {short_items[max_index]} (short by {short_amounts[max_index]})")
Actual output:
Fully stocked!
Why it works this way: The max finder is inside if len(short_amounts) > 0, because starting at short_amounts[0] would crash on an empty list. Because short_items is empty, the combine step prints “Fully stocked!” instead of the report, so there’s no error and no wrong answer.