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

Stack Operations in Python Code Examples

The document contains various implementations of stack operations using functions like PUSH, POP, DISPLAY, and PEEK for different data types and conditions. It includes examples related to employee details, temperature handling, and item management, demonstrating how to manipulate stacks in Python. Additionally, it discusses concepts such as CSV files, the LEGB rule, and the distinction between parameters and arguments in functions.
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)
11 views7 pages

Stack Operations in Python Code Examples

The document contains various implementations of stack operations using functions like PUSH, POP, DISPLAY, and PEEK for different data types and conditions. It includes examples related to employee details, temperature handling, and item management, demonstrating how to manipulate stacks in Python. Additionally, it discusses concepts such as CSV files, the LEGB rule, and the distinction between parameters and arguments in functions.
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

STACK

def PUSH(Arr):
for x in Arr:
if x % 5 == 0:
[Link](x)
def DISPLAY(stk):
if stk == []:
print(“Stack is Empty : Underflow ”)
else:
for x in range(len(stk)-1,-1,-1):
print(stk[x])
stk = []
PUSH(Arr)
DISPLAY(stk)

EMP = {“EOP1”:16000 , “EOP2” :28000 , “EOP3”:19000,“EOP4”:15000 , “EOP5”:30000 }


def PUSH(EMP):
for k , v in [Link]():
if v < 25000 :
[Link](k)
def POP(stk):
while len(stk) :
print([Link]( ) , end = “ “)
else:
print(“Stack Empty : Underflow “)
stk = []
PUSH(EMP)
POP(stk)
----------------------------------------------------------
Num = [31,55,76,89,21,45,76,68]
def PUSH(Num):
for val in Num:
if val % 2 == 1:
[Link]( val )
def POP(stk):
while len(stk) :
print([Link]( ) , end = “ “)
else:
print(“Stack Empty : Underflow “)
stk = []
PUSH(Num)
POP(stk)

write a menu driven program using function PUSH(), POP() , DISPLAY() , PEEK() to implement the stack. The
program will store the employee details i.e. Employee number , employee name and salary.
def push(stk):
eno = int(input(“Enter Employee number :”))
ename = input(“Enter Employee Name :”)
sal = int(input(“Enter the salary:”))
lst = [eno,ename,sal]
[Link]( lst )
def pop(stk):
if stk == []:
print(“ Stack is empty : Underflow “)
else:
print([Link]() )

def display(stk):
if stk == []:
print(“ Stack is empty : Underflow “)
else:
for i in range(len(stk)-1,-1,-1):
print(stk[i])
def peek(stk) :
if stk == []:
print(“ Stack is empty : Underflow “)
else:
top = len(stk) – 1
print(stk[top])
stk = []
while True:
print(“1. Push”)
print(“2. Pop”)
print(“3. Display”)
print(“4. Peek”)
print(“5. Exit”)
ch = int(input(“Enter your choice:”))
if ch == 1 :
push(stk)
elif ch == 2:
pop(stk)
elif ch == 3:
display(stk)
elif ch == 4:
peek(stk)
else:
break

Aruna has a list containing temperatures of 10 cities. You need to help her 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 negative temperatures into a stack.
● Pop and display the content of the stack.
For Example:
If the sample Content of the list is as follows:
T=[-9, 3, 31, -6, 12, 19, -2, 15, -5, 38]
Sample Output of the code should be:
-5 -2 -6 -9
ANSWER :
T = [-9, 3, 31, -6, 12, 19, -2, 15, -5, 38]
def PUSH(T):
for val in T:
if val < 0:
[Link]( val )
def POP(stk):
while len(stk) :
print([Link]( ) , end = “ “)
else:
print(“Stack Empty : Underflow “)
stk =[]
Push(T)
Pop(stk)
-----------------------------------------------------------------------------------------------
W = [ ‘Elucidate’ , ‘Haughty’ , ‘Pacify’, ‘Quip’ , ‘Rapport’ ,’Urbane’, ’Young’, ’Zenith’ ]
def PUSH(W):
for val in W :
if len(val) < 7:
[Link]( val )
def POP(stk):
while len(stk) :
print([Link]( ) , end = “ “)
else:
print(“Stack Empty : Underflow “)
stk =[]
Push(W)
Pop(stk)
----------------------------------------------------------------------------------------------------------

Answer :

def pushElement():
for d in student :
if d[2]==“XII” and d[3]== “A” :
k = [ d[0] , d[1] ]
[Link] (k)
def popElement() :
while len(xiia) :
print( [Link]() )
else :
print(“Stack Empty”)
pushElement()
popElement()
Ditem = { “Pen”:106 , “Pencil”:59 ,”Notebook”:80 , “Eraser”:25 }
stk = [ ]
def Push(Sitem):
count = 0
for k,v in [Link]() :
if ( v > 75 ):
[Link]( k )
count = count + 1
print( “The count of elements in the stack is :”, count )
Push(Ditem)
----------------------------------------------------------
def PushNV(N):
for word in N:
for letter in Word:
if letter in “aeiouAEIOU” :
break
else:
[Link](word)
def PopNV(NoVowel) :
while len(NoVowel) :
print([Link]( ) , end = “ “)
else:
print(“Empty Stack “)
All = []
NoVowel = []
for i in range(5):
k = input(“Enter a Word :”)
[Link](k)
PushNV(All)
Pop (NoVowel)
------------------------------------------------
def Push3_5(N):
for val in N:
if val % 3 == 0 or val % 5 == 0 :
Only3_5.append(val)
def Pop3_5(Only3_5) :
while len(Only3_5) :
print(Only3_5.pop( ) , end = “ “)
else:
print(“Stack Empty“)
NUM = []
Only3_5 = []
for i in range(5):
k = int(input(“Enter an Integer :”))
[Link](k)
Push3_5(NUM)
Pop3_5(Only3_5)
---------------------------------------------------------------------------------------------------------
stu_stk =[]
def push_elements(stu_stk , stu_dict) :
for k,v in stu_dict.items() :
if v[2] >= 80 :
stu_stk.append(k)
def Pop_elements(stu_stk):
while len(stu_stk) :
print(stu_stk.pop())
else:
print(“Stack Empty”)

