0% found this document useful (0 votes)
40 views4 pages

CSE221 Lab1

Uploaded by

Kazi Tasmia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views4 pages

CSE221 Lab1

Uploaded by

Kazi Tasmia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

CSE221: Algorithms

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

Sample Input File Sample Output File

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

Sample Input File Sample Output File

15 The result of 67 + 41 is 108


calculate 67 + 41 The result of 85 / 5 is 17.0
calculate 85 / 5 The result of 13 - 56 is -43
calculate 13 - 56 The result of 99 - 95 is 4
calculate 99 - 95 The result of 3 / 10 is 0.3
calculate 3 / 10 The result of 12 * 19 is 228
calculate 12 * 19 The result of 14 - 6 is 8
calculate 14 - 6 The result of 3 * 88 is 264
calculate 3 * 88 The result of 45 * 68 is 3060
calculate 45 * 68 The result of 81 - 0 is 81
calculate 81 - 0 The result of 77 + 40 is 117
calculate 77 + 40 The result of 8 * 84 is 672
calculate 8 * 84 The result of 73 - 22 is 51
calculate 73 - 22 The result of 85 - 86 is -1
calculate 85 - 86 The result of 28 * 58 is 1624
calculate 28 * 58

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

For the input 2, your code should run at θ(n).

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.

Sample Input Sample Output

13 ABC will departure for Dhaka at 17:30


ABCD will departure for Mymensingh at 00:30 ABC will departure for Khulna at 03:00
DhumketuExpress will departure for Chittagong at 02:30 ABC will departure for Barisal at 03:00
SubornoExpress will departure for Chittagong at 14:30 ABCD will departure for Chittagong at 01:00
ABC will departure for Dhaka at 17:30 ABCD will departure for Mymensingh at 00:30
ShonarBangla will departure for Dhaka at 12:30 ABCE will departure for Sylhet at 23:05
SubornoExpress will departure for Rajshahi at 14:30 DhumketuExpress will departure for Chittagong at 02:30
ABCD will departure for Chittagong at 01:00 PadmaExpress will departure for Chittagong at 20:30
SubornoExpress will departure for Dhaka at 11:30 PadmaExpress will departure for Dhaka at 19:30
ABC will departure for Khulna at 03:00 ShonarBangla will departure for Dhaka at 12:30
PadmaExpress will departure for Chittagong at 20:30 SubornoExpress will departure for Chittagong at 14:30
ABC will departure for Barisal at 03:00 SubornoExpress will departure for Rajshahi at 14:30
ABCE will departure for Sylhet at 23:05 SubornoExpress will departure for Dhaka at 11:30
PadmaExpress will departure for Dhaka at 19:30

Please note, that you have to take the input from an input4.txt file and show the
output in an output4.txt file.

You might also like