0% found this document useful (0 votes)
41 views

Stacks

The document provides examples of questions and answers related to stacks and functions for stack operations. It includes sample code for functions to push and pop elements from a stack, as well as examples applying the stack data structure to problems involving lists, dictionaries and nested lists.

Uploaded by

Advait
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Stacks

The document provides examples of questions and answers related to stacks and functions for stack operations. It includes sample code for functions to push and pop elements from a stack, as well as examples applying the stack data structure to problems involving lists, dictionaries and nested lists.

Uploaded by

Advait
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Stacks

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)

Predict the output


SP23
Q1 Predict the output of the following code: (2)
S= “LOST”
L= [10,21,33,4]
D={ }
for I in range (len (S)):
if I%2 == 0 :
D [L . pop( ) ] = S[ I ]
else :
D [L.pop( ) ] = I+3
for K ,V in D.tems( ) :
print ( K, V, sep = “*”)

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)

Q2 Write a function in python, MakePush(Package) and MakePop(Package) to add


a new Package and delete a Package from a List of Package Description,
considering them to act as push and pop operations of the Stack data structure.(4)
A2
def MakePush(Package):
a=int(input("enter package title : "))
Package.append(a)
def MakePop(Package):
if (Package==[]):
print( "Stack empty")
else:
print ("Deleted element:",Package.pop())
(½ mark for MakePush() header)
( ½ mark for accepting a value from user)
( ½ mark for adding value in list)
( ½ mark for MakePop() 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)

Q4 Write a function in Python POP(Arr), where Arr is a stack implemented by a


list of numbers. The function returns the value deleted from the stack. (3)
A4
def popStack(st) :
# If stack is empty
if len(st)==0:
print("Underflow")
else:
L = len(st)
val=st[L-1]
print(val)
st.pop(L-1)

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

1 mark for correct PUSH operation


1 mark for correct POP operation
1 mark for correct function calls and displaying the output

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”]

The stack should contain


[“Ashmit”,”1010101010”]
[“Gurdas”,”9999999999”]

The output should be:


[“Ashmit”,”1010101010”]
[“Gurdas”,”9999999999”]
Stack Empty
A7
def Push_element(cust):
if cust[2]=="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())

Q8 Write a function in Python, Push(SItem) where , SItem is a dictionary containing


the details of stationary items– {Sname:price}.
The function should push the names of those items in the stack who have price
greater than 75. Also display the count of elements pushed into the stack.
For example:
If the dictionary contains the following data:
Ditem={"Pen":106,"Pencil":59,"Notebook":80,"Eraser":25}
The stack should contain
Notebook
Pen

The output should be:


The count of elements in the stack is 2
stackItem=[]
A8
def Push(SItem):
count=0
for k in SItem:
if (SItem[k]>=75):

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)

For example: If the nested list contains the following data:


NList=[["New York", "U.S.A.", 11734],
["Naypyidaw", "Myanmar", 3219],
["Dubai", "UAE", 2194],
["London", "England", 6693],
["Gangtok", "India", 1580],
["Columbo", "Sri Lanka", 3405]]

The stack should contain:


['Naypyidaw', 'Myanmar'],
['Dubai', 'UAE'],
['Columbo', 'Sri Lanka']
The output should be:
['Columbo', 'Sri Lanka']
['Dubai', 'UAE']
['Naypyidaw', 'Myanmar']
Stack Empty

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)

You might also like