Popcorn Hack 1: Predict, Run, and Fix the Crash

Code Runner Challenge

Popcorn Hack 1

View IPYNB Source
%%python

# CODE_RUNNER: Popcorn Hack 1 

#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"]

print(items_needed[0])                        # "blankets" - Python is 0-indexed, so index 0 is the first element
print(items_needed[len(items_needed) - 1])    # "meals" - last valid index is length-1, not length
print(len(items_needed))                      # 4 - there are 4 items in the list
print(items_needed[3])                        # CRASH - valid indices are 0,1,2,3; index 4 doesn't exist so changed to 3
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Popcorn Hack 2: Sum and Average

Code Runner Challenge

Popcorn Hack 2

View IPYNB Source
%%python

# CODE_RUNNER: Popcorn Hack 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
for volunteer_hour in volunteer_hours:
    total += volunteer_hour

average = total / len(volunteer_hours)
print(f"Total Hours: {total}")
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: Count Then Filter

Code Runner Challenge

Popcorn Hack 3

View IPYNB Source
%%python

# CODE_RUNNER: Popcorn Hack 3

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_requested in frequency:
    if frequency[item_requested] >= 2:
        popular.append(item_requested)

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

Homework Hack: Indexing, Accumulator, Count, and Filter

Code Runner Challenge

Homework Hack 3

View IPYNB Source
%%python

# CODE_RUNNER: Homework Hack 3

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
# 2. Print the last donation using indexing
print(f"Donations Total: {len(donations_by_item)}")
print(f"Latest Donation: {donations_by_item[-1]}")

# LESSON 2 - sum and average
# 3. Use a loop to total costs (do NOT use sum())
# 4. Print "Total: <total>" and "Average: <average>" as integers using the int() function
total = 0
for cost in costs:
    total += cost

avg = total / len(costs)
print(f"Total Costs: {int(total)}")
print(f"Average Cost Per Item: {int(avg)}")

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

print(frequency)

popular = []
for item in frequency:
    if frequency[item] >= 3:
        popular.append(item)

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