Intro to Programming with Python - Final exam Practice Fall 2024
Intro to Programming with Python - Final exam Practice Fall 2024
(Practice)
• Academic honesty is required in all work you submit to be graded. You should NOT copy
or share your code with other students to avoid plagiarism issues.
• You must stop working on the exam and submit your solutions on time. Otherwise, your
test will NOT be graded.
• This is a open-book exam, and NO Internet access is allowed.
• Drafting paper and pen/pencil are allowed. We will provide the drafting paper to you. DO
NOT USE any web-based drafting/drawing tools during the exam.
• Late submissions without an approved extension will not graded at all.
• During the exam, you are NOT allowed to leave the room or/and use the bathroom. In
case of emergency, you are allowed to leave the exam room maximum of 5 minutes with
the monitoring of an invigilator. Also, you are required to hand over your phone to the
invigilator.
• You will submit your submission to Canvas( Assignment, Final exam) with the following
filename format: Final_P1.py. Your submission MUST have a py extension.
• Good luck!
You are given a list of students and their grades in multiple subjects. Write a function to_pandas(students)
that takes in a list of students with their grades and returns a Pandas dataframe containing the
students’ grades and the average grade of each student, each rounded to 1 decimal place:
round(avg_grade, 1). Assume that there are only 2 subjects: math and history.
1. students is a list of dictionaries, where each dictionary represents a student and their
grades in math and history. The dictionary has 2 keys: "name" and "grades".
3. "grades": a dictionary representing the grades of the students in each subject (math and
history). The keys of this dictionary are the names of the subject. The values are the
grades of that student in each subject.
Example case:
>>> students = [{"name": "Alice", "grades": {"math": 90, "history": 85}},
{"name": "Bob", "grades": {"math": 75, "history": 80}},
{"name": "Eve", "grades": {"math": 82, "history": 90}}]
>>> grade_df=average_grades(students))
>>> grade_df
Extra credit (10pts): Your function should work for input of any length. For example, it can
take input like this:
You can see that Eve does not have a grade for English. So in the output data frame, Eve’s
score for English will be 0.
>>> students = [
{"name": "Alice", "grades": {"math": 90, "history": 85, "english":85}},
{"name": "Bob", "grades": {"math": 75, "history": 80, "english": 90}},
{"name": "Eve", "grades": {"math": 82, "history": 90}}]
>>> grade_df=average_grades(students))
>>> grade_df
Write a function recursion_power(a, b) that compute the value of "a" raised to the power of "b"
using recursion, i.e: ab .
In this problem, you are going to create a Python class named ApartRental. This class models
an apartment rental service with the following service:
1. A __init__(self, location, f loor, bedroom, rate) method that initializes the instance with
key attributes: location of the apartment, which floor it is on (4th floor, for instance), number
of bed rooms, and daily rental rate.
2. A calculate_cost(self, days) method for calculating the total rental cost based on the num-
ber of days the apartment is rented, with a price structure:
Example:
A sparse matrix is a matrix where most of the elements in the matrix are zeros. A sparse
representation is very convenient for problems that require heavy computation. However, at
the end of the day, you have to reconstruct the matrix from it’s sparse represetation. In this
problem, you will create a function sparse_construct(v,r,c) that returns a matrix A which can be
represented by (v,r,c) which are:
• A vector r which has the same length as vector v. Vector r stores the row index of the
corresponding value in vector v.
• A vector c which has the same length as vector v. Vector c stores the column index of the
corresponding value in vector v.
Hint: Numpy will be very helpful. As a matter of fact, (v,r,c) are Numpy arrays. For instance, an
input matrix A will be returned if you make a call sparse_construct(v,r,c) :
0 1 0 1 0 1
A= −1 0 0 v = −1 r = 1 c = 0
0 0 5 5 2 2