Professional Documents
Culture Documents
SPTerm2
Q1Give any two characteristics of stacks. (2)
A1 Characteristics of Stacks:
-It is a LIFO data structure
-The insertion and deletion happens at one end i.e. from the top of the stack (2)
A1
4*L
33*4
21*S
10*6 (2)
Write a function
SP19 new
Q1 Write a function in Python, INSERTS(Arr,data) and DELETES(Arr) for performing
insertion and deletion operations in a Stack. Arr is the list used for implementing
stack and data is the value to be inserted.
A1
def INSERTQ(Arr):
data=int(input("enter data to be inserted: "))
Arr.append(data)
def DELETEQ(Arr):
1
if (Arr==[]):
print( "Stack empty")
else:
print ("Deleted element is: ",Arr[0])
del(Arr[0])
( ½ mark insert header)
( ½ mark for accepting a value from user)
( ½ mark for adding value in list)
( ½ mark for delete header)
( ½ mark for checking empty list condition)
( ½ mark for displaying “Stack empty”)
( ½ mark for displaying the value to be deleted)
( ½ mark for deleting value from list)
SP20
Q3 Write a function in Python PUSH(Arr), where Arr is a list of numbers. From
this list push all numbers divisible by 5 into a stack implemented by using a
list. Display the stack if it has at least one element, otherwise display
appropriate error message.
2
A3
def PUSH(Arr,value):
s=[]
for x in range(0,len(Arr)):
if Arr[x]%5==0:
s.append(Arr[x])
if len(s)==0:
print("Empty Stack")
else:
print(s)
SP Term2
Q5 Julie has created a dictionary containing names and marks as key value pairs of
6 students. Write a program, with separate user defined functions to perform the
following operations: (3)
● Push the keys (name of the student) of the dictionary into a stack, where the
corresponding value (marks) is greater than 75.
● Pop and display the content of the stack.
For example:
If the sample content of the dictionary is as follows:
R={"OM":76, "JAI":45, "BOB":89, "ALI":65, "ANU":90, "TOM":82}
The output from the program should be:
TOM ANU BOB OM
A5
R={"OM":76, "JAI":45, "BOB":89, "ALI":65, "ANU":90, "TOM":82}
def PUSH(S,N):
3
S.append(N)
def POP(S):
if S!=[ ]:
return S.pop()
else:
return None
ST=[ ]
for k in R:
if R[k]>=75:
PUSH(ST,k)
while True:
if ST!=[ ]:
print(POP(ST),end=" ")
else:
break
Q6 Alam has a list containing 10 integers. You need to help him create a program
with separate user defined functions to perform the following operations based on
this list.
● Traverse the content of the list and push the even numbers into a stack.
● Pop and display the content of the stack.
For Example:
If the sample Content of the list is as follows:
N=[12, 13, 34, 56, 21, 79, 98, 22, 35, 38]
Sample Output of the code should be:
38 22 98 56 34 12
A6
N=[12, 13, 34, 56, 21, 79, 98, 22, 35, 38]
def PUSH(S,N):
S.append(N)
def POP(S):
if S!=[ ]:
return S.pop()
else:
return None
ST=[ ]
for k in N:
if k%2==0:
PUSH(ST,k)
while True:
if ST!=[ ]:
4
print(POP(ST),end=" ")
else:
break
SP22
Q7 A list contains following record of a customer:
[Customer_name, Phone_number, City]
Write the following user defined functions to perform given operations on the stack
named ‘status’:
(i) Push_element() - To Push an object containing name and Phone number of
customers who live in Goa to the stack
(ii) Pop_element() - To Pop the objects from the stack and display them. Also, display
“Stack Empty” when there are no elements in the stack.
For example:
If the lists of customer details are:
[“Gurdas”, “99999999999”,”Goa”]
[“Julee”, “8888888888”,”Mumbai”] [“Murugan”,”77777777777”,”Cochin”]
[“Ashmit”, “1010101010”,”Goa”]
5
L1=[cust[0],cust[1]]
status.append(L1)
def Pop_element ():
num=len(status)
while len(status)!=0:
dele=status.pop()
print(dele)
num=num-1
else:
print("Stack Empty")
(1.5 marks for correct push_element() and 1.5 marks for correct pop_element())
6
stackItem.append(k)
count=count+1
print("The count of elements in the stack is : ", count)
(1 mark for correct function header
1 mark for correct loop
½ mark for correct If statement
½ mark for correct display of count)
SP23
Q9 A list, NList contains following record as list elements:
[City, Country, distance from Delhi]
Each of these records are nested together to form a nested list. Write the following
user defined functions in Python to perform the specified operations on the stack
named travel.
(i) Push_element(NList): It takes the nested list as an argument and pushes a list
object containing name of the city and country, which are not in India and distance is
less than 3500 km from Delhi.
(ii) Pop_element(): It pops the objects from the stack and displays them. Also, the
function should display “Stack Empty” when there are no elements in the stack. (3)
A9
travel [ ]
def Push_element (NList):
for L in Nlist:
7
if L[1]! = “India” and L[2] <3500:
travel.append( [L[0], L[1] ] )
def Pop_element( ) :
whjle len(travel) :
print (travel.pop( ) )
else:
print (“Stack Empty”) (3)