10.3.2. Lab#
Use this assignment to practice the chapter’s concepts and coding skills in runnable Python code.
Sign in from the account menu before you start working on the assignment.
Write and run your code directly in the coding cells.
Multiple submissions are allowed. The highest score is kept.
### Question 1: Rank students by year, then grade
# `students` holds `(name, grade, year)` tuples.
# Define `ranked_labels(records)`. Inside it, use `sorted()` with a `lambda` key
# of `(s[2], -s[1])` to order the records alphabetically by year, and within
# the same year by grade from highest to lowest. Then use `map()` with a
# `lambda` to return a list of strings formatted as `"name (year, grade)"`.
# Call `ranked_labels(students)` and print each label on its own line.
# Print exactly these five lines:
# Alice (junior, 85)
# Diana (junior, 65)
# Eve (senior, 88)
# Bob (senior, 72)
# Charlie (sophomore, 91)
students = [
("Alice", 85, "junior"), ("Bob", 72, "senior"),
("Charlie", 91, "sophomore"), ("Diana", 65, "junior"), ("Eve", 88, "senior")
]
### Your code starts here.
### Your code ends here.
### Question 2: Aggregate grades with reduce
# `students` holds `(name, grade, year)` tuples.
# Import `reduce` from `functools`. Define `total_grade(records)` that uses
# `reduce()` with a `lambda`, starting from `0`, to return the sum of every
# record's grade. Compute `average_grade` as `total_grade(students) / len(students)`.
# Print exactly these two lines:
# Total grade points: 401
# Average grade: 80.2
students = [
("Alice", 85, "junior"), ("Bob", 72, "senior"),
("Charlie", 91, "sophomore"), ("Diana", 65, "junior"), ("Eve", 88, "senior")
]
### Your code starts here.
### Your code ends here.
### Question 3: Count seniors recursively
# `students` holds `(name, grade, year)` tuples.
# Write a recursive function `count_seniors(records)` that returns `0`
# for an empty list, and otherwise returns `1` (if the first record's
# year is `"senior"`, else `0`) plus `count_seniors` of the rest of the
# list. Do not use a loop.
# Print exactly this one line:
# Senior count: 2
students = [
("Alice", 85, "junior"), ("Bob", 72, "senior"),
("Charlie", 91, "sophomore"), ("Diana", 65, "junior"), ("Eve", 88, "senior")
]
### Your code starts here.
### Your code ends here.
### Question 4: Guard a grade converter with a decorator
# `students` holds `(name, grade, year)` tuples.
# Import `wraps` from `functools`. Write a decorator `require_valid_grade(func)`.
# Its wrapper uses `@wraps(func)` and `*args`, raises `ValueError("grade out of range")`
# when the first argument is below `0` or above `100`, and otherwise returns
# `func(*args)`.
# Apply it with `@` to `letter(grade)`, which returns `"A"` for 90 and above,
# `"B"` for 80 and above, `"C"` for 70 and above, and `"F"` otherwise.
# Print `f"{name}: {letter(grade)}"` for each student in order. Then, inside
# `try`/`except ValueError`, call `letter(105)` and print `f"Caught: {error}"`.
# Print exactly these six lines:
# Alice: B
# Bob: C
# Charlie: A
# Diana: F
# Eve: B
# Caught: grade out of range
students = [
("Alice", 85, "junior"), ("Bob", 72, "senior"),
("Charlie", 91, "sophomore"), ("Diana", 65, "junior"), ("Eve", 88, "senior")
]
### Your code starts here.
### Your code ends here.
### Question 5: Cache repeated grade lookups
# `students` holds `(name, grade, year)` tuples. `grades` lists the grades of
# a longer roster, so some values repeat. `calls` is an empty list.
# Import `lru_cache` from `functools`. Write `band(grade)`, decorated with
# `@lru_cache(maxsize=None)`, that appends `grade` to `calls` and returns the
# grade band `grade // 10 * 10`.
# Use `map()` with `band` to build `bands` from `grades`.
# Print exactly these three lines (`band.cache_info().hits` is the hit count):
# Bands: [80, 70, 90, 60, 80, 80, 70]
# Computed: 5
# Cache hits: 2
students = [
("Alice", 85, "junior"), ("Bob", 72, "senior"),
("Charlie", 91, "sophomore"), ("Diana", 65, "junior"), ("Eve", 88, "senior")
]
grades = [85, 72, 91, 65, 88, 85, 72]
calls = []
### Your code starts here.
### Your code ends here.
Question 1
Question 2
Question 3
Question 4
Question 5