6/7/25, 6:45 PM Week10_Coding: Attempt review | REC-CIS
Dashboard / My courses / GE23231/GE23233-PUP/PSPP-2024 / Searching techniques: Linear and Binary / Week10_Coding
Status Finished
Started Saturday, 7 June 2025, 5:10 PM
Completed Saturday, 7 June 2025, 6:15 PM
Duration 1 hour 5 mins
Marks 5.00/5.00
Grade 100.00 out of 100.00
[Link]/moodle/mod/quiz/[Link]?attempt=223488&cmid=604 1/9
6/7/25, 6:45 PM Week10_Coding: Attempt review | REC-CIS
Question 1
Correct
Mark 1.00 out of 1.00
Given an listof integers, sort the array in ascending order using the Bubble Sort algorithm above. Once sorted, print the following three
lines:
1. List is sorted in numSwaps swaps., where numSwaps is the number of swaps that took place.
2. First Element: firstElement, the first element in the sorted list.
3. Last Element: lastElement, the last element in the sorted list.
For example, given a worst-case but small array to sort: a=[6,4,1]. It took 3 swaps to sort the array. Output would be
Array is sorted in 3 swaps.
First Element: 1
Last Element: 6
Input Format
The first line contains an integer,n , the size of the list a .
The second line contains n, space-separated integers a[i].
Constraints
· 2<=n<=600
· 1<=a[i]<=2x106.
Output Format
You must print the following three lines of output:
1. List is sorted in numSwaps swaps., where numSwaps is the number of swaps that took place.
2. First Element: firstElement, the first element in the sorted list.
3. Last Element: lastElement, the last element in the sorted list.
Sample Input 0
3
123
Sample Output 0
List is sorted in 0 swaps.
First Element: 1
Last Element: 3
For example:
Input Result
3 List is sorted in 3 swaps.
3 2 1 First Element: 1
Last Element: 3
5 List is sorted in 4 swaps.
1 9 2 8 4 First Element: 1
Last Element: 9
Answer: (penalty regime: 0 %)
1 x = int(input())
2 y = list(map(int,input().split(' ')))
3 z = [Link]()
4 count = 0
5 ▼ for i in range(len(y)) :
6 ▼ for j in range(i,len(y)) :
7 ▼ if z[i] > z[j] :
8 z[i],z[j] = z[j],z[i]
[Link]/moodle/mod/quiz/[Link]?attempt=223488&cmid=604 2/9
6/7/25, 6:45 PM Week10_Coding: Attempt review | REC-CIS
9 count += 1
10
11 print(f"List is sorted in {count} swaps.")
12 print(f"First Element: {z[0]}")
13 print(f"Last Element: {z[-1]}")
14
Input Expected Got
3 List is sorted in 3 swaps. List is sorted in 3 swaps.
3 2 1 First Element: 1 First Element: 1
Last Element: 3 Last Element: 3
5 List is sorted in 4 swaps. List is sorted in 4 swaps.
1 9 2 8 4 First Element: 1 First Element: 1
Last Element: 9 Last Element: 9
Passed all tests!
Correct
Marks for this submission: 1.00/1.00.
[Link]/moodle/mod/quiz/[Link]?attempt=223488&cmid=604 3/9
6/7/25, 6:45 PM Week10_Coding: Attempt review | REC-CIS
Question 2
Correct
Mark 1.00 out of 1.00
Write a Python program to sort a list of elements using the merge sort algorithm.
For example:
Input Result
5 3 4 5 6 8
6 5 4 3 8
Answer: (penalty regime: 0 %)
1 x = int(input())
2 y = input()
3 y = [Link](' ')
4 [Link]()
5 for i in y : print(i,end=' ')
Input Expected Got
5 3 4 5 6 8 3 4 5 6 8
6 5 4 3 8
9 14 21 27 41 43 45 46 57 70 14 21 27 41 43 45 46 57 70
14 46 43 27 57 41 45 21 70
4 23 43 49 86 23 43 49 86
86 43 23 49
Passed all tests!
Correct
Marks for this submission: 1.00/1.00.
[Link]/moodle/mod/quiz/[Link]?attempt=223488&cmid=604 4/9
6/7/25, 6:45 PM Week10_Coding: Attempt review | REC-CIS
Question 3
Correct
Mark 1.00 out of 1.00
Write a Python program for binary search.
For example:
Input Result
1,2,3,5,8 False
6
3,5,9,45,42 True
42
Answer: (penalty regime: 0 %)
1 x = input()
2 y = input()
3 x = [Link](',')
4 count = 0
5 z = [Link]()
6 count = 0
7 ▼ while True:
8 ▼ if y not in z[:len(z)//2] and y not in z[len(z)//2:]:
9 break
10 ▼ elif y in z[:len(z)//2] :
11 count +=1
12 z = z[:len(z)//2]
13 break
14 ▼ elif y in z[len(z)//2:] :
15 count +=1
16 z = z[len(z)//2:]
17 break
18 ▼ if count > 0:
19 print(True)
20 ▼ else :
21 print(False)
22
Input Expected Got
1,2,3,5,8 False False
6
3,5,9,45,42 True True
42
52,45,89,43,11 True True
11
Passed all tests!
Correct
Marks for this submission: 1.00/1.00.
[Link]/moodle/mod/quiz/[Link]?attempt=223488&cmid=604 5/9
6/7/25, 6:45 PM Week10_Coding: Attempt review | REC-CIS
Question 4
Correct
Mark 1.00 out of 1.00
An list contains N numbers and you want to determine whether two of the numbers sum to a given number K. For example, if the input
is 8, 4, 1, 6 and K is 10, the answer is yes (4 and 6). A number may be used twice.
Input Format
The first line contains a single integer n , the length of list
The second line contains n space-separated integers, list[i].
The third line contains integer k.
Output Format
Print Yes or No.
Sample Input
7
0124653
1
Sample Output
Yes
For example:
Input Result
5 Yes
8 9 12 15 3
11
6 No
2 9 21 32 43 43 1
4
Answer: (penalty regime: 0 %)
1 x = int(input())
2 y = list(map(int,input().split(' ')))
3 z = int(input())
4 count = 0
5 ▼ for i in y :
6 ▼ for j in y :
7 ▼ if i+j == z and [Link](i) != [Link](j):
8 count +=1
9 ▼ if count > 0 :
10 print('Yes')
11 ▼ else :
12 print ('No')
13
[Link]/moodle/mod/quiz/[Link]?attempt=223488&cmid=604 6/9
6/7/25, 6:45 PM Week10_Coding: Attempt review | REC-CIS
Input Expected Got
5 Yes Yes
8 9 12 15 3
11
6 No No
2 9 21 32 43 43 1
4
6 Yes Yes
13 42 31 4 8 9
17
Passed all tests!
Correct
Marks for this submission: 1.00/1.00.
[Link]/moodle/mod/quiz/[Link]?attempt=223488&cmid=604 7/9
6/7/25, 6:45 PM Week10_Coding: Attempt review | REC-CIS
Question 5
Correct
Mark 1.00 out of 1.00
To find the frequency of numbers in a list and display in sorted order.
Constraints:
1<=n, arr[i]<=100
Input:
1 68 79 4 90 68 1 4 5
output:
12
42
51
68 2
79 1
90 1
For example:
Input Result
4 3 5 3 4 5 3 2
4 2
5 2
Answer: (penalty regime: 0 %)
1 a = list(map(int,input().split()))
2 b = set(a)
3 ▼ for i in b :
4 print(i,[Link](i))
Input Expected Got
4 3 5 3 4 5 3 2 3 2
4 2 4 2
5 2 5 2
12 4 4 4 2 3 5 2 1 2 1
3 1 3 1
4 3 4 3
5 1 5 1
12 1 12 1
[Link]/moodle/mod/quiz/[Link]?attempt=223488&cmid=604 8/9
6/7/25, 6:45 PM Week10_Coding: Attempt review | REC-CIS
Input Expected Got
5 4 5 4 6 5 7 3 3 1 3 1
4 2 4 2
5 3 5 3
6 1 6 1
7 1 7 1
Passed all tests!
Correct
Marks for this submission: 1.00/1.00.
◄ Week10_MCQ
Jump to...
Sorting ►
[Link]/moodle/mod/quiz/[Link]?attempt=223488&cmid=604 9/9