0% found this document useful (0 votes)
7 views8 pages

Python Stack Operations for Various Conditions

The document contains a series of Python functions for stack operations, including pushing and popping elements based on specific conditions such as price, fees, names containing certain letters, and more. Each function is defined to handle different data types and structures, such as dictionaries and lists, and includes examples of usage. The overall focus is on implementing stack behavior through user-defined functions.

Uploaded by

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

Python Stack Operations for Various Conditions

The document contains a series of Python functions for stack operations, including pushing and popping elements based on specific conditions such as price, fees, names containing certain letters, and more. Each function is defined to handle different data types and structures, such as dictionaries and lists, and includes examples of usage. The overall focus is on implementing stack behavior through user-defined functions.

Uploaded by

Sathyaseelan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

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


details of stationary items as {name: price}.
The function should push the names of those items into a stack which have a price
greater than 75.
Also, display the count of elements pushed into the stack.
stack = []
def Push(SItem):
count = 0
for k in SItem:
if SItem[k] > 75:
[Link](k)
count += 1
print("The count of elements in the stack is:", count)
S = {"Pen": 106, "Pencil": 59, "Notebook": 80, "Eraser": 25}
Push(S)
2. Write the following user-defined functions to perform operations on the stack named
Univ:
Push_element() – To push an object (Course_name, Fees, Duration) into the stack if
Fees > 100000.
Pop_element() – To pop and display all elements from the stack. If the stack is
empty, display "Underflow".
Univ = []
def Push_element():
for x in L:
if x[1] > 100000:
[Link](x)

def Pop_element():
if Univ:
while Univ:
print([Link]())
else:
print("Underflow")
L = [["MCA", 200000, 3], ["MBA", 500000, 2], ["BA", 100000, 3]]
Push_element()
Pop_element()

3. Write separate user-defined functions for the following:


🔹 (i) PUSH(N)
This function accepts a list of names N as a parameter.
It pushes only those names into the stack named OnlyA which contain the letter 'A'.
🔹 (ii) POPA(OnlyA)
This function pops each name from the stack OnlyA and displays it.
When the stack is empty, the message "EMPTY" is displayed.
OnlyA = []
def PUSH(N):
for name in N:
if 'A' in name:
[Link](name)
def POPA(OnlyA):
while len(OnlyA) > 0:
print([Link](), end=" ")
else:
print("EMPTY")

N = ['Anu', 'Bhanu', 'Akhil', 'Charles']


PUSH(N)
POPA(OnlyA)

4. Write the following user-defined functions:


🔹 (i) pushEven(N)
This function accepts a list of integers named N as a parameter. It pushes only even numbers
into the stack named EVEN.
🔹 (ii) popEven(EVEN) This function pops and displays each element from the stack EVEN.
If the stack is empty, it displays the message: "Stack Empty".
EVEN = []
def pushEven(N):
for x in N:
if x % 2 == 0:
[Link](x)

def popEven(EVEN):
while EVEN:
print([Link](), end=" ")
else:
print("Stack Empty")

N = [10, 5, 3, 8, 15, 4]
pushEven(N)
popEven(EVEN)

5. Write a function in Python, Push(SItem) where SItem is a dictionary containing the


details of stationery items in the format:
{item_name: price}
The function should:
 Push the names of items into a stack if the price is greater than 75.
 Also display the count of elements pushed into the stack.

stack = []
def Push(SItem):
count = 0
for k in SItem:
if SItem[k] > 75:
[Link](k)
count += 1
print("The count of elements in the stack is:", count)
S = {"Pen": 106, "Pencil": 59, "Notebook": 80, "Eraser": 25}
Push(S)

6. Write user-defined functions to perform stack operations on a stack named Univ.


(i) Push_element()
Push into the stack those course records where Fees > 100000.
(ii) Pop_element()
Pop and display each course from the stack. If the stack is empty, display "Underflow".
Univ = []
def Push_element():
for x in L:
if x[1] > 100000:
[Link](x)

def Pop_element():
if Univ:
while Univ:
print([Link]())
else:
print("Underflow")
L = [["MCA", 200000, 3], ["MBA", 500000, 2], ["BA", 100000, 3]]
Push_element()
Pop_element()

[Link] a list named Nums which contains random integers.

Write the following user defined functions in Python and perform the specified
operations on a stack named BigNums.
(i) PushBig(): It checks every number from the list Nums and pushes all
such numbers which have 5 or more digits into the stack, BigNums.
(ii) PopBig(): It pops the numbers from the stack, BigNums and displays
them. The function should also display “Stack Empty” when there are
no more numbers left in the stack.
For example: If the list Nums contains the following data:
Nums=[213, 10025, 167, 254923, 14, 1297653, 31498, 386, 92765]
Then on execution of PushBig(),the stack BigNums should store:
[10025, 254923, 1297653, 31498, 92765]
And on execution of PopBig(), the following output should be displayed:
92765
31498
1297653
254923
10025
Stack Empty
def PushBig(Nums,BigNums): for N
in Nums:
if len(str(N))>=5:
[Link](N)
def PopBig(BigNums): while
BigNums:
print([Link]()) else:
print(“Stack Empty”)
8. 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.
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

