Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Select and circle the best answer:

1. Consider the following code below and the list = [1, 4, 5, 7]    

for n in range(len(list)):
if (list[n] % 5 == 0 ):
print(list[n])

- The output will be:

a) 5
b) 4
c) All elements
d) None of mentioned.

2. After executing the following code, the output will be:

a) 14 def selectionSort(nlist):
b) 33 for i in range(len(nlist)):
c) All elements of the list minPosition = i
d) None of mentioned. for j in range(i+1, len(nlist)):
if nlist[minPosition] > nlist[j]:
minPosition = j
temp = nlist[i]
nlist[i] = nlist[minPosition]
nlist[minPosition] = temp

3. Whats the output of the following code?

a) 0.5 def mult_fun (x, y, z):


b) 2.0 if(x > y):
c) 3 print(y / x)
d) None of mentioned. elif(x < y):
print(y / x)
else:
print(z)
# ------------------------
mult_fun(2, 2, 3)
4. The output (position) of calling the
def LinearSearch(data, key):  
following LinearSearch([0, 4, 2, 0], 4): 
index = 0  
while( index < len(data) ):    
a)  0 if(data[index] == key ):
b) -1    break 
c) 3 index=index+1 
d) None of mentioned. if( index < len(data) ): 
return index    
else:      
return -1  

5. Number of operations in the following code def LinearSearch(data, key):  


following LinearSearch([0, 4, 2, 0], 0):  index = 0  
while( index < len(data) ):    
a) 0 if(data[index] == key ):
b) -1    break 
c) 3 index=index+1 
d) None of mentioned. if( index < len(data) ): 
return index    
else:      
return -1  
Part 2 (SA Questions):

1. For the list = [1, 15, 4, 15, 3, 16, 17, 13, 11].

- Write the list order for 4 iterations by using bubble sort.

def bubbleSort(alist):
for passnum in range(len(alist)-1, 0 ,-1):
for i in range(passnum):
if alist[i]>alist[i+1]:
print(alist)
temp = alist[i]
alist[i] = alist[i+1]
alist[i+1] = temp

1 15 4 15 3 16 17 13 11
1 4 15 15 3 16 17 13 11
1 4 15 3 15 16 17 13 11
1 4 15 3 15 16 13 17 11
1 4 15 3 15 16 13 11 17
2. For the list = [1, 15, 4, 15, 3, 16, 17, 13, 11].
- Write the list order for 3 iterations by using Selection sort.

def selectionSort(nlist):
for i in range(len(nlist)):
minPosition = i
for j in range(i+1, len(nlist)):
if nlist[minPosition] > nlist[j]:
minPosition = j
temp = nlist[i]
nlist[i] = nlist[minPosition]
nlist[minPosition] = temp

1 15 4 15 3 16 17 13 11
1 3 4 15 15 16 17 13 11
1 3 4 11 15 16 17 13 15
1 3 4 11 13 16 17 15 15
1 3 4 11 13 15 17 16 15

- Write 3 midpoint values (indices-if any) by using binary search for the list below. (target
= 3). (Show all work).

def binarySearch(alist, item):


first = 0
last = len(alist)-1
found = False
while first <= last and not found:
midpoint = (first + last)//2
if alist[midpoint] == item:
found = True
else:
if item < alist[midpoint]:
last = midpoint-1
else:
first = midpoint+1
return found

0 1 2 3 4 5 6 7 8

11 3 4 1 13 15 9 16 17
1 3 4 9 11 13 15 16 17

Sort the List …….

Target = 3
M1 = (0+8)/2 = 4 = 11
M2 = (0+3)/2 = 1 = 3
Midpoint is = Found

3. For the list = [1, 15, 4, 15, 3, 16, 17, 13, 11].
- Write the list order for 3 iterations by using Insertion sort.

def insertionSort(nlist):
for index in range(1,len(nlist)):
currentvalue = nlist[index]
position = index
while position>0 and nlist[position-1]> currentvalue:
nlist[position]=nlist[position-1]
position = position-1
nlist[position]=currentvalue

1 15 4 15 3 16 17 13 11
1 4 15 15 3 16 17 13 11
1 3 4 15 15 16 17 13 11
1 3 4 13 15 15 16 17 11
1 3 4 11 13 15 15 16 17

You might also like