3.09 Developing Algorithms HW
Popcorn Hack 1: Translate Pseudocode to Python
FOR EACH h IN hours becomes for h in hours:, LENGTH(hours) becomes len(hours), and ← becomes =. Expected: 14 and 3.5.
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]
# Step 2: start total at 0
total = 0
# Step 3: FOR EACH -> for ... in ...: 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: print total, then average
print(total)
print(average)
Actual output: 14, then 3.5.
Popcorn Hack 2: Predict, Then Fix
Prediction: Full workshops: 1. Only Fitness (15) is greater than 12; Self-Defense has exactly 12, which > does not count.
The bug: at the boundary (exactly 12 of 12 seats), > and >= disagree. A workshop with 12 signed up is full, so the comparison must be >=.
Code Runner Challenge
3.09 Popcorn 2 - Predict, then fix the full-workshop counter
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:
if count >= capacity: # fixed: > became >= so EXACTLY 12 counts as full
full = full + 1
print("Full workshops:", full)
# My prediction before running (with the original >): 1
# After the fix (>=): 2
Actual output (fixed): Full workshops: 2. The original > version printed 1.
Popcorn Hack 3: Modify the Counter into a Collector
To go from counting to collecting, keep the loop and the if, and change what is tracked: matches.append(item) instead of count = count + 1.
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"]
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
# TODO 1: an if that is True when signed_up[i] < capacity
if signed_up[i] < capacity:
# TODO 2: append the workshop name
open_workshops.append(workshops[i])
# TODO 3: print how many there are, then the list
print("Workshops with open seats:", len(open_workshops))
print(open_workshops)
Actual output:
Code Runner Challenge
3.09 Homework - Safe Passage Heals supply restock report
View IPYNB Source
Workshops with open seats: 2
['Parenting Skills', 'Self-Sufficiency']
MCQ Check
The four MCQ questions are an interactive widget on the lesson page. Answer them there, then paste the copied result line here and in the Notes field:
MCQ 3.09: <paste your score line>
Homework Hack: Supply Closet Restock Report
Combines three patterns: collect the items below target, sum the shortages, and find the max shortage. Socks has exactly 25 of a 25 target, so it is not short (the boundary again).
# 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
short_items = []
short_amounts = []
for i in range(len(items)):
# only strictly below target counts, so Socks (25 of 25) is not short
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 loop (no sum())
total_needed = 0
for amount in short_amounts:
total_needed = total_needed + amount
# Step 3: FIND THE MAX shortage (track its position so we know the item's name)
biggest_position = 0
for j in range(len(short_amounts)):
if short_amounts[j] > short_amounts[biggest_position]:
biggest_position = j
# Step 4: COMBINE: "Fully stocked!" if nothing is short, otherwise the report
if len(short_items) == 0:
print("Fully stocked!")
else:
print("Restock needed:")
for k in range(len(short_items)):
print(short_items[k] + ": need " + str(short_amounts[k]))
print("Total items needed: " + str(total_needed))
print("Most urgent: " + short_items[biggest_position] + " (short by " + str(short_amounts[biggest_position]) + ")")
Actual output:
Restock needed:
Blankets: need 12
Shampoo: need 10
Total items needed: 22
Most urgent: Blankets (short by 12)
Edge case check: if every in_stock value equals or exceeds its target, both lists stay empty, the max loop never runs, and the if len(short_items) == 0 branch prints Fully stocked! instead of crashing on an empty list.
Submission Notes
Lesson: CSP 3.09 Developing Algorithms
Notebook: <published URL>
MCQ 3.09: <paste your score line>
Popcorn Hacks 1-3 completed in class and copied to notebook: yes
Homework output: Restock needed / Blankets: need 12 / Shampoo: need 10 / Total items needed: 22 / Most urgent: Blankets (short by 12)