Lab

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.

Hide code cell source

### Solution
students = [
    ("Alice", 85, "junior"), ("Bob", 72, "senior"),
    ("Charlie", 91, "sophomore"), ("Diana", 65, "junior"), ("Eve", 88, "senior")
]

ranked = sorted(students, key=lambda s: (s[2], -s[1]))
labels = list(map(lambda s: f"{s[0]} ({s[2]}, {s[1]})", ranked))

for label in labels:
    print(label)
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.

Hide code cell source

### Solution
students = [
    ("Alice", 85, "junior"), ("Bob", 72, "senior"),
    ("Charlie", 91, "sophomore"), ("Diana", 65, "junior"), ("Eve", 88, "senior")
]

from functools import reduce

total_grade = reduce(lambda acc, s: acc + s[1], students, 0)
average_grade = total_grade / len(students)

print("Total grade points:", total_grade)
print(f"Average grade: {average_grade:.1f}")
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.

Hide code cell source

### Solution
students = [
    ("Alice", 85, "junior"), ("Bob", 72, "senior"),
    ("Charlie", 91, "sophomore"), ("Diana", 65, "junior"), ("Eve", 88, "senior")
]

def count_seniors(records):
    if not records:
        return 0
    is_senior = 1 if records[0][2] == "senior" else 0
    return is_senior + count_seniors(records[1:])


print("Senior count:", count_seniors(students))
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.

Hide code cell source

### Solution
students = [
    ("Alice", 85, "junior"), ("Bob", 72, "senior"),
    ("Charlie", 91, "sophomore"), ("Diana", 65, "junior"), ("Eve", 88, "senior")
]

juniors = {name: grade for name, grade, year in students if year == "junior"}

print("Juniors:", juniors)
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.

Hide code cell source

### Solution
students = [
    ("Alice", 85, "junior"), ("Bob", 72, "senior"),
    ("Charlie", 91, "sophomore"), ("Diana", 65, "junior"), ("Eve", 88, "senior")
]

def make_threshold_filter(min_grade):
    return lambda s: s[1] >= min_grade


is_honor = make_threshold_filter(90)
honor_roll = list(filter(is_honor, students))
names = list(map(lambda s: s[0], honor_roll))

print("Honor roll:", names)
Honor roll: ['Charlie']