Week 7 Tutorial: Revision Week
Tutorial Problem 1: Analyze Flight Data
You are working with flight data for an airline. The data is provided as a list of tuples, where each tuple contains (flight_number, origin_city, destination_city, passenger_count).
Your task is to write a function called find_flights_from_city. This function should take three arguments:
flight_data: The list of flight tuples.origin: A string representing the origin city to search for.min_passengers: An integer representing the minimum number of passengers a flight must have to be included in the results.
The function should iterate through the flight data and find all flights that depart from the specified origin city AND have a passenger_count greater than or equal to min_passengers.
The function must return a new list containing only the flight_number strings of the matching flights. If no flights match the criteria, it should return an empty list.
flights = [
('AA101', 'New York', 'Los Angeles', 250),
('UA202', 'Chicago', 'New York', 180),
('DL303', 'New York', 'Miami', 160),
('AA404', 'Dallas', 'Los Angeles', 220),
('UA505', 'New York', 'Chicago', 175),
('DL606', 'Miami', 'New York', 150)
]
# Case 1: Find all flights from 'New York' with at least 200 passengers.
# Matches: ('AA101', 'New York', 'Los Angeles', 250)
# Expected Output: ['AA101']
# Case 2: Find all flights from 'New York' with at least 150 passengers.
# Matches: ('AA101', ..., 250), ('DL303', ..., 160), ('UA505', ..., 175)
# Expected Output: ['AA101', 'DL303', 'UA505']
# Case 3: Find all flights from 'Chicago' with at least 200 passengers.
# Matches: None
# Expected Output: []
Tutorial Problem 2: Find Saddle Point
In a 2D grid of numbers, a “saddle point” is an element that is the smallest in its row and the largest in its column.
Write a function find_saddle_point_coordinates(grid) that takes a non-empty 2D list (matrix) of unique integers. The function should search for a saddle point. If a saddle point is found, the function must return its coordinates as a (row, column) tuple. If no saddle point exists, the function should return None. You can assume the input grid will be rectangular (all rows have the same length).
Grid:
[[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
Expected: (2, 0)
Explanation: 7 is minimum in row [7, 8, 9] and maximum in column [1,4,7]
Grid:
[[-1, -2, 0],
[ 1, -5, 3],
[-4, -3, -6]]
Expected: (0, 1)
Explanation: -2 is minimum in row [-1, -2, 0] and maximum in column [-2,-5,-3]
Grid:
[[1, 2, 3],
[8, 5, 9],
[4, 6, 7]]
Expected: None
Grid:
[[5, 6, 7],
[4, 2, 1],
[3, 0,-1]]
Expected: (0, 0)
Explanation: 5 is minimum in row 5, 6, 7] and maximum in column [5,4,3]
Tutorial Problem 3: Longest Downward Trend
You are analyzing a list of daily stock prices. A “downward trend” is a sequence of one or more consecutive days where the price is strictly less than the price on the previous day.
Write a function longest_downward_trend(prices) that takes a list of numbers (integers or floats) representing daily prices. The function should calculate and return the length of the longest downward trend.
A single day is not a trend, so a sequence of one price has a trend length of 0. A sequence of two decreasing prices ([10, 8]) is a trend of length 2.
# Prices: [100, 98, 97, 99, 96, 95, 94, 100]
# Trends:
# 100 -> 98 -> 97 (length 3)
# 99 -> 96 -> 95 -> 94 (length 4)
# Longest is 4.
# Expected Output: 4
# Prices: [10, 20, 30, 40, 50]
# No downward trends.
# Expected Output: 0
# Prices: [50, 40, 30, 20, 10]
# Entire list is one trend.
# Expected Output: 5
# Prices: [10] or []
# Not enough data for a trend.
# Expected Output: 0
Tutorial Problem 4: Merge Two Sorted Lists
You are given two lists of integers, list1 and list2, that are already sorted in ascending order. Your task is to write a function merge_sorted_lists(list1, list2) that merges these two lists into a single, new list that is also sorted in ascending order.
Important Constraint: You must achieve this by iterating through both lists only once. Do not simply concatenate the two lists and then call a sorting function (like (list1 + list2).sort()). The goal is to build the final list by intelligently picking the next smallest element from the two input lists. This is a common and efficient algorithmic pattern.
# Case 1: Standard case
l1 = [1, 5, 9, 10]
l2 = [2, 3, 8]
# Expected Output: [1, 2, 3, 5, 8, 9, 10]
# Case 2: One list is empty
l3 = []
l4 = [4, 6, 7]
# Expected Output: [4, 6, 7]
# Case 3: Lists with different lengths
l5 = [10, 20, 30, 40, 50]
l6 = [15, 25]
# Expected Output: [10, 15, 20, 25, 30, 40, 50]
# Case 4: Both lists are empty
l7 = []
l8 = []
# Expected Output: []
Tutorial Problem 5: Tic-Tac-Toe Winner Check
You are building a Tic-Tac-Toe game. A critical piece of logic is the function that determines if a player has won.
Write a function check_winner(board) that takes a 3x3 nested list representing the game board. The board will contain the strings 'X', 'O', or ' ' (a space for an empty cell).
The function should check for a winning condition: three of the same symbols in a row, column, or on either of the two main diagonals.
- If
'X'has won, the function should return the string'X'. - If
'O'has won, the function should return the string'O'. - If there is no winner yet (the game is ongoing or is a tie), the function should return the string
'No Winner'.
You can assume there will not be a situation where both ‘X’ and ‘O’ have a winning line on the same board.
# Case 1: 'X' wins on the top row
board_x_wins_row = [
['X', 'X', 'X'],
['O', ' ', 'O'],
[' ', 'O', ' ']
]
# Expected Output: 'X'
# Case 2: 'O' wins on the middle column
board_o_wins_col = [
['X', 'O', 'X'],
['X', 'O', ' '],
[' ', 'O', ' ']
]
# Expected Output: 'O'
# Case 3: 'X' wins on a diagonal
board_x_wins_diag = [
['X', 'O', 'O'],
[' ', 'X', ' '],
['O', ' ', 'X']
]
# Expected Output: 'X'
# Case 4: No winner
board_no_winner = [
['X', 'O', 'X'],
['X', 'O', 'O'],
['O', 'X', 'X']
]
# Expected Output: 'No Winner'
This content will be available starting November 13, 2025.