Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
35 views

Intro to Programming with Python - Final exam Practice Fall 2024

The document outlines the final exam instructions for BANA3020 Introduction to Programming with Python, scheduled for January 8, 2025, with a duration of 120 minutes. It includes guidelines on academic honesty, submission requirements, and exam conduct, as well as four programming problems covering topics such as Python data structures, recursion, object-oriented programming, and sparse matrices. Students are expected to write functions and classes as specified in the problems, adhering to the provided formats and requirements.

Uploaded by

Thanh Doãn
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Intro to Programming with Python - Final exam Practice Fall 2024

The document outlines the final exam instructions for BANA3020 Introduction to Programming with Python, scheduled for January 8, 2025, with a duration of 120 minutes. It includes guidelines on academic honesty, submission requirements, and exam conduct, as well as four programming problems covering topics such as Python data structures, recursion, object-oriented programming, and sparse matrices. Students are expected to write functions and classes as specified in the problems, adhering to the provided formats and requirements.

Uploaded by

Thanh Doãn
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Final Exam - Fall 2024

BANA3020 Introduction to Programming with Python January 08, 2025

(Practice)

Test duration: 120 minutes

Exam Submission Instructions:

• 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!

Final Exam (Theory Part) - Fall 2024 Page 1


Problem 1 (20pts) - Python dictionary, list, and dataframes

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".

2. "name": a string represent the name of the student.

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

1 Name math history Average


2 0 Alice 90 85 87.5
3 1 Bob 75 80 77.5
4 2 Eve 82 90 86.0

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

1 Name math history english Average


2 0 Alice 90 85 85 86.666667
3 1 Bob 75 80 90 81.666667

Final Exam (Theory Part) - Fall 2024 Page 2


4 2 Eve 82 90 0 57.333333

Problem 2 (30pts) - Recursion

Part A - Power (15pts)

Write a function recursion_power(a, b) that compute the value of "a" raised to the power of "b"
using recursion, i.e: ab .

1 print ( recur_power (3 ,4) )


2 81

Part B - Harmonic sum series (15pts)

A harmonic sum series is defined as the sum of reciprocals of positive integers.


1 1 1
H_n = 1 + + + ··· + .
2 3 n
Write a function recursion_hamonic(n) that takes a parameter n and return the harmonic sum
series of n using recursion.

1 print ( re cur si on _ha rm on ic (5) )


2 2.28333333333 3333

Problem 3 (20pts) - OOP

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:

• Up to 3 days: Standard daily rate.


• More than 3 and up to 7 days: 15% discount on the total cost.
• More than 7 and up to 14 days: 20 % discount on the total cost.
• More than 14 and above: 25% discount on the total cost.

Final Exam (Theory Part) - Fall 2024 Page 3


3. A __str__ method that returns a string representation of the apartment, including its loca-
tion, floor, number of bedrooms, and daily rental rate: "location,floor, bedroom,rate"

Example:

1 my_apt = ApartRental ( " Vin Ocean " ,4 ,2 ,20)


2 rental_days =3
3 print ( my_apt )
4 Vin Ocean ; 4 floor ; 2 bedrooms ; $60

Problem 4 (30pts) - Sparse matrix again

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 v that stores all the non-zero elements of the matrix.

• 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

Final Exam (Theory Part) - Fall 2024 Page 4

You might also like