10.3.2. Lab#
Use this lab to practice the chapter’s coding skills in runnable Python code.
Sign in from the account menu before you start working on the lab.
Launch Live Code (the rocket) to write and run code 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.
# Use sorted() with a lambda key of (s[2], -s[1]) to build ranked:
# students ordered alphabetically by year, and within the same year
# by grade from highest to lowest.
# Use map() with a lambda to build labels, a list of strings
# formatted as "name (year, grade)".
# Print each label on its own line, in ranked order. 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.
Alice (junior, 85)
Diana (junior, 65)
Eve (senior, 88)
Bob (senior, 72)
Charlie (sophomore, 91)
### Question 2: Aggregate grades with reduce
# students holds (name, grade, year) tuples.
# Import reduce from functools. Use reduce() with a lambda to build
# total_grade, the sum of every student's grade, starting from 0.
# Compute average_grade as total_grade / 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.
Total grade points: 401
Average grade: 80.2
### 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.
Senior count: 2
### Question 4: Build a juniors-only grade book
# students holds (name, grade, year) tuples.
# Use a dict comprehension to build juniors, mapping each junior's
# name to their grade (only include students whose year is
# "junior").
# Print exactly this one line:
# Juniors: {'Alice': 85, 'Diana': 65}
students = [
("Alice", 85, "junior"), ("Bob", 72, "senior"),
("Charlie", 91, "sophomore"), ("Diana", 65, "junior"), ("Eve", 88, "senior")
]
### Your code starts here.
### Your code ends here.
Juniors: {'Alice': 85, 'Diana': 65}
### Question 5: Build a threshold filter
# students holds (name, grade, year) tuples.
# Write make_threshold_filter(min_grade), a function that returns a
# lambda taking one student tuple s and returning True when
# s[1] >= min_grade.
# Call make_threshold_filter(90) and store the result in is_honor.
# Use filter() with is_honor to build honor_roll, then map() with a
# lambda to build names, the list of honor roll student names.
# Print exactly this one line:
# Honor roll: ['Charlie']
students = [
("Alice", 85, "junior"), ("Bob", 72, "senior"),
("Charlie", 91, "sophomore"), ("Diana", 65, "junior"), ("Eve", 88, "senior")
]
### Your code starts here.
### Your code ends here.
Honor roll: ['Charlie']
Question 1
Question 2
Question 3
Question 4
Question 5