COMPUTER SICENCE
CHAPTER 7
TYPE A SOLUTIONS
Answer 7
1. Linear lists: - Storage of unique id of employee of a company.
2. Queue: - CPU scheduling, Disk Scheduling, appointment list i.e. FIFO
implementation.
Answer 13 A two-dimension list can be used to implement marks or roll numbers of
student in various terms, Sales record, account details, etc. Basically where ever we
have to store data in a table format we can use a 2D list.
Answer 15 Ragged list can be used to store list of students in different classes, or
account details of different branches, etc.
Answer 17
TYPE B SOLUTIONS
Answer 1 Output:
[2, 5, 1, 7, 3, 6, 8, 9, 2, 5, 1, 7, 3, 6, 8, 9]
No, this code will just duplicate and add the list not as the desired purpose in question.
Answer 2 To make the code to work according to the purpose.
SqLst = [2*i for i in NumLst]
Answer 3 SqLst = [2*i for i in NumLst]
Answer 4 The code for above condition
ML =(1,4,9,16,25,36,49,64,81,100)
l1=[i for i in ML if(i%2 == 0)]
print(l1)
Page 1 of 5
Output:
[4, 16, 36, 64, 100]
Answer 5
ML =(1,4,9,16,25,36,49,64,81,100)
l1=[i for i in ML if(i%2 != 0)]
print(l1)
Output:
[1, 9, 25, 49, 81]
Answer 6
gen = []
for i in [0, 9, 21, 32]:
gen.append(i/2)
print(gen)
Output:
[0.0, 4.5, 10.5, 16.0]
Answer 7
(i) 0
(ii) 0
Answer 8 Output:
5 [21, 12] 3
5 [21, 12] 3
[21, 12] 31 2
Answer 9 Indentation error
Answer 10 Output:
[0, 9, 36, 81, 4]
[0, 9, 36, 81, 4]
[0, 9, 36, 81]
Answer 11 Output:
[2, 4, 6, 8]
Answer 12 Output:
(i) [[9, 6], [4, 5], 10]
(ii) [[9, 6], [4, 5, 10]]
Answer 13 The code will cause error if ch is 2 because then value 100 being absent in
Lst1 and Lst2 and its index being asked. Also, the error will encounter on ch is 3 as Lst2
is empty and underflow case (nothing to pop out) arise.
Page 2 of 5
Answer 14 # corrected code
Lst1 = [23, 34, 12, 77, 34, 26, 28, 93, 48, 69, 73, 23, 19, 88]
Lst2 = []
print("List1 original is :", Lst1)
ch = int(input("Enter 1/2/3 and predict which operation was performed?"))
if ch == 1:
Lst1.append(100)
Lst2.append(100)
elif ch == 2:
if Lst1.count(100) >0:
print(Lst1.index(100))
if Lst2.count(100) >0:
print(Lst2.index(100))
elif ch == 3:
print(Lst1.pop())
Answer 15
(i) The if statement is called twice and list comprehension is called in incorrect way
#correct code
[y for y in range(100) if (y % 2 == 0 and y % 5 == 0)]
(ii) The twice if statement
# correct code
(y for y in range(100) if (y % 2 == 0 and y % 5 == 0 ))
Answer 16 #correct code
["good" if i < 3 else "better" for i in range(6)]
No requirement of the colon in list comprehension.
Answer 17
For Question 15 –
(i) The if statement is called twice and list comprehension is called in incorrect way
#correct code
[y for y in range(100) if (y % 2 == 0 and y % 5 == 0)]
(ii) The twice if statement is the error in this case.
# correct code
(y for y in range(100) if (y % 2 == 0 and y % 5 == 0 ))
For Question 16 –
#correct code
["good" if i < 3 else "better" for i in range(6)]
No requirement of the colon in list comprehension.
Page 3 of 5
TYPE C SOLUTIONS
Answer 1
def find_in_list(lst, v):
for i in range(len(lst)):
if lst[i] == v:
return i
break
return -1
l = [1, 2, 3, 4]
print(find_in_list(l, 3)) # 2
print(find_in_list(l, 5)) # -1
Output:
2
-1
Answer 2
def unique(L):
return len(set(L))
L=[]
print(unique(L))
L=[1,3,3,4,4,8]
print(unique(L))
L=[1,2,2]
print(unique(L))
L=[33,33,33]
print(unique(L))
Output:
0
4
2
1
Answer 3
cubes_by_four =[x**3 for x in range(1,11) if (x**3)%4==0]
for x in cubes_by_four:
print(x)
Output:
8
64
216
Page 4 of 5
512
1000
Answer 4
a=[1,1,2,3,4,5,8,13,21,34,55,89]
b=[1,2,3,4,5,6,7,8,9,10,11,12,13]
def common_elements(a,b):
a_set=set(a)
b_set=set(b)
if (a_set & b_set):
print(a_set & b_set)
else:
print("Two lists have nothing on common!")
common_elements(a,b)
Output:
{1, 2, 3, 4, 5, 8, 13}
Answer 5
V = [[2, 6], [3, 10], [15], [23], [1, 8, 15, 22, 29], [14]]
def Mvisit(l):
c = 0 #Counter
hv = None # the visits
for i in V:
if len(i) > c: # compares numbers of visits
c = len(i)
hv = i
print("Highest number of visits are :", hv)
Mvisit(V)
Output:
Highest number of visits are : [1, 8, 15, 22, 29]
Page 5 of 5