Week 6 Tutorial: Advanced Lists, Tuples & Linear Search
Problem 1: Sensor Data Aggregation
Problem Statement:
You are working with a log file from a set of climate sensors. The log is represented as a list of tuples, where each tuple contains a (sensor_id, temperature). Each sensor may report multiple readings.
Your task is to write a function summarize_sensor_data that processes this list and returns a summary. The summary should be a list of tuples, where each tuple contains a unique sensor_id and its highest recorded temperature.
Requirements:
- The function must find the maximum temperature for each unique sensor ID.
- The final output list must be sorted alphabetically by sensor ID.
- If the input list of readings is empty, the function should return an empty list.
Test Cases: Case 1:
readings = [
('SensorB', 25.4),
('SensorA', 22.1),
('SensorB', 26.1), # New max for SensorB
('SensorC', 30.5),
('SensorA', 21.9), # Lower than previous SensorA reading
('SensorB', 25.9)
]
# Expected Output: [('SensorA', 22.1), ('SensorB', 26.1), ('SensorC', 30.5)]
Case 2:
readings_empty = []
# Expected Output: []
Problem 2: Top N Frequent Elements
Problem Statement:
Write a function get_top_n_frequent(items, n) that takes a list of items and an integer n, and returns a list of the n most frequent items.
Requirements:
- The function should determine the frequency of each unique item in the
itemslist. - It should return a list containing the
nitems that appear most frequently. - Tie-Breaking Rule: If two items have the same frequency, the one that comes first alphabetically or numerically should be ranked higher.
- The final returned list should be ordered from most frequent to least frequent.
- If the number of unique items is less than
n, return all unique items, ordered by frequency.
Test Cases: Case 1:
votes = ['apple', 'orange', 'banana', 'apple', 'orange', 'apple', 'grape']
n = 2
# Frequencies: apple=3, orange=2, banana=1, grape=1
# Top 2 are 'apple' and 'orange'.
# Expected Output: ['apple', 'orange']
Case 2 (with tie-breaking):
words = ['code', 'sleep', 'eat', 'code', 'repeat', 'eat', 'code', 'sleep']
n = 3
# Frequencies: code=3, sleep=2, eat=2, repeat=1
# 'code' is #1. For #2, 'eat' and 'sleep' are tied. 'eat' comes before 'sleep' alphabetically.
# So the top 3 are 'code', 'eat', 'sleep'.
# Expected Output: ['code', 'eat', 'sleep']
Problem 3: Matrix Diagonal Sums
Problem Statement:
Write a function sum_diagonals(matrix) that calculates the sum of the elements on the two main diagonals of a square matrix (an n x n grid).
Definitions:
- The primary diagonal consists of elements from the top-left to the bottom-right. (i.e., where
row_index == col_index). - The anti-diagonal (or secondary diagonal) consists of elements from the top-right to the bottom-left.
Requirement:
- If the matrix has an odd dimension (e.g., 3x3, 5x5), the center element is part of both diagonals. Be careful not to count it twice in your total sum.
Test Cases:
Case 1: 4x4 Matrix (even dimension)
matrix_4x4 = [
[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12],
[13, 14, 15, 16]
]
# Primary diagonal: 1 + 6 + 11 + 16 = 34
# Anti-diagonal: 4 + 7 + 10 + 13 = 34
# Total sum = 34 + 34 = 68
# Expected Output: 68
Case 2: 3x3 Matrix (odd dimension)
matrix_3x3 = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
# Primary diagonal: 1 + 5 + 9 = 15
# Anti-diagonal: 3 + 5 + 7 = 15
# The center element '5' is in both.
# Total sum = (1 + 5 + 9) + (3 + 7) = 15 + 10 = 25
# Expected Output: 25
Problem 4: Matrix Transposition
Problem Statement: In linear algebra, the transpose of a matrix is an operator which flips a matrix over its diagonal; that is, it switches the row and column indices of the matrix.
Write a function transpose_matrix(matrix) that takes a rectangular matrix (a non-empty list of lists where all inner lists have the same length) and returns its transpose.
For example, if the input matrix has dimensions M rows by N columns, the output matrix will have dimensions N rows by M columns. The element that was at matrix[row][col] will be at transposed_matrix[col][row].
Test Cases: Case 1: Rectangular Matrix (3x2)
matrix_3x2 = [
[1, 2],
[3, 4],
[5, 6]
]
# Expected Output (a 2x3 matrix):
# [
# [1, 3, 5],
# [2, 4, 6]
# ]
Case 2: Square Matrix (2x2)
matrix_2x2 = [
['a', 'b'],
['c', 'd']
]
# Expected Output:
# [
# ['a', 'c'],
# ['b', 'd']
# ]
Problem 5: Leaderboard Sorting
Problem Statement:
You are given a leaderboard for a game, represented as a list of tuples ('player_name', score). Your task is to write a function sort_leaderboard(leaderboard) that sorts this data and returns the sorted list.
The sorting must follow two rules in order:
- Primary Sort Key: Players should be sorted by their
scorein descending order (highest score first). - Secondary Sort Key: If two or more players have the same score (a tie), they should be sorted by their
player_namein ascending (alphabetical) order.
Constraint: You have not yet learned how to provide a custom key to the .sort() method. You must solve this problem by transforming the data into a temporary structure that can be sorted correctly using the default behavior of .sort().
Hint: Think about how Python sorts a list of tuples by default. It sorts by the first element, then the second for ties, and so on. How can you manipulate the score so that a standard ascending sort will achieve a descending result?
Test Cases: Case 1:
leaderboard = [
('Alice', 88),
('Bob', 92),
('Charlie', 92),
('David', 85)
]
# Sorting order: Bob (92), then Charlie (92, tie-broken by name), then Alice (88), then David (85)
# Expected Output: [('Bob', 92), ('Charlie', 92), ('Alice', 88), ('David', 85)]
Case 2:
scores = [('Zelda', 100), ('Mario', 120), ('Link', 100), ('Sonic', 100)]
# Expected Output: [('Mario', 120), ('Link', 100), ('Sonic', 100), ('Zelda', 100)]
This content will be available starting November 06, 2025.