0% found this document useful (0 votes)
355 views7 pages

STACK Worksheet

Uploaded by

workshopkv2rke
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)
355 views7 pages

STACK Worksheet

Uploaded by

workshopkv2rke
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/ 7

Q1

A list contains following record of a student:


[StudentName, Class, Section, MobileNumber]
Write the following user defined functions to perform given operations on the stack named ‘xiia’:
(i) pushElement() - To Push an object containing name and mobile number of students who
belong to class xii and section ‘a’ to the stack
(ii) popElement() - 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 students details are:
[“Rajveer”, “99999999999”,”XI”, “B”]
[“Swatantra”, “8888888888”,”XII”, “A”]
[“Sajal”,”77777777777”,”VIII”,”A”]
[“Yash”, “1010101010”,”XII”,”A”]
The stack “xiia” should contain
[“Swatantra”, “8888888888”]
[“Yash”, “1010101010”]
The output should be:
[“Yash”, “1010101010”]
[“Swatantra”, “8888888888”]
Stack Empty

ANS:

xiia=[]
student=[['Rajveer', '99999999999','XI', 'B'],['Swatantra', '8888888888','XII', 'A'],
['Sajal','77777777777','VIII','A'],['Yash', '1010101010','XII','A']]
def pushElement(student):
for d in student:
if d[2]=='XII' and d[3]=='A':
xiia.append([d[0],d[1]])
def popElement():
while len(xiia)!=0:
print(xiia.pop())
else:
print('Stack Empty')
pushElement(student)
print(xiia)
popElement()
(1.5 marks for correct pushElement() and 1.5 marks for correct popElement())

Q2
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 25.
Also display the count of elements pushed into the stack.
For example:
If the dictionary contains the following data:
Ditem = {“Rubber”:5, "Pencil":5, "Pen":30, "Notebook": 60, "Eraser":5, “Watch”: 250}
The stack should contain
Pen
Notebook
Watch
The output should be:
The count of elements in the stack is 3

ANS:
stackItem=[]
def Push(SItem):
count=0
for k in SItem:
if (SItem[k]>=25):
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)

Q3
A dictionary contains the names of some cities and their population in crore.
Write a python function push(stack, data), that accepts an empty list, which
is the stack and data, which is the dictionary and pushes the names of those
countries onto the stack whose population is greater than 25 crores.
For example :
The data is having the contents {'India':140, 'USA':50, 'Russia':25, 'Japan':10}
then the execution of the function push() should push India and USA on the
stack.

ANS:
data={'India':140, 'USA':50, 'Russia':25, 'Japan':10}
stack=[]
def push(stack, data):
for x in data:
if data[x]>25:
stack.append(x)
push(stack, data)
print(stack)
½ mark should be deducted for all incorrect syntax. Full marks to be
awarded for any other logic that produces the correct result.

Q4.
A list of numbers is used to populate the contents of a stack using a function
push(stack, data) where stack is an empty list and data is the list of numbers.
The function should push all the numbers that are even to the stack. Also
write the function pop() that removes the top element of the stack on its
each call.

Ans:
data = [1,2,3,4,5,6,7,8]
stack = []
def push(stack, data):
for x in data:
if x % 2 == 0:
stack.append(x)
def pop(stack):
if len(stack)==0:
return "stack empty"
else:
return stack.pop()
push(stack, data)
print(pop(stack))
½ mark should be deducted for all incorrect syntax. Full marks to be
awarded for any other logic that produces the correct result.

Q5
Write a program to perform push operations on a Stack containing Student details as given in
the
following definition of student node:
RNo integer
Name String
Age integer
def isEmpty(stk):
if stk == [ ]:
return True
else:
return False
def stk_push(stk, item):
# Write the code to push student details using stack.
Ans:
def stkpush(stk, item):
stk.append(item)
top=len(stk)-1

Q6
Write a program to perform pop operations on a Stack containing Student details as given in the
following definition of student node:
RNo integer
Name String
Age integer
def isEmpty(stk):
if stk == [ ]:
return True
else:
return False
def stk_pop(stk):
# Write the code to pop a student using stack

ANS:
def stkpop(stk):
if isEmpty( ):
print(“Underflow”)

Q7
Q8
Ans:

You might also like