3.09 Developing Algorithms HW
Popcorn Hack 1: Translate Pseudocode to Python
The volunteer-hours pseudocode, translated step by step with a for loop instead of sum().
Prediction: 14, then 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)
Output: 14, then 3.5
Popcorn Hack 2: Predict, Then Fix
Prediction before running: Full workshops: 1. The check is count > capacity, and 12 is not greater than 12, so Self-Defense is skipped and only Fitness (15) is counted.
The fix: change > to >= so a workshop with exactly 12 people counts as full.
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: >= so exactly 12 counts as full
full = full + 1
print("Full workshops:", full)
# My prediction before running: 1 (the original > skipped the workshop at exactly 12)
Output after the fix: Full workshops: 2
Popcorn Hack 3: Modify the Counter into a Collector
Same loop and same if as a counter, but it collects names with append instead of adding 1.
Prediction: Workshops with open seats: 2, then ['Parenting Skills', 'Self-Sufficiency']
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
if signed_up[i] < capacity:
open_workshops.append(workshops[i])
print("Workshops with open seats:", len(open_workshops))
print(open_workshops)
Output: Workshops with open seats: 2, then ['Parenting Skills', 'Self-Sufficiency']. Self-Defense is left out because it has exactly 12.
MCQ
| # | Question | My answer |
|---|---|---|
| 1 | What the gifts counter prints | C 4 |
| 2 | Which one has a side effect on the list | B top_b |
| 3 | Input that proves < 10 and <= 10 are not equivalent |
C 10 |
| 4 | Changing a counter so it appends items to a list | B Modified an existing algorithm |
MCQ 3.09: 4/4 | answers: C,B,C,B
Homework Hack: Supply Closet Restock Report
The report combines three patterns: it collects the items below target, sums the shortages with a loop, and finds the max shortage. Socks has exactly 25 of 25, so it is not short. If nothing is short, it prints Fully stocked! instead.
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] # 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)):
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 you know the item's name)
max_index = 0
for i in range(len(short_amounts)):
if short_amounts[i] > short_amounts[max_index]:
max_index = i
# Step 4: COMBINE: print "Fully stocked!" if nothing is short, otherwise the report
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]})")
Output:
Restock needed:
Blankets: need 12
Shampoo: need 10
Total items needed: 22
Most urgent: Blankets (short by 12)