Problem Set #5 Due date: August 1, 2026, 11:59 PM
REMINDER: As you work on your coding assignments, it is important to remember that passing the examples provided does
not guarantee full credit. While these examples can serve as a helpful starting point, it is ultimately your responsibility to
create additional tests, ensure that it is functioning correctly and meets all the requirements and specifications outlined in
the assignment instructions. Your grade for this assignment is divided in two parts:
50% > Passing all post-due date tests 50% > Clarity and design of your code
(no partial credit for failed cases) (Class Coding Standards, requirements/specifications)
Before you start…
• Your code must be written using Python language.
• You are not allowed to use material that has not been covered in class.
o The only string methods you are allowed to use are: split, strip and join
o The only list methods you are allowed to use are: append, pop and del
o The only dictionary methods you are allowed to use are: keys, values, items, get, pop and del
o You can use the len function and slicing
• Function names must be defined as stated in the function description. Failure to comply with this requirement
will result in a 0 score for the visible and post-due date testing.
• No code should be written outside functions, including function calls. All testing code must be done inside
the main function
• Your functions should NOT have any input() calls in its body.
• Your code should be concise and efficient without useless code or redundant code.
• Failure to follow the assignment’s requirements will result in a zero score for the assignment even if the
autograder gives you full score.
• Code submitted with syntax errors does not receive ANY credit.
• Revise the Grading Notes provided in the Canvas Assignment that includes the evaluation criteria for Clarity
and Design.
• You will be writing several functions, but they will all be saved in one file: [Link]. Please save all the
functions in this one file or you will lose points.
• Ask questions using our Problem Set Questions channel in Microsoft Teams.
To receive any credit for any of these problems, you are not allowed to use material we have not covered in
class
• The only string methods you are allowed to use are: split, strip and join
• The only list methods you are allowed to use are: append, pop and del
• The only dictionary methods you are allowed to use are: keys, values, items, get, pop and del
• No list or dictionary comprehension syntax allowed
• You can use the len function and slicing syntax only in the form covered in lectures
• Think about how you can organize your solution into logical steps by breaking it down into smaller,
focused tasks, it is part of your grade! Helper functions are expected in almost every problem
For all the problems in this assignment, you are only allowed to use the following data types we have covered in
class: int, float, bool, str, list, tuple and dict. You are not allowed to use any built-in functions or libraries that are
not listed in the reminder above.
Part 1: Warm up!
Problem 1.1 (10 pts) HR compliance checks often require a minimum salary floor for every position across teams.
You are given a possibly ragged 2D list where each row is a team and each value is an employee’s current hourly
wage. To enforce policy, any wage below the floor must be raised in place to the floor value. Write the function
ensure_min_salary that takes such 2D list and a minimum salary value and mutates the 2D list so that any
entry less than the minimum salary becomes exactly that minimum.
Example:
>>> wages = [[15.0, 22.5], [10.0, 30.0, 18.0]]
>>> ensure_min_salary(wages, 20.0)
>>> wages
[[20.0, 22.5], [20.0, 30.0, 20.0]]
Problem 1.2 (10 pts) Supermarkets receive multiple shipments of each product every week. A logistics manager
wants a quick total for each product across all its deliveries. You’re given a .txt/.csv file where each line is one
product, and the comma-separated values on that line are the delivery amounts (some products may have more
deliveries than others). Write the function inventory_totals that takes one argument, a string for the file
name, creates a 2D list with its contents, and returns a list of floats containing the sum of each row, rounded to two
decimals using Python’s built-in round function.
Example:
# [Link]
3.25,1.50,2.10
10
5.0,2.0
0.99,1.01,1.00,0.50
>>> inventory_totals("[Link]")
[6.85, 10.0, 7.0, 3.5]
Problem 1.3 (10 pts) A city runs a small network of air-quality sensors. Data is collected to a comma-separated
central file where each column is a sensor and each row is a time slot. If a sensor misses a reading, that spot may
be blank and the row will be shorter than others. Write the function sensor_sums that takes a file name, creates
a 2D list with its contents, and returns a list of floats with the sum of readings per sensor (column), rounded to two
decimals using Python’s built-in round function.
Example:
# [Link]
5.0,1.0,2.0
1.0,2.0
0.5,0.5,0.5
>>> sensor_sums("[Link]")
[6.5, 3.5, 2.5]
Part 2: Read it and work it!
Problem 2.1 (10 pts) A neighborhood café logs each sale in a .csv file as item, price. Baristas sometimes forget
to type the price or leave blank lines (item and price are not provided). The owner needs a daily summary that
ignores malformed lines. Write the function daily_sales_summary that takes a string representing the name
of a csv file and returns a tuple of size two, the first element is the count of valid sales and the second element is
the total revenue of all valid sales rounded to two decimal places using Python’s built-in round function. A “valid
sale” has a non-empty item name and a numeric price.
Example:
# [Link]
latte,4.50
cappuccino,5.00
,
espresso,2.50
tea,
>>> daily_sales_summary("[Link]")
(3, 12.0)
Problem 2.2 (15 pts) A magic square of order n (number of integers along one side) is an arrangement
of n x n numbers, usually distinct positive numbers, such that the sums of the numbers in each row, each column,
and both main diagonals are the same, this constant sum is called the 'magic constant':
Write the function is_magic_square. The function takes one argument, a string, that represents the name of
a .txt or .csv file. Your function must read the contents of the file, create a 2D list with the contents of the file, and
return True if the 2D list is a magic square, False otherwise. The file contains comma-separated numerical values
representing the elements of the 2D list that might be ragged.
Tip: Structure your algorithm by splitting into coherent tasks. You do not write the entire code into
this function. You need to compute the magic constant in multiple directions, you might want to define
other functions that return the sum for each part of the matrix! Proper procedural design and reuse
of code are part of your design grade.
Example:
>>> is_magic_square('num_2.txt')
True
Problem 2.3 (15 pts) You are to design a simple ticket booking system. Write the Python code to implement the
function buy_ticket that takes in two strings, the name of a .txt file and the proposed seat selection. The
function returns True, if the booking is successful, False otherwise. When the booking is successful, your function
should modify the contents of the file to update the seat arrangement.
The .txt file will consist of a series of letters (X or O) separated by one space. Take a look at the [Link] example:
OOOOX
OOOOOXXXXXX
XXXXXXX
XXXXXXX
OOXXOX
The O means that the seat is available for purchase. X means the seat is already booked and unavailable.
Your function must create a 2D list that represents the file’s seating arrangement, then buy the ticket if available
(change the O to X), and write the updated seat arrangement back to the same file.
Rows are labeled alphabetically (A-Z), and columns are labeled numerically (1, 2, 3, ...), so if you invoke
buy_ticket("[Link] ", "B1"), it will change the file to:
OOOOX
XOOOOXXXXXX
XXXXXXX
XXXXXXX
OOXXOX
The seat input could be "B1" or "b1" and both are valid. You can assume that the rows are only one letter
Example:
>>> buy_ticket("[Link]", "a1")
False
>>> buy_ticket("[Link]", "B3")
True
>>> buy_ticket("[Link]", "A10")
False
The content of [Link] after the three calls is:
When you add a new line in the file, you will be adding the new line character (\n). Depending on your
implementation, when you re-write the file you might have a new line (\n) at the end of your file. If
that is the case, you have two options:
• Ensure your logic does not include the new line character the end of the last row or
• In your implementation, make sure the result of splitting the row is not an empty list, as
"\n".split()evaluates to []. If that is the case, then you need a conditional to process the
row only when it is not an empty list to avoid any potential IndexErrors.