Popcorn Hack 1

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

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:     # Changed > to >= to include exactly 12
        full = full + 1

print("Full workshops:", full)

# My prediction before running: 1 (because only 15 was strictly greater than 12)
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Popcorn Hack 3

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: replace pass with an if that is True when signed_up[i] < capacity
    if signed_up[i] < capacity:
        # TODO 2: inside your if, append workshops[i] to open_workshops
        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)
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

Homework Hack: Supply Closet 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 whether the item is below its target and record the shortage
    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
# Add each shortage to the total
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)
# Find the largest shortage and remember its position
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
# Print the restock report or a fully stocked message
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[max_index] + " (short by " + str(short_amounts[max_index]) + ")")
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...