Stacks (FINAL)
Stacks (FINAL)
Sc Practical
STACKS ASSIGNMENT
#1 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.
CODE :
def Push_element(lst):
for i in lst:
if i[2]=='Goa' or i[2]=='GOA':
status.append(i[0])
status.append(i[1])
def Pop_element():
while len(status):
print("The element poped is:",status.pop())
else:
print("Stack empty.")
status=[]
l=[]
n=int(input("Enter the no. of records to be added :"))
print("Enter the record in the format [Customer_name,Phone_number,City]")
for i in range(n):
r=eval(input("Enter the record :"))
l.append(r)
Push_element(l)
print("The stack is:",l)
top=len(status)-1
for j in range(top,-1,-1):
print(status[j])
Pop_element()
OUTPUT:
#2 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.
CODE :
def Push(SItem):
c=0
l=[]
t=SItem.items()
for i in t:
x=list(i)
l.append(x)
for j in l:
if j[1]>75:
Stack.append(j[0])
c=c+1
return c
Stack=[]
d={}
a='y'
while a=='y' or a=='Y':
SName=input("Enter the name of stationary :")
Price=int(input("Enter the price :"))
d[SName]=Price
a=input("Wish to continue :")
count=Push(d)
print("The Stack will be :")
top=len(Stack)-1
for k in range(top,-1,-1):
print(Stack[k])
print("The no. of items having price > 75 is :",count)
OUTPUT:
#3 Vedika has created a dictionary containing names
and marks as key-value pairs of 5 students. Write
a menu driven program, with separate user-defined
functions to perform the following operations:
1. Push the keys (name of the student) of the
dictionary into a stack, where
the corresponding value (marks) is greater
than 70.
2. Pop and display the content of the stack.
3. Display function displays the stack.
CODE :
def Push(Stu):
l=[]
t=Stu.items()
for i in t:
x=list(i)
l.append(x)
for i in l:
if i[1]>70:
stack.append(i[0])
print("The names are successfully pushed to the stack.")
def Pop():
while(len(stack)):
print("The item poped is :",stack.pop())
else:
print("The Stack is now Underflow.")
def Display():
if stack==[]:
print("The Stack is currently empty.")
else:
print("The Stack currently is:")
top=len(stack)-1
for i in range(top,-1,-1):
print(stack[i])
stack = []
d = {}
n=int(input("Enter the no. of records to be added :"))
for i in range(n):
St = input("Enter student Name :")
Mk = int(input("Enter the Marks :"))
d[St] = Mk
print("The dictionary is successfully created.")
print("The dictionary is :",d)
a = 'y'
while a=='y' or a=='Y':
print("1. Push")
print("2. Pop")
print("3. Display")
ch=int(input("Enter your choice :"))
if ch==1:
Push(d)
if ch==2:
Pop()
if ch==3:
Display()
a=input("Wish to continue :")
OUTPUT:
#4 Write functions AddPlayer(player) and
DeletePlayer(player) in python to add and remove a
player by considering them as push and pop
operations in a stack.
CODE :
def AddPlayer(player):
n=int(input("Enter the no. of players to be added :"))
for i in range(n):
p=input("Enter the player name :")
player.append(p)
return player
def DeletePlayer(player):
while len(player):
print("The player removed is :",player.pop())
else:
print("The stack is now underflow.")
l=[]
player=[]
a='y'
while a=='y' or a=='Y':
print("1. AddPlayer.")
print("2. DeletePlayer.")
ch=int(input("Enter your choice :"))
if ch==1:
lst=AddPlayer(l)
print("The stack currently is :")
top=len(lst)-1
for i in range(top,-1,-1):
print(lst[i])
if ch==2:
DeletePlayer(lst)
a=input("Wish to continue :")
OUTPUT:
#5 Write a function to inspect an element from the
stack.
CODE :
def inspect(stk):
print("The top most element of the stack is :")
print(stk[top])
a='y'
while a=='y' or a=='Y':
if len(stk)!=1:
ch=input("Do you want to remove the topmost element (y/n):")
if ch=='y' or ch=='Y':
it = stk.pop()
print("The item poped is :",it)
ln = len(stack)-1
print("Now the stack is:")
for i in range(ln,-1,-1):
print(stack[i])
print("Now the topmost element is :",stk[ln])
else:
ch=input("Do you want to remove the topmost element (y/n):")
if ch=='y' or ch=='Y':
im = stk.pop()
print("The item poped is :",im)
print("The stack is now empty.")
break
stack = eval(input("Enter a stack :"))
top = len(stack)-1
print("The Stack curently is :")
for i in range(top,-1,-1):
print(stack[i])
inspect(stack)
OUTPUT:
#6 Write a python function named is_underflow() to
check a stack is an underflow.
CODE :
def is_underflow(stk):
if stk!=[]:
print("The stack currently is not underflow.")
ch = input("Do you want to make it empty/underflow (y/n):")
if ch=='y'or ch=='Y':
while len(stack):
print("The item poped is :",stack.pop())
else:
print("The stack is now empty/underflow.")
else:
print("The stack is underflow.")
a='y'
while a=='y' or a=='Y':
stack = eval(input("Enter a stack :"))
top = len(stack)-1
print("The stack currently is :")
for i in range(top,-1,-1):
print(stack[i])
is_underflow(stack)
a = input("Wish to continue :")
OUTPUT: