3.10 Lists HW
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 above CollegeBoard Psuedocode into Python code and run it.
#Predict the output of each print statement before running the code. Add your prediction as a comment next to each print statement.
items_needed = ["blankets", "socks", "water", "meals"] # 4 items, so valid Python indexes are 0 through 3
print(items_needed[0]) # Prediction: blankets (first item, index 0 in Python vs 1 in pseudocode)
print(items_needed[len(items_needed) - 1]) # Prediction: meals (last item; Python's last index is len - 1, pseudocode's is LENGTH)
print(len(items_needed)) # Prediction: 4
print(items_needed[3]) # Prediction: IndexError with items_needed[5]. FIX: changed to [3], the last valid index -> meals
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
#Now try it yourself! Write a for loop that calculates the sum of all the volunteer hours in the list and prints it out.
#Calculate the average of the volunteer hours in the list using the value you calculated for the sum and the length of the list and print it out.
volunteer_hours = [7, 6, 8, 5, 7, 9]
total = 0 # Initialize the accumulator BEFORE the loop
for hours in volunteer_hours:
total += hours # Add each element inside the loop
print(f"Total: {total}") # 42
# FIX: guard against an empty list so we never divide by zero (ZeroDivisionError)
if len(volunteer_hours) == 0:
print("No data yet")
else:
average = total / len(volunteer_hours)
print(f"Average: {average}") # 7.0
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
print(frequency)
# 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 ...
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 ...