Popcorn Hack 1: Translate Pseudocode to Python

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)
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Popcorn Hack 2: Predict, Then Fix Full-Workshop Counter

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

# My prediction before running: The code will print 1 because 12 > 12 evaluates to False for Self-Defense.
# Fix: Change '>' to '>=' so exact capacity (12) is included as full.

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 boundary condition from > to >=
    full = full + 1

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

Popcorn Hack 3: Modify Counter into Collector

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
  # 1: check if signed_up[i] is strictly less than capacity
  if signed_up[i] < capacity:
    # 2: inside your if, append workshops[i] to open_workshops
    open_workshops.append(workshops[i])

# 3: print how many there are, then the list
print("Workshops with open seats:", len(open_workshops))
print(open_workshops)
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

MCQ

MCQ 3.09: 4/4 | answers: C,B,C,B

  1. C
  2. B
  3. C
  4. B

Homework Hack: Safe Passage Heals Supply Restock Report

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)):
  # Check if in-stock amount is strictly less than target amount
  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(1, 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"
      f" {short_amounts[max_index]})"
  )
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...