7.5.3. Homework#
Use these questions after class to check your understanding of the chapter concepts and coding skills.
Sign in from the account menu before you start working on the homework.
Complete all true/false and coding questions before you submit the homework.
Launch Live Code (the rocket) to write and run code in the coding cells.
Multiple submissions are allowed. The highest score is kept.
1. Key Lookup
A support dashboard can use a dictionary to look up a customer plan quickly from a username key.
2. Missing Keys
Using scores["missing"] is always safer than .get() when a key may not exist.
3. Nested Dictionaries
A roster keyed by course code can store another dictionary for section metadata such as instructor and seat count.
4. Sets
A set is the right collection when a report must preserve duplicate names exactly as they appeared.
5. Hashability
A list can always be placed inside a set because all Python objects are hashable.
### 6. Look up with a fallback
# Use `.get()` to print the requested plan. Print exactly this line:
# # Plan for chen: trial
plans = {"ana": "pro", "bob": "basic"}
user = "chen"
### Your code starts here.
### Your code ends here.
Plan for chen: trial
### 7. Build a frequency dictionary
# Count departments and print exactly this line:
# # Counts: {'sales': 2, 'ops': 3, 'data': 1}
departments = ["sales", "ops", "sales", "data", "ops", "ops"]
### Your code starts here.
### Your code ends here.
Counts: {'sales': 2, 'ops': 3, 'data': 1}
### 8. Group values by key
# Group names by role and print exactly this line:
# # Groups: {'analyst': ['Ana', 'Chen'], 'manager': ['Bob']}
people = [("Ana", "analyst"), ("Bob", "manager"), ("Chen", "analyst")]
### Your code starts here.
### Your code ends here.
Groups: {'analyst': ['Ana', 'Chen'], 'manager': ['Bob']}
### 9. Find unique categories
# Use a set and sorted list. Print exactly this line:
# # Categories: ['api', 'data', 'web']
categories = ["data", "web", "data", "api", "web"]
### Your code starts here.
### Your code ends here.
Categories: ['api', 'data', 'web']
### 10. Use set algebra
# Create active paid users with set intersection. Print exactly this line:
# # Active paid users: ['ana', 'chen']
active = {"ana", "bob", "chen"}
paid = {"ana", "chen", "dee"}
### Your code starts here.
### Your code ends here.
Active paid users: ['ana', 'chen']