Homework

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.

Hide code cell source

### Solution
# True
# A dictionary maps each key to a value and finds a key by hashing, so plans[username]
# retrieves a customer's plan quickly without searching through every record.

2. Missing Keys

Using scores["missing"] is always safer than .get() when a key may not exist.

Hide code cell source

### Solution
# False
# scores["missing"] raises a KeyError when the key does not exist. scores.get("missing")
# returns None, or a default you supply, so get() is the safer choice when a key may be
# absent.

3. Nested Dictionaries

A roster keyed by course code can store another dictionary for section metadata such as instructor and seat count.

Hide code cell source

### Solution
# True
# A dictionary value can be any object, including another dictionary. In Section 7.3,
# student["grades"]["math"] uses the outer key to reach the inner dictionary and the inner
# key to read one value; a roster keyed by course code works the same way.

4. Sets

A set is the right collection when a report must preserve duplicate names exactly as they appeared.

Hide code cell source

### Solution
# False
# A set stores each value only once and does not keep insertion order, so duplicate names
# would be lost. A list preserves duplicates in the order they appeared.

5. Hashability

A list can always be placed inside a set because all Python objects are hashable.

Hide code cell source

### Solution
# False
# Only hashable objects, such as numbers, strings, and tuples of hashable items, can go in
# a set. A list is mutable and therefore unhashable, so adding one to a set raises a
# TypeError.
### 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.

Hide code cell source

### Solution
plans = {"ana": "pro", "bob": "basic"}
user = "chen"
print(f"Plan for {user}:", plans.get(user, "trial"))
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.

Hide code cell source

### Solution
departments = ["sales", "ops", "sales", "data", "ops", "ops"]
counts = {}

for department in departments:
    counts[department] = counts.get(department, 0) + 1

print("Counts:", counts)
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.

Hide code cell source

### Solution
people = [("Ana", "analyst"), ("Bob", "manager"), ("Chen", "analyst")]
groups = {}

for name, role in people:
    groups.setdefault(role, []).append(name)

print("Groups:", groups)
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.

Hide code cell source

### Solution
categories = ["data", "web", "data", "api", "web"]
unique_categories = sorted(set(categories))
print("Categories:", unique_categories)
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.

Hide code cell source

### Solution
active = {"ana", "bob", "chen"}
paid = {"ana", "chen", "dee"}
print("Active paid users:", sorted(active & paid))
Active paid users: ['ana', 'chen']