Popcorn Hack 1

Code Runner Challenge

Lists Popcorn 1 - predict, run, fix the crash

View IPYNB Source
# CODE_RUNNER: Lists Popcorn 1 - predict, run, fix the crash
# Convert the CollegeBoard Pseudocode into Python code and run it.
# Predict the output of each print statement before running the code.

items_needed = ["blankets", "socks", "water", "meals"]

print(items_needed[0])  # Prediction: "blankets" (first element)
print(
    items_needed[len(items_needed) - 1]
)  # Prediction: "meals" (last element)
print(len(items_needed))  # Prediction: 4 (length of the list)

# Fixed: Originally items_needed[5] raised an IndexError because Python is 0-indexed (valid indexes 0-3).
print(items_needed[3])  # Prediction: "meals" (4th element at index 3)
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Popcorn Hack 2

Code Runner Challenge

Lists Popcorn 2

View IPYNB Source
# CODE_RUNNER: Lists Popcorn 2
# Write a for loop that calculates the sum of all volunteer hours and prints it out.
# Calculate the average volunteer hours using the total sum and length of the list.

volunteer_hours = [7, 6, 8, 5, 7, 9]

total = 0
for hours in volunteer_hours:
  total += hours

print(f"Total volunteer hours: {total}")

# Guard against division by zero if list is empty
if len(volunteer_hours) == 0:
  print("No data yet")
else:
  average = total / len(volunteer_hours)
  print(f"Average volunteer hours: {round(average, 2)}")
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Popcorn Hack 3

Code Runner Challenge

Lists Popcorn 3 - count then filter

View IPYNB Source
# CODE_RUNNER: Lists Popcorn 3 - count then filter
items_requested = [
    "blankets",
    "socks",
    "meals",
    "socks",
    "water",
    "socks",
    "meals",
]  # use real class data

frequency = {}
for item in items_requested:
  if item in frequency:
    frequency[item] += 1
  else:
    frequency[item] = 1

# 1. Build a list of items with 2 or more votes
popular = []
for item in frequency:
  if frequency[item] >= 2:
    popular.append(item)

# 2. Print that list
print(popular)
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

4. Lists Homework

Code Runner Challenge

Lists Homework - indexing, accumulator, count and filter on one dataset

View IPYNB Source
# CODE_RUNNER: Lists Homework - indexing, accumulator, count and filter on one dataset
donations_by_item = [
    "blankets",
    "socks",
    "blankets",
    "water",
    "socks",
    "blankets",
    "meals",
    "socks",
    "blankets",
    "water",
    "blankets",
    "socks",
]

costs = [2, 3, 2, 4, 3, 2, 1, 3, 2, 4, 2, 3]

# LESSON 1 - indexing
# 1. Print how many donations were made
print(len(donations_by_item))

# 2. Print the last donation using indexing
print(donations_by_item[-1])

# LESSON 2 - sum and average
# 3. Use a loop to total costs (do NOT use sum())
total = 0
for cost in costs:
  total += cost

# 4. Print "Total: <total>" and "Average: <average>" as integers using the int() function
average = total / len(costs)
print(f"Total: {int(total)}")
print(f"Average: {int(average)}")

# LESSON 3 - count and filter
# 5. Build a frequency dictionary of donations_by_item and print it
frequency = {}
for item in donations_by_item:
  if item in frequency:
    frequency[item] += 1
  else:
    frequency[item] = 1

print(frequency)

# 6. Build a list of items with 3 or more donations and print it
popular_items = []
for item in frequency:
  if frequency[item] >= 3:
    popular_items.append(item)

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