3.09 Developing Algorithms HW
3.09 Developing Algorithms Homework
POPCORN HACK 1
Code Runner Challenge
POPCORN HACK 1
View IPYNB Source
# CODE_RUNNER: POPCORN HACK 1
# 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
POPCORN HACK 2
View IPYNB Source
# CODE_RUNNER: POPCORN HACK 2
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: exactly 12 is full
full = full + 1
print("Full workshops:", full)
# My prediction before running the original code: 1
# Output after the fix: 2
Lines: 1
Characters: 0
Output
Click "Run" in code control panel to see output ...
POPCORN HACK 3
Code Runner Challenge
POPCORN HACK 3
View IPYNB Source
# CODE_RUNNER: POPCORN HACK 3
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
# Check if this workshop still has open seats.
if signed_up[i] < capacity:
# Append the matching workshop name to the collector list.
open_workshops.append(workshops[i])
# Print how many open workshops 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 Check
HOMEWORK HACK
# Trace version: shows how key variables change after each loop step.
items = ["Toothbrushes", "Blankets", "Socks", "Shampoo", "Notebooks"]
in_stock = [40, 8, 25, 5, 30]
target = [30, 20, 25, 15, 30]
# Step 1: COLLECT items below target, and how many of each are needed.
short_items = []
short_amounts = []
for i in range(len(items)):
shortage = target[i] - in_stock[i]
if in_stock[i] < target[i]:
short_items.append(items[i])
short_amounts.append(shortage)
print(f"collect step {i}: item={items[i]}, shortage={shortage}, short_items={short_items}, short_amounts={short_amounts}")
# Step 2: SUM the shortages with a loop.
total_needed = 0
for amount in short_amounts:
total_needed = total_needed + amount
print(f"sum step: added={amount}, total_needed={total_needed}")
# Step 3: FIND THE MAX shortage and track its position.
most_urgent_index = 0
largest_shortage = short_amounts[0]
for i in range(len(short_amounts)):
if short_amounts[i] > largest_shortage:
largest_shortage = short_amounts[i]
most_urgent_index = i
print(f"max step {i}: largest_shortage={largest_shortage}, most_urgent={short_items[most_urgent_index]}")
# Step 4: COMBINE the earlier answers into one report.
print("\nFinal 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[most_urgent_index]} (short by {largest_shortage})")
HOMEWORK HACK
Code Runner Challenge
HOMEWORK HACK
View IPYNB Source
# CODE_RUNNER: HOMEWORK HACK
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.
total_needed = 0
for amount in short_amounts:
total_needed = total_needed + amount
# Step 3: FIND THE MAX shortage and track its position.
if len(short_amounts) > 0:
most_urgent_index = 0
largest_shortage = short_amounts[0]
for i in range(len(short_amounts)):
if short_amounts[i] > largest_shortage:
largest_shortage = short_amounts[i]
most_urgent_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[most_urgent_index]} (short by {largest_shortage})")
Lines: 1
Characters: 0
Output
Click "Run" in code control panel to see output ...