CSE221 Lab1
CSE221 Lab1
Lab 01
Fall 2024
Task 1:
a) You are given a file “input1a.txt”. The first line of the input file will contain an integer T,
representing the number of test cases. The next T lines will contain an integer N. You
have to calculate if the number is Odd or Even. For each test case print the expected
output. All the results must be compiled in a single file, “output1a.txt.”
5 10 is an Even number.
10 19 is an Odd number.
19 7 is an Odd number.
7 3 is an Odd number.
3 100 is an Even number.
100
b) You are given a file “input1b.txt”. The first line of the input file will contain an integer T,
representing the number of test cases. The next T lines will contain a single arithmetic
expression. Each arithmetic expression will start with the prefix “calculate“. The
expression is guaranteed to have exactly two operands and one operator.
Calculate the result of each expression. Your output format should match the sample
output format exactly. All the results must be compiled in a single file, “output1b.txt.”
Task 2:
Here is the code of bubble sort. Its run time complexity is θ(n2). Change the code in a way so
that its time complexity is θ(n) for the best-case scenario.
You have to explain how you have achieved the θ(n) for the best-case scenario in a
comment block of your code.
def bubbleSort(arr):
for i in range(len(arr)-1):
for j in range(len(arr)-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
Input 1: Output 1:
5 12345
32145
Input 2: Output 2:
6 5 10 15 20 25 30
5 10 15 20 25 30
Please note, that you have to take the input from an input2.txt file and show the output in
an output2.txt file.
Task 3:
Suppose you are given a task to rank the students. You have gotten the marks and ID of the
students. Now your task is to rank the students based on their marks using a sorting algorithm.
However, you have to keep in mind that your sorting algorithms perform the minimum
number of swapping operations.
Input:
The first line of the input file will contain an integer N ( 1 ≤ N ≤ 1000 ).
The second line will contain N integers, representing the Student ID, Si ( 1 ≤ Si ≤ 1000 ). The
next line will contain the N integer, Sm ( 1 ≤ Sm ≤ 1000 ), which denotes the obtained mark of the
corresponding students.
Output:
You have to show the Student ID and obtained marks in descending order based on their
obtained mark. If two or more students get the same mark, then students with the lower ID will
get prioritized. See the input and output for a better understanding.
Input 1: Output 1:
7 ID: 4 Mark: 50
7493251 ID: 9 Mark: 50
40 50 50 20 10 10 10 ID: 7 Mark: 40
ID: 3 Mark: 20
ID: 1 Mark: 10
ID: 2 Mark: 10
ID: 5 Mark: 10
Input 2: Output 2:
4 ID: 5 Mark: 80
7253 ID: 7 Mark: 80
80 60 80 50 ID: 2 Mark: 60
ID: 3 Mark: 50
Please note, that you have to take the input from an input3.txt file and show the output in
an output3.txt file
Task 4:
You have been recently recruited as the Software Engineer at Jumanji Railway Software
System. You have a big task at hand. You will be given the N ( 1 ≤ N ≤ 100 ) schedule of
the train. The next N line will contain the name of the train and the departure time. See
the input format for better understanding.
Your task is to write a sorting algorithm that will group the trains in the lexicographical
order based on the name of the trains. If two or more trains have the same name, then
the train with the latest departure time will get prioritized. If there is still a tie, then the
train which comes first in the input file will come first.
Please note, that you have to take the input from an input4.txt file and show the
output in an output4.txt file.