CS102_Lab1_Spring2025
CS102_Lab1_Spring2025
Lab Week 1
1 Introduction 3
1.1 Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Marking scheme . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.3 Lab Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.4 Late submission policy . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.5 Use of AI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.6 Viva . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2 Prerequisites 5
2.1 Assert Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2 Pytest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3 Lab Exercises 7
3.1 Last name first . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.1.1 Challenge . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.1.2 Note . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.1.3 Sample Interaction . . . . . . . . . . . . . . . . . . . . . . . 7
3.1.4 Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.1.5 Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.2 Dude, Where’s My Robot? . . . . . . . . . . . . . . . . . . . . . . . 8
3.2.1 Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3.2.2 Function Description . . . . . . . . . . . . . . . . . . . . . . 8
3.2.3 Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3.2.4 Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3.2.5 Sample Interaction . . . . . . . . . . . . . . . . . . . . . . . 8
3.2.6 Sample Interaction Explaination . . . . . . . . . . . . . . . . 9
3.2.7 Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.3 Loan Repayment Strategy . . . . . . . . . . . . . . . . . . . . . . . 9
3.3.1 Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.3.2 Function Description . . . . . . . . . . . . . . . . . . . . . . 9
3.3.3 Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.3.4 Sample Interaction . . . . . . . . . . . . . . . . . . . . . . . 10
3.3.5 Sample Interaction Explaination . . . . . . . . . . . . . . . . 10
3.3.6 Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.4 Social Network User Removal . . . . . . . . . . . . . . . . . . . . . 10
3.4.1 Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1
3.4.2 Function Description . . . . . . . . . . . . . . . . . . . . . . 11
3.4.3 Sample Interaction . . . . . . . . . . . . . . . . . . . . . . . 11
3.4.4 Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.5 Updating Employee Records . . . . . . . . . . . . . . . . . . . . . . 11
3.5.1 Function Description . . . . . . . . . . . . . . . . . . . . . . 11
3.5.2 Sample Interaction . . . . . . . . . . . . . . . . . . . . . . . 12
3.5.3 Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2
Introduction
1.1 Instructions
• This lab will contribute 1% towards your final grade.
• The deadline for submission of this lab is at the end of lab time.
• The lab must be submitted online via CANVAS. You are required to submit
a zip file that contains all the .py files.
• The zip file should be named as lab01 aa12345.zip where aa12345 will be
replaced with your student id.
• Files that don’t follow the appropriate naming convention will not
be graded.
• Your final grade will comprise of both your submission and your
lab performance.
3
1.5 Use of AI
Taking help from any AI-based tools such as ChatGPT is strictly prohibited and
will be considered plagiarism.
1.6 Viva
Course staff may call any student for Viva to provide an explanation for their
submission.
4
Prerequisites
2.2 Pytest
Pytest is a popular testing framework in Python known for its simplicity and
power in facilitating efficient and scalable testing for applications and software. In
order to use Pytest, one first needs to install it and create test functions prefixed
with test in a Python file. To install Pytest, you can write the following command
on the terminal:
pip install pytest
After installing Pytest, one can execute all the tests by typing the following
command on the terminal:
pytest
If you want to test any particular file, then you will have to specify the filename
as follows:
pytest < filename >. py
If all the tests pass successfully, then you should receive a similar output.
5
However, if all tests fail, then you should receive a similar output.
6
Lab Exercises
3.1.1 Challenge
Write a function named last name first that accepts a single parameter t, which
is passed as a list of tuples. Each tuple contains a name in parts (first name, middle
name, last name). Your function should modify each name so that the last name
appears first in the tuple.
3.1.2 Note
This function modifies a list in place and, as such, should not return any useful
value.
>> t = [( ' Ahmed ' , ' Dawood ') , ( ' Haroon ' , ' Hussain ' , ' Fawad ' ,
' Rasheed ') , ( ' Muhammad ' , ' Faisal ' , ' Amin ') ]
>> last_name_first ( t )
>> t
[( ' Dawood ' , ' Ahmed ') , ( ' Rasheed ' , ' Haroon ' , ' Hussain ' , '
Fawad ') , ( ' Amin ' , ' Muhammad ' , ' Faisal ') ]
3.1.4 Constraints
t is a list of tuples, where each tuple has one or more strings in it.
3.1.5 Testing
In order to test your function, type the following command on the terminal:
pytest test_q1 . py
7
3.2 Dude, Where’s My Robot?
This function should be implemented in the file q2.py.
3.2.1 Problem
Your neighbor’s garden robot was hacked and moved from its charging pod where
you had last left it. They cannot find the robot and need the grass trimmed right
now for a party with their committee friends this evening. Luckily, they know of
your skill with computers. You manage to retrieve the last set of instructions sent
to the robot. Now you just have to figure out how far away the robot has moved.
The set contains n instructions. Each instruction is of the form direction distance.
On receiving this instruction, the robot moves distance in direction. The robot
executes the instructions one after the other. Given the list of instructions, you
want to find out the distance that the robot has moved.
3.2.3 Input
The input is a list of tuples where each tuple contains the direction (where direction
is one of UP, DOWN, RIGHT, and LEFT ) and distance of type integer.
3.2.4 Output
Return the total distance that the robot has moved from its original distance once
it has carried out the given instructions. Round the distance up to the nearest
integer.
>> total_distance ([( ' UP ' , '5 ') , ( ' DOWN ' , '3 ') , ( ' LEFT ' , '3 ')
, ( ' RIGHT ' , '2 ') ])
3
>> total_distance ([( ' RIGHT ' , 1) , ( ' DOWN ' , 0) , ( ' LEFT ' , -2) ,
( ' DOWN ' , -4) ])
5
8
3.2.6 Sample Interaction Explaination
In the first sample, the robot moved 5 up, then 3 down, then 3 left, and then 2
right. Reluctantly, the robot is 2 up and 1 left from its original position. The
distance is math.sqrt(2 ∗ 2 + 1 ∗ 1) which, rounded up, is 3.
In the second sample, the robot is 3 right and 4 up from its position. The distance
is math.sqrt(3 ∗ 3 + 4 ∗ 4) which, rounded up, is 5.
In the third case, the robot has not moved. The distance is 0.
3.2.7 Testing
In order to test your function, type the following command on the terminal:
pytest test_q2 . py
3.3.1 Problem
On the advice of your relative from the stock market, you have invested in stock
in the hope to eventually pay off your Habib loan. Your relative sends you daily
updates on your stocks in the following form.
9
• purchase date : int
• shares : int
• symbol : str
3.3.3 Constraints
• The argument contains at least 1 tuple.
>> compute_profit ([( ' 25 - Jan -2001 ' , 43.5 , 25 , ' CAT ' , 92.45) ,
( ' 25 - Jan -2001 ' , 42.8 , 50 , ' DD ' , 51.19) , ( ' 25 - Jan -2001 ' ,
42.1 , 75 , ' EK ' , 34.87) , ( ' 25 - Jan -2001 ' , 37.58 , 100 , ' GM ' ,
37.58) ])
1101
3.3.6 Testing
In order to test your function, type the following command on the terminal:
pytest test_q3 . py
10
3.4.1 Problem
You are building a social networking application focused on connecting people
based on common interests. The application stores user data in a dictionary
users data where each user’s name is the key. The corresponding values include
the user’s phone number and a list of friends they have connected with. Your task
is to create a function that removes user name and all their connections from the
users data.
>> users_data = {
.. ' Alice ': ( ' 123 -456 -7890 ' , [ ' Bob ' , ' Charlie ' ]) ,
.. ' Bob ': ( ' 987 -654 -3210 ' , [ ' Alice ' , ' David ' ]) ,
.. ' Charlie ': ( ' 111 -222 -3333 ' , [ ' Alice ' ]) ,
.. ' David ': ( ' 555 -666 -7777 ' , [ ' Bob ' ])
}
3.4.4 Testing
In order to test your function, type the following command on the terminal:
pytest test_q4 . py
11
3. record title – The type of data that needs to be updated. It can be ID,
Position, Salary or Experience.
4. data – data (string or int): The new data that should replace the existing
data in the specified record title.
3. If the ID is found, update the corresponding record title with the new data
and return a message: Record updated .
>> employee_records = [
.. ( " E001 " , " Manager " , 80000 , 5) ,
.. ( " E002 " , " Developer " , 60000 , 2) ,
.. ( " E003 " , " Analyst " , 50000 , 1) ,
.. ( " E004 " , " Designer " , 70000 , 3)
]
>> update_record ( employee_records , " E001 " , " ID " , " E005 " )
ID cannot be updated
>> update_record ( employee_records , " E001 " , " Position " , "
Senior Manager " )
[( " E001 " , " Senior Manager " , 80000 , 5) , ( " E002 " , " Developer " ,
60000 , 2) , ( " E003 " , " Analyst " , 50000 , 1) , ( " E004 " , "
Designer " , 70000 , 3) ]
>> update_record ( employee_records , ' E005 ' , " Salary " , 55000)
Record not found
3.5.3 Testing
In order to test your function, type the following command on the terminal:
pytest test_q5 . py
12