travel = []
def Push_element(NList):
for L in NList:
if L[1] != "India" and L[2] < 3500:
[Link]([L[0], L[1]])
def Pop_element():
while len(travel):
print([Link]())
else:
print("Stack Empty")

9. A list contains the following record of customer:


a. [Customer_name, Room Type]
Write the following user-defined functions to perform given operations on the stack named
' Hotel':
i) Push_Cust () - To Push customers names of those customers who are staying in
Delux' Room Type.
ii) Pop_Cust ()- To Pop the names of customers from the stack and display them. Also,
display "Underflow" when there are no customers in the stack.
For example: If the lists with customer details are as follows:
["siddarth", "Delux"] ["Rahul", "Standard"] ["Jerry", "Delux"]
The stack should contain
Jerry Siddharth
The output should be:
Jerry Siddharth
Underflow

hotel = []
customer = [["Ravi", "Delux"], ["Anu", "Standard"], ["Meena", "Delux"], ["Kiran",
"Suite"]]
def push_cust():
for i in customer:
if i[1] == 'Delux':
[Link](i[0])
return hotel
def pop_cust():
if hotel == []:
return "Underflow"
else:
return [Link]()
# Push Delux customers to hotel stack
push_cust()

# Pop and print each customer until stack is empty


while True:
if hotel == []:
print(pop_cust())
break
else:
print(pop_cust())

10 Write a function in Python, Push (Vehicle) where, Vehicle is a dictionary containing


details of vehicles - {Car_Name: Maker}.
The function should push the name of car manufactured by "TATA' (including all the
possible cases like Tata, TaTa, etc.) to the stack.
For example:
If the dictionary contains the following data:
Vehicle={"Santro" : "Hyundai", "Nexon": "TATA", "Safari" : "Tata"} The
stack should contain Safari Nexon

Vehicle={"Santro" : "Hyundai", "Nexon": "TATA", "Safari" : "Tata"}


stk=[]
def push(vehicle):
for i in vehicle:
if vehicle[i].lower()=='tata':
[Link](i)
return stk

push(Vehicle)
for i in range(-1,-len(stk)-1,-1):
print(stk[i])

11.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

status=[]
def Push_element(cust): if
cust[2]=="Goa":
L1=[cust[0],cust[1]]
[Link](L1)

def Pop_element ():


num=len(status) while
len(status)!=0:
dele=[Link]()
print(dele) num=num-1
else:
print("Stack Empty")

12. 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=[]
def Push(SItem):
count=0
for k in SItem:
if (SItem[k]>=75):
[Link](k)
count=count+1
print("The count of elements in the stack is : ", count)

13. 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:

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 [Link] 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
R={"OM":76, "JAI":45, "BOB":89, "ALI":65, "ANU":90, "TOM":82}
def PUSH(S,N):
[Link](N) def
POP(S):
if S!=[]:
return [Link]() 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

14. 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
N=[12, 13, 34, 56, 21, 79, 98, 22, 35, 38]
def PUSH(S,N):
[Link](N) def
POP(S):
if S!=[]:
return [Link]() else:
return None ST=[]
for k in N:
if k%2==0:
PUSH(ST,k)
while True:
if ST!=[]:
print(POP(ST),end=" ")
else:
break

Write the following user-defined functions in Python:


(i) push_trail(N, myStack)
Here, N and myStack are lists. The function should push the last 5 elements from list N onto
the stack myStack.
📌 Example:
If N = [1, 2, 3, 4, 5, 6, 7],
Then after push_trail(), the stack should contain:
[3, 4, 5, 6, 7]
ii) pop_one(myStack)
The function should pop and return one element from the stack.
If the stack is empty, it should display 'Stack Underflow' and return None.
(iii) display_all(myStack)
The function should display all elements of the stack without removing them.
If the stack is empty, it should display 'Empty Stack'.

def push_trail(N, myStack):


# Push last 5 elements of N into myStack
for i in N[-5:]:
[Link](i)

def pop_one(myStack):
if myStack == []:
print("Stack Underflow")
return None
else:
return [Link]()

def display_all(myStack):
if myStack == []:
print("Empty Stack")
else:
for item in myStack:
print(item, end=' ')
print()
N = [1, 2, 3, 4, 5, 6, 7]
myStack = []

push_trail(N, myStack) # myStack becomes [3, 4, 5, 6, 7]


display_all(myStack) # Output: 3 4 5 6 7

You might also like