Back to teaching portfolio

Chapter 7 · Lists & Tuples

CSBP119 Practical Questions

Use Python collections to manage rosters and matrices. Emphasis is on slicing, accumulation, and understanding when tuples provide better guarantees than lists.

Problem 1 · Waitlist Manager

Seat Assignments & Waitlist

Given a list of seats and a list of student names, pair them up and report who is on the waitlist once seats run out.

  1. Iterate over indices to pair seats and students.
  2. Print each assignment.
  3. List remaining students as the waitlist.
seats = ["A1", "A2", "B1", "B2"]
students = ["Sara", "Hamad", "Lina", "Ali", "Mariam"]

index = 0
while index < len(seats) and index < len(students):
    print(f"Seat {seats[index]} -> {students[index]}")
    index += 1

while index < len(students):
    print("Waitlist:", students[index])
    index += 1
Problem 2 · Matrix Ops

Lab Utilization Grid

Store weekly utilization percentages in a nested list. Calculate daily totals and the highest utilization slot.

  1. Represent the grid as a list of tuples.
  2. Loop through rows and build a totals list.
  3. Track the max value plus coordinates.
usage = [
    (70, 55, 60),
    (80, 65, 75),
    (60, 50, 90)
]

daily_totals = []
for day in usage:
    total = 0
    slot = 0
    while slot < len(day):
        total += day[slot]
        slot += 1
    daily_totals.append(total)
print("Daily totals:", daily_totals)

max_value = 0
max_day = 0
max_slot = 0

day_index = 0
while day_index < len(usage):
    slot_index = 0
    while slot_index < len(usage[day_index]):
        value = usage[day_index][slot_index]
        if value > max_value:
            max_value = value
            max_day = day_index
            max_slot = slot_index
        slot_index += 1
    day_index += 1

print(f"Peak usage {max_value}% on day {max_day} slot {max_slot}")
Problem 3 · Tuple Leaderboard

Scoreboard Tracker

Maintain a list of tuples in the form (team, points). Update the score for a specific team and display the standings.

  1. Create the initial list of tuples.
  2. Loop through the list to find the target team.
  3. Print each tuple showing the updated standings.
standings = [("Aquila", 12), ("Lynx", 9), ("Orion", 15)]
team_name = input("Team to update: ").strip()
points_to_add = int(input("Points to add: "))

index = 0
while index < len(standings):
    team, score = standings[index]
    if team.lower() == team_name.lower():
        standings[index] = (team, score + points_to_add)
    index += 1

for entry in standings:
    print(entry[0], "->", entry[1], "points")