3.10 Lists HW
- Lists
- POPCORN HACK 1
- Code Runner Challenge
- POPCORN HACK 2
- Code Runner Challenge
- POPCORN HACK 3
- Code Runner Challenge
- HOMEWORK HACK
- Code Runner Challenge
Lists
POPCORN HACK 1
Code Runner Challenge
POPCORN HACK 1
View IPYNB Source
# CODE_RUNNER: POPCORN HACK 1
items_needed = ["blankets", "socks", "water", "meals"]
print(items_needed[0]) # prediction: blankets
print(items_needed[3]) # prediction: meals, the last item
print(len(items_needed)) # prediction: 4
print(items_needed[3]) # fixed from items_needed[5], which is out of range
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
volunteer_hours = [7, 6, 8, 5, 7, 9]
total_hours = 0
for hours in volunteer_hours:
total_hours = total_hours + hours
if len(volunteer_hours) == 0:
print("No data yet")
else:
average_hours = total_hours / len(volunteer_hours)
print(total_hours)
print(average_hours)
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
items_requested = ["blankets", "socks", "meals", "socks", "water", "socks", "meals"]
frequency = {}
for item in items_requested:
if item in frequency:
frequency[item] += 1
else:
frequency[item] = 1
popular = []
for item in frequency:
if frequency[item] >= 2:
popular.append(item)
print(frequency)
print(popular)
Lines: 1
Characters: 0
Output
Click "Run" in code control panel to see output ...
HOMEWORK HACK
Code Runner Challenge
HOMEWORK HACK
View IPYNB Source
# CODE_RUNNER: HOMEWORK HACK
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
print(len(donations_by_item))
print(donations_by_item[-1])
# LESSON 2 - sum and average
total = 0
for cost in costs:
total = total + cost
if len(costs) == 0:
print("No cost data")
else:
average = total / len(costs)
print("Total:", int(total))
print("Average:", int(average))
# LESSON 3 - count and filter
frequency = {}
for item in donations_by_item:
if item in frequency:
frequency[item] += 1
else:
frequency[item] = 1
popular = []
for item in frequency:
if frequency[item] >= 3:
popular.append(item)
print(frequency)
print(popular)
Lines: 1
Characters: 0
Output
Click "Run" in code control panel to see output ...