Linear Lists in Python Solutions for Class 12

Solutions of Data Structure -I : Linear Lists chapter of Computer Science (Python) NCERT Class 12. Solved Question answer from Linear Lists in Python topic from Sumita Arora book for the preparation of upcoming annual board exams.

Short Answer Questions : Linear Lists in Data Structure

Q.1. What are data structures? Name some common data structures.

Answer:
Data structures are the way of organising data in computer so that it can be used effectively. Queue and stack are some of the common data structures.

Q.2. Is data structure related to a data type ? Explain.

Answer:
A data structure contains values which can be of different data type. No data structure needs to have value of data type of same type.
s=[5,’a’,’peace’,True] is correct .

Q.3. What do you understand by linear and non-linear data structures?

Answer:
A data structure is said to be of linear if its elements form a sequence. e.g. stack , queue , linked list.
A data structures where the elements are stored at different levels. e.g. Tree

Q.4.Name some linear data structures. Is linked list a linear data structure ?

Answer:
A linear data structure contains data in the form of sequences. Some of them are stacks, queue. Yes, linked list a linear data structure.

Q.5.What is the working principle of data structures stack and queues?

Answer:
Both stacks and queues are linear data structure.
stack follows LIFO – Last in first out. The last elements is ejected first.
Queue follow FIFO-first in first out. The first element is ejected first.

Q.6. What is linear list data structure? Name some operations that you can perform on linear lists.

Answer:
A linear list data structure is a sequence of elements. When all its elements are homogenous and are stored in sequential memory locations. then it is called an array. Operations that can be performed on linear list are insertion, deletion, searching, traversal,sorting, merging.

Q.7.Suggseted situtation where you can use these data structures
a)linear lists
Answer:
To store addresses of people in a company
b)stacks
Answer:
To create undo mechanism in a text editor
c)Queues
Answer:
To store roll number of students in a class.

Q.8.What is a list comprehension? How is it useful?

Answer:
List comprehension is a concise description of a list that shorthands the list creating for loop in the form of a single statement. They are useful when we need to reduce the code size and make it more readable and more efficient. e.g.
l=[i*2 for i in range(5)] #l=[0,2,4,6,8]

Q.9.Enlist some advantage of list comprehensions.

Answer:
(i) Code reduction: A single line in most cases to create a list with elements.
(ii) Faster code processing: calls to append() are avoided

Q.10.In which situations should you use list comprehension and in which situation you should not use list comprehension

Answer:
List comprehensions should be used when there is a need to decrease code size and increase readability and performance. IT should not be used where there are multiple cases to check. e.g.
l=[i for i in range(10)if i%2==0] #can be used
l=[ i for i in range(10)if i%7==0 and i%5==0]#can be used here too but hard to understand and keep check

Q.11.What is a nested list? Give some examples.

Answer:
A list that contains one or more lists as its element is called a nested list.e.g.
a=[1,2,3]
b=[a,4,5,6] # b is a nested list

Q.12.What is a two dimensional list? How is it related to nested lists.

Answer:
A two dimensional list is a list of lists i.e. each of its element is another list. It can be visualized as a table with each inner list being a row. e.g.
l=[[1,2,3],[4,5,6]]
A two dimensional list is a nested list where all its elements are lists rather than some.

Q.13.Suggest a situation where you can use a regular two dimensional list.

Answer:
A two dimensional list can be used to store roll numbers of students in different sections in a class. e.g.
roll=[[4015,4016,4017],[4178,4187,4189],[4232,4235,4256]]

Q.14.What are ragged list? How are these different from two dimensional lists?

Answer:
A ragged list is a list that has lists with different shapes as its elements. It is also a 2d list but it is an irregular 2d list. They are different from regular 2d lists as all the lists in a regular 2d list have the same space.
ragged=[[1,2,3],[3,4],[5,6,7,8]
regular= [[4015,4016,4017],[4178,4187,4189],[4232,4235,4256]]

Q.15.Suggest a situation where you can use ragged list?

Answer:
Ragged lists can be used to store vehicle numbers of vehicles in different districts of a state. e.g.
veh=[[4069,4078],[5045]]

Q.16.How are lists internally stored? How are 2D lists internally stored?

Answer:
Lists are stored in memory exactly like strings , expect that because some of their objects are larger than others,they store a refrence at each index instead of single character as in strings. Each of the individual items of the list are stored somewhere else in memory.Similarly, a 2 list stores references of its element list at it indexes.

Q.17.Consider the following code and show how internally the memory is assigned to these
List1=[2,3]
List2=[3,4,5]
List3=[List1,List2]
Assume that the numbers 1….10 are stored in the memory addresses 16000 onwards,each consuming 16 bytes of memory.

Answer:

Linear Lists in Python Solutions for Class 12

Application Based Question : Linear Lists in Python Solutions for Class 12

Q1.Create a list SqLst that stores the doubles of elements of another list NumLst. Following code is trying to achieve this. Will this code is trying to achieve this. Will this code work as desired? What will be stored in SqLst after following code?
NumLst=[2,5,1,7,3,6,8,9]
SqlLst= NumLst*2

Answer:
The above code will not work as desired. It will concatenate two NumLst and contain [2,5,1,7,3,6,8,9,2,5,1,7,3,6,8,9]
To achieve the purpose we can do
NumLst= [2,5,1,7,3,6,8,9]
SqlLst=[2*i for i in NumLst]

Q2.Change the above code so that it works as stated in previous question.

Answer:
NumLst= [2,5,1,7,3,6,8,9]
SqlLst=[2*i for i in NumLst]

Q3.Modify your previous code so that SqList stores the doubled numbers in ascending order.

Answer:
NumLst= [2,5,1,7,3,6,8,9]
SqlLst=[2*i for i in sorted(NumLst)]

Q4. Consider a list ML = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]. Write code using a list comprehension that takes the list ML and makes a new list that has only the even elements of this list in it.