------------------------------------------
def MakePush(Package) :
a = int(input(“Enter package title = “))
[Link](a)
def MakePop(Package) :
while len(package) :
print( [Link]() )
else :
print(“Stack Empty”)
package = []
MakePush(Package)
MakePop(Package)
----------------------------------------------------------------------------------------------------------

sitem = [ [‘pen’,120.00,’pcs’,’reynolds’,132.00] , [‘paper’,345.00,’rim’,’camel’,500.00] ,


[‘Eraser’,100.00,’box’,’IBP’,110.00] ]
stack = []
def Push(stack,sitem) :
for rec in sitem :
if rec[1] == rec[4]*0.90 :
[Link](rec[0])
def print_stack() :
for a in range(len(stack)-1,-1,-1):
print(stack[a])
print(“The count of elements in stack =” , len(stack))
push(stack,sitem)
print_stack()
----------------------------------------------------------------------------------------------------------------------
visitors = [ [305,”10/11/2022”,”geetha”,”F”,35] , [306,”10/11/2022”,”arham”,”M”,15],
307,”11/11/2022”,”david”,”M”,18], [308,”11/11/2022”,”madhuri”,”F”,17], [309,”11/11/2022”,”sikandar”,”M”,13] ]
status = []
def push_element(visitors) :
for rec in visitors :
if rec[4] >= 15 and rec[4] <= 20 :
[Link]( rec[3] )
def pop_element() :
mc = 0
fc = 0
while len(status):
a = [Link]()
if a == “M” :
mc = mc + 1
elif a == “F” :
fc = fc + 1
else :
print(“Done”)
print(“Male = ”, mc)
print(“Female =”, fc)
push_element(visitors)
pop_element()
------------------------------------------------------------------------------------------
expression = “42*5.8*16/24-8+2”
st = []
def Push(st,expression) :
for a in expression :
if a in “+-*/” :
[Link](a )
def Pop(st):
while len(st) :
print([Link](), end = “ “)
else:
print(“Stack Empty”)
push(st,expression)
pop(st)
------------------------------------------------------------------
Kitem = {“spoons”:116 , “knife”:50,
“Plates”:180 , “Glass”: 60 }
def Push(Kitem) :
total = 0
for k,v in [Link]() :
if v < 100 :
[Link](k)
total = total + v
print (“The average price of an item = “, total / len(stk) )
Push(Kitem)
-------------------------------------------------------------------------------------------------------------------------------------------
status = []
def push_element() :
rno = int(input(“Enter Roll = “))
name = input(“Name = “)
dob = input(“DOB =”)
class = int(input(“class =”))
rec = [rno,name,dob,class]
[Link](rec)
def pop_element() :
while len(status) :
print([Link]())
else:
print(“Stack Empty”)
push_element()
pop_element()
-----------------------------------------------
Lname = [ ‘narender’, ‘jaya’ , ‘raju’ , ‘ramesh’ , ‘amit’ , ‘piyush’ ]
Lage = [ 45,23,59,34,51,43 ]
Lnameage = []
def Push_na() :
for i in range (len(Lage)):
if Lage[i] > 50 :
[Link]( (Lname[i] , Lage[i]) )
def Pop_na() :
while len(Lnameage) :
t = [Link]()
print( “The name removed is “, t[0] )
print( “The age of person is” , t[1] )
else :
print(“underflow”)
Push_na()
Pop_na()
-------------------------------------------------------------
Advantages of CSV File :
 Easier to create.
 Preferred export and import format for databases and spreadsheets.
 Capable of storing large amounts of data.
LEGB Rule :
Python resolves the scope of a name using LEGB rule.
(i.e.) it checks environments in the order : Local , Enclosing , Global and Built-in.
Arguments : The values being passed through a function-call statement are called arguments (or actual
arguments or actual parameters).
Parameters : The values received in the function header are called parameters (or formal parameters or
formal arguments).
Positional Arguments/Parameters : Arguments passed to a function in correct positional order.
Default Parameter : A default argument is an argument that assumes a default value if a value is not provided
in the function call for that argument.
Keyword Arguments : Value can be provided by using their name instead of the position (order) in function
call statement. These are called keyword arguments.
Function: A function is a group of statements that exists within a program for the purpose of performing a
specific task.
Difference between Parameter and Argument : Parameters are temporary variable names within functions. The
argument can be thought of as the value that is assigned to that temporary variable.
Example :To calculate sum of two numbers.
def sum(a,b):
return a+b
sum(10,20)
Here a, b are the parameters for the function ‘sum’. 10, 20 are the arguments for the function ‘sum’.

You might also like