Python 3.09 - Developing Algorithms HW
Python 3.09 Developing Algorithms
Completed all three Popcorn Hacks, the four-question MCQ, and the cafeteria-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
# Study hours for four days.
hours = [3, 5, 2, 4]
total = 0
for h in hours:
total = total + h
average = total / len(hours)
print(total)
print(average)
Popcorn Hack 2: Predict, Then Fix
Prediction before fixing: 1, because count > capacity only counts 15 and leaves out the boundary value 12.
Fix: change > to >=, so both full clubs are counted and it prints 2.
Code Runner Challenge
3.09 Popcorn 2 - Fix the full-club counter
View IPYNB Source
# CODE_RUNNER: 3.09 Popcorn 2 - Fix the full-club counter
# A club with exactly 12 members is also full, so the boundary must be included.
members = [12, 7, 15, 9]
capacity = 12
full = 0
for count in members:
if count >= capacity:
full = full + 1
print("Full clubs:", full)
Popcorn Hack 3: Modify a Counter into a Collector
Instead of counting matches, this algorithm collects the names of clubs that still have fewer than 12 members.
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
clubs = ["Chess", "Robotics", "Art", "Drama"]
members = [12, 7, 15, 9]
capacity = 12
open_clubs = []
for i in range(len(clubs)):
if members[i] < capacity:
open_clubs.append(clubs[i])
print("Clubs with open spots:", len(open_clubs))
print(open_clubs)
MCQ 3.09
Answers: C, B, C, B
Score: 4/4
- C — 4: the unconditional increment runs three times, plus one extra increment for the
$120gift. - B —
top_b:.sort()changes the original list in place, so it has a side effect. - C — 10: the boundary value is where
< 10and<= 10give different answers. - B — Modified an existing algorithm: the loop and condition stay the same while the tracked result changes.
Homework Hack: Cafeteria Restock Report
This combines three known algorithms: collect shortages, sum them with a loop, and find the maximum shortage. Items exactly at their target are not included.
Code Runner Challenge
3.09 Homework - cafeteria restock report
View IPYNB Source
# CODE_RUNNER: 3.09 Homework - cafeteria restock report
items = ["Milk", "Apples", "Rice", "Cereal", "Napkins"]
in_stock = [40, 8, 25, 5, 30]
target = [30, 20, 25, 15, 30]
# Step 1: COLLECT every item below its target, plus how many are missing.
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 missing amounts with a loop.
total_needed = 0
for amount in short_amounts:
total_needed = total_needed + amount
# Step 3: FIND THE MAX missing amount and remember where it was.
biggest = 0
for i in range(len(short_amounts)):
if short_amounts[i] > short_amounts[biggest]:
biggest = i
# Step 4: REPORT the results, including the fully-stocked case.
if len(short_items) == 0:
print("Fully stocked!")
else:
print("Restock needed:")
for i in range(len(short_items)):
print(short_items[i] + ": need " + str(short_amounts[i]))
print("Total items needed:", total_needed)
print("Most urgent: " + short_items[biggest] + " (short by " + str(short_amounts[biggest]) + ")")
Expected Homework Output
Restock needed:
Apples: need 12
Cereal: need 10
Total items needed: 22
Most urgent: Apples (short by 12)