Answer:
ML = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
new = [i for i in ML if i%2 == 0] # i%2 == 0 checks for even numbers
print(new) # [4, 16, 36, 64, 100]

Q5. Write equivalent list comprehension for the following code :
target1=[]
for number in source:
if number & 1:
target1.append(number)

Answer:
target1=[number for number in source if number & 1]

Q6.Write equivalent for loop for the following list comprehension :
gen=(i/2 for i in [0,9,21,32])
print(gen)

Answer:
gen=[ ]
for i in [0,9,21,32]:
gen.append(i/2)
print(gen)

Q7. Predict the output of following code if the input is :
(i)12,3,4,5,7,12,8,23,12
(ii)8,9,2,3,7,8
s= eval(input(“enter a list:”))
n=len(s)
t=s[1:n-1]
print(s[0]==s[n-1] and t.count(s[0])==0)

Answer:
(i) False
(ii)True

Q8. Predict the output :

def h_t(NLst): #function swapping first and last element
from_back= Nlst.pop()
from_front=Nlst.pop(0)
NLst.append(from_front)
Nlst.insert(0,from_back)
NLst1 = [[21,12],31]
NLst3=NLst1.copy() # shallow copy
NLst2=Nlst1
Nlst[-1]=5
NLst2.insert(1,6)
h_t(NLst1)
print(NLst1[0],NLst[-1],len(NLst1))
print(NLst1[0],NLst[-1],len(NLst2))
print(NLst1[0],NLst[-1],len(NLst3))
#Nlst2 is a shallow copy, so changes made to it or NLst1 won’t affect each other

Answer:
#output
5 [21,12] 3
5 [21,12] 3
[21,12] 31 2

Q9.Predict the output :
ages=[11,14,15,17,13,18,25]
print(ages)
Elig=[x for x in ages if x in range (14,18)]
print(Elig)

Answer:
Only those ages will be inserted in Elig which are in the range 14 to 17.
[11,14,15,17,13,18,25]
[14,15,17]

Q10.Predict the output :
L1=[x**2 for x in range(10) if x%3 ==0]
L2=L1
L1.append(len(L1))
print(L1)
print(L2)
L2.remove(len(L2) – 1)
print(L1)

Answer:
[0,9,36,81,4]
[0,9,36,81,4]
[0,9,36,81]

Q.11. Predict the output:
def even(n):
return n%2==0
list1= [1,2,3,4,5,6,7,8,9]
ev= [n for n in list1 if n%2==0]
evp=[n for n in list1 if even(n)]
print(evp)

Answer:
[2,4,6,8]

Q.12.Predict the output.
(i)
b=[[9,6],[4,5],[7,7]]
x=b[:2]
x[1].append(10)
print(x)
Answer:
[[9,6],[4,5],10]]

(ii)
b=[[9,6],[4,5],[7,7]]
x=b[:2]
x[1].append(10)
print(x)
Answer:
[[9,6],[4,5,10]]

Q.13. Find the Error. Consider the following code, which runs correctly at times but gives error at other times. Find the error and its reason.
Lst1=[23,34,12,77,34,26,28,93,48,69,73,23,19,88]
Lst2=[ ]
print(“List1 orginally 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:
print(Lst1.index(100))
print(Lst2.index(100))
elif ch==3:
print(Lst1.pop())
print(lst2.pop())

Answer:
The code will run into error when ch is 2 because till now the value 100 doesnot exist in Lst1 or Lst2 and its index is being asked. Also the code will run into error when ch is 3 because LST2 is empty and nothing can be popped from it.

Q.14.Suggest the correction for the error(s) in previous question’s code.
Answer:

 Lst1=[23,34,12,77,34,26,28,93,48,69,73,23,19,88]
Lst2=[ ]
print("List1 orginally 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(Lsst2.index(100)) 
elif ch==3:
    print(Lst1.pop())
    print(lst2.pop())

Q.15.Find the error. Consider the following code(s) and predict the error(s):
(i) y for y in range(100) if y%2==0 and if y%5==0
Answer:

#if has been used twice
y for y in range(100) if y%2==0 and y%5==0

(ii)( y for y in range(100) if y%2==0 and if y%5==0 )
Answer:
if has been used twice
( y for y in range(100) if y%2==0 and y%5==0 )

Q16. Find the error in the following list comprehension :
[“good” if i <3 : else: “better” for i in range (6)]
Answer:

colons are not required after else
[“good” if i <3 : else: “better” for i in range (6)]

Q17. Suggest corrections for the errors in both the previous questions.
In 15 (i) #if has been used twice
y for y in range(100) if y%2==0 and y%5==0
(ii) Q.15.Find the error. Consider the following code(s) and predict the error(s):
(i) y for y in range(100) if y%2==0 and if y%5==0
Answer:

#if has been used twice
y for y in range(100) if y%2==0 and y%5==0

(ii)( y for y in range(100) if y%2==0 and if y%5==0 )
Answer:
if has been used twice
( y for y in range(100) if y%2==0 and y%5==0 )
16. colons are not required after else
[“good” if i <3 : else: “better” for i in range (6)]

Programming Practice/Knowledge based questions:

Q1. Write a program that uses a function called find_in_list() to check for the position o f the first occurrence of v in the list passed as parameter (1st) or -1 if not found. The header for the function is given below
def find_in_list (1st, v ):
“”” lst – a list
v – a value that may or may not be in the list “””

Answer:
def find_in_list(lst, v): #starting function is define from here
    for i in range(len(lst)): # for loop for calculating
        if lst[i] == v:
            return(i)
        
    return (-1) # if the value v is not found -1 is returned 
O = [1, 2, 3, 4]
print(find_in_list(O, 3)) # 2
print(find_in_list(O, 5)) # -1

Q.2. Implement the following function for a linear list, which find outs and returns the number of unique elements in the list
def unique (Lst):
“””passed parameter 1st is a list of integers.”””
After implementing the above function, test it with following lists and show the output producedby above function along with the reason for that output.
(i) lst=[ ]
(ii)lst=[1,2,3]
(iii)lst=[1,2,2]
(iv)lst=[1,2,2,3,3]
Answer:

def unique(lst):
return len(se(lst))
#(i)
lst= [ ]
print(unique(lst)) #0

ii)
lst=[1,2,3]
print(unique(lst)) #3

iii)
lst=[1,2,2]
print(unique(lst)) #2

iv)
lst=[1,2,2,3,3]
print(unique(lst)) #3

3. Use a list comprehension to create a list, CB4. The comprehension should consist of the cubes of the numbers 1 through 10 only if the cube is evenly divisible by four. Finally, print that list to the console Note that in this case, the cubed num ber should be evenly divisible by 4, not the original number.

Answer:
CB4=[i**3 for i in range(1,11)if (i**3)%4==o]#cubes of numbers whose cubes are divisible by 4
print(CB4)
#output
[8,64,216,512,1000]

4. Take two lists, say for example these two :
a=[1,1,2,3,5,8,13,21,34,55,89]
b=[1,2,3,4,5,6,7,8,9,10,11,12,13]

and write a program that returns a list that contain only the elements that are common between the lists. Make sure your program works on two list of different sizes. Write this in one line of Python using at least one list comprehension. Run the complete program and show output.

Answer:
a=[1,1,2,3,5,8,13,21,34,55,89]
b=[1,2,3,4,5,6,7,8,9,10,11,12,13]   

new_list=[ ]
for i in a :
    if i in b and i not in new_list:
         new_list.append(i)
print(new_list

Q.5. Suppose we have a list V where each element represents the visit dates for a particular patient in the las* month. We want to calculate the highest number of visits made by any patient. Write a function MVisit(Lst) to do this.
A sample list (V) is shown below :

V = [[2, 6], [3, 10], [15], [23], [1, 8, 15, 22, 29], [14]]
For the above given list V, the function MVisit( ) should return the result as [1, 8, 15, 22, 29].

Answer:
V = [[2, 6], [3, 10], [15], [23], [1, 8, 15, 22, 29], [14]]
c = 0 # count the number of visits
hv = None # the visits
for i in V:
if len(i) > c: # compares numbers of visits
    c = len(i)
    hv = i
print(‘Highes number of visits :’, hv)

We are also providing the solutions of all the chapters of Computer Science (Python) in this site. the links of relevant topics are provided below:

Computer Science with Python Class 12 Sumita Arora Solutions – CS Study

Leave a Comment

Your email address will not be published. Required fields are marked *