Practical Programs
Practical Programs
Question:
Write Python functions to perform the following operations on the stack named
students:
(ii) Pop_high_scorers(): Pops and displays names and roll numbers of high
scorers. Displays "Stack Empty" when there are no elements in the stack.
SList = [
["John Doe", 101, 85],
["Jane Smith", 102, 90],
["Michael Johnson", 103, 78],
["Emily Brown", 104, 92],
["David Lee", 105, 88],
["Sarah Clark", 106, 82],
["James Wilson", 107, 95],
["Emma Martinez", 108, 79],
["Ryan Thompson", 109, 87],
]
#Solution:
def Push_high_scorers(SList):
stack = []
for record in SList:
if record[2] > 85:
stack.append([record[0], record[1]])
return stack
def Pop_high_scorers(stack):
if not stack:
return "Stack Empty"
else:
return stack.pop()
# Example usage
students = Push_high_scorers(SList)
print(Pop_high_scorers(students))
print(Pop_high_scorers(students))
print(Pop_high_scorers(students))
print(Pop_high_scorers(students))
Expected output:
****************************************************************************
Question:
Write Python functions to perform the following operations on the stack named
courses:
(ii) Pop_short_courses(): Pops and displays the title and instructor of the
course from the stack. Displays "Stack Empty" when there are no elements in
the stack.
(iii) Peak_short_course(): Peeks and displays the title and instructor of the top
course on the stack without removing it. Displays "Stack Empty" when there are
no elements in the stack.
#Default List (CList):
CList = [
["Python for Beginners", "John Doe", 3],
["Advanced Java", "Jane Smith", 6],
["Data Science", "David Johnson", 4],
["Machine Learning", "Alice Brown", 5],
["Web Development", "Emily White", 6],
["Network Security", "Michael Davis", 2],
]
#Solution:
def Push_short_courses(CList):
stack = []
for record in CList:
if record[2] < 6:
stack.append([record[0], record[1]])
return stack
def Pop_short_courses(stack):
if not stack:
return "Stack Empty"
else:
return stack.pop()
def Peak_short_course(stack):
if not stack:
return "Stack Empty"
else:
return stack[-1]
# Example usage
courses = Push_short_courses(CList)
print(Peak_short_course(courses))
print(Pop_short_courses(courses))
print(Peak_short_course(courses))
print(Pop_short_courses(courses))
print(Peak_short_course(courses))
Expected Output:
****************************************************************************
Question:
Write Python functions to perform the following operations on the stack named
cities:
(ii) Pop_large_city(): Pops and displays the name and country of the city from
the stack. Displays "Stack Empty" when there are no elements in the stack.
(iii) Peak_large_city(): Peeks and displays the name and country of the top
city on the stack without removing it. Displays "Stack Empty" when there are no
elements in the stack.
CList = [
["New York", "USA", 20000],
["Tokyo", "Japan", 15000],
["Mumbai", "India", 18000],
["London", "UK", 17000],
["Beijing", "China", 19000],
["Sydney", "Australia", 14000],
]
#Solution:
def Push_large_cities(CList):
stack = []
for record in CList:
if record[2] > 15000:
stack.append([record[0], record[1]])
return stack
def Pop_large_city(stack):
if not stack:
return "Stack Empty"
else:
return stack.pop()
def Peak_large_city(stack):
if not stack:
return "Stack Empty"
else:
return stack[-1]
# Example usage
cities = Push_large_cities(CList)
print(Peak_large_city(cities))
print(Pop_large_city(cities))
print(Peak_large_city(cities))
print(Pop_large_city(cities))
print(Peak_large_city(cities))
Expected Output:
['Beijing', 'China']
['Beijing', 'China']
['London', 'UK']
['London', 'UK']
['Mumbai', 'India']
****************************************************************************
Question:
Write Python functions to perform the following operations on the stack named
movies:
MList = [
["Inception", "Christopher Nolan", 8.8],
["The Dark Knight", "Christopher Nolan", 9.0],
["Interstellar", "Christopher Nolan", 8.6],
["Parasite", "Bong Joon-ho", 8.6],
["Titanic", "James Cameron", 7.8],
["The Godfather", "Francis Ford Coppola", 9.2],
["Joker", "Todd Phillips", 8.5],
]
#Solution:
def Push_top_rated(MList):
stack = []
for record in MList:
if record[2] >= 8.5:
stack.append([record[0], record[1]])
return stack
def Pop_top_rated(stack):
if not stack:
return "Stack Empty"
else:
return stack.pop()
# Example usage
movies = Push_top_rated(MList)
print(Pop_top_rated(movies))
print(Pop_top_rated(movies))
print(Pop_top_rated(movies))
print(Pop_top_rated(movies))
Expected Output:
****************************************************************************
Question:
Write Python functions to perform the following operations on the stack named
employees:
(ii) Pop_high_salary(): Pops and displays the name and department of the
employee from the stack. Displays "Stack Empty" when there are no elements in
the stack.
(iii) Peak_high_salary(): Peeks and displays the name and department of the
top employee on the stack without removing it. Displays "Stack Empty" when
there are no elements in the stack.
EList = [
["Alice Johnson", "HR", 80000],
["Bob Williams", "Engineering", 90000],
["Carol Brown", "Finance", 75000],
["David Davis", "Marketing", 85000],
["Eva Green", "IT", 95000],
["Frank White", "Sales", 70000],
["Gina Miller", "Operations", 82000],
]
#Solution:
def Push_high_salary(EList):
stack = []
for record in EList:
if record[2] > 80000:
stack.append([record[0], record[1]])
return stack
def Pop_high_salary(stack):
if not stack:
return "Stack Empty"
else:
return stack.pop()
def Peak_high_salary(stack):
if not stack:
return "Stack Empty"
else:
return stack[-1]
# Example usage
employees = Push_high_salary(EList)
print(Peak_high_salary(employees))
print(Pop_high_salary(employees))
print(Peak_high_salary(employees))
print(Pop_high_salary(employees))
print(Peak_high_salary(employees))
Expected Output:
****************************************************************************
Question:
Write Python functions to perform the following operations on the stack named
sports:
(ii) Pop_top_players(): Pops and displays names and sports of top players.
Displays "Stack Empty" when there are no elements in the stack.
SList = [
["Roger Federer", "Tennis", 103],
["Lionel Messi", "Football", 45],
["LeBron James", "Basketball", 89],
["Cristiano Ronaldo", "Football", 60],
["Serena Williams", "Tennis", 73],
["Michael Phelps", "Swimming", 82],
]
#Solution:
def Push_top_players(SList):
stack = []
for record in SList:
if record[2] > 50:
stack.append([record[0], record[1]])
return stack
def Pop_top_players(stack):
if not stack:
return "Stack Empty"
else:
return stack.pop()
# Example usage
sports = Push_top_players(SList)
print(Pop_top_players(sports))
print(Pop_top_players(sports))
print(Pop_top_players(sports))
print(Pop_top_players(sports))
Expected Output:
****************************************************************************
Question:
Write Python functions to perform the following operations on the stack named
products:
PList = [
["Laptop", "Electronics", 1200],
["Smartphone", "Electronics", 800],
["Refrigerator", "Home Appliances", 1500],
["Television", "Electronics", 900],
["Washing Machine", "Home Appliances", 700],
["Microwave Oven", "Kitchen Appliances", 400],
["Air Conditioner", "Home Appliances", 1100],
]
#Solution:
def Push_expensive_products(PList):
stack = []
for record in PList:
if record[2] > 1000:
stack.append([record[0], record[1]])
return stack
def Pop_expensive_products(stack):
if not stack:
return "Stack Empty"
else:
return stack.pop()
def Peak_expensive_product(stack):
if not stack:
return "Stack Empty"
else:
return stack[-1]
# Example usage
products = Push_expensive_products(PList)
print(Peak_expensive_product(products))
print(Pop_expensive_products(products))
print(Peak_expensive_product(products))
print(Pop_expensive_products(products))
print(Peak_expensive_product(products))
Expected output:
['Air Conditioner', 'Home Appliances']
['Air Conditioner', 'Home Appliances']
['Refrigerator', 'Home Appliances']
['Refrigerator', 'Home Appliances']
['Laptop', 'Electronics']
****************************************************************************
Question:
Write Python functions to perform the following operations on the stack named
courses:
CList = [
["Python for Beginners", "John Doe", 3],
["Advanced Java", "Jane Smith", 6],
["Data Science", "David Johnson", 4],
["Machine Learning", "Alice Brown", 5],
["Web Development", "Emily White", 6],
["Network Security", "Michael Davis", 2],
]
#Solution:
def Push_short_courses(CList):
stack = []
for record in CList:
if record[2] < 6:
stack.append([record[0], record[1]])
return stack
def Pop_short_courses(stack):
if not stack:
return "Stack Empty"
else:
return stack.pop()
# Example usage
courses = Push_short_courses(CList)
print(Pop_short_courses(courses))
print(Pop_short_courses(courses))
print(Pop_short_courses(courses))
print(Pop_short_courses(courses))
Expected Output:
['Network Security', 'Michael Davis']
['Machine Learning', 'Alice Brown']
['Data Science', 'David Johnson']
['Python for Beginners', 'John Doe']
****************************************************************************
Question:
Write Python functions to perform the following operations on the stack named
workshops:
WList = [
["Digital Marketing", "Alice Johnson", 35],
["Python Programming", "Bob Williams", 25],
["Leadership Skills", "Carol Brown", 40],
["Data Analysis", "David Davis", 30],
["Project Management", "Eva Green", 45],
["Public Speaking", "Frank White", 20],
]
#Solution:
def Push_popular_workshops(WList):
stack = []
for record in WList:
if record[2] > 30:
stack.append([record[0], record[1]])
return stack
def Pop_popular_workshops(stack):
if not stack:
return "Stack Empty"
else:
return stack.pop()
# Example usage
workshops = Push_popular_workshops(WList)
print(Pop_popular_workshops(workshops))
print(Pop_popular_workshops(workshops))
print(Pop_popular_workshops(workshops))
print(Pop_popular_workshops(workshops))
Expected Output:
****************************************************************************
Write Python functions to perform the following operations on the stack named
cities:
(ii) Pop_large_city(): Pops and displays the name and country of the city from
the stack. Displays "Stack Empty" when there are no elements in the stack.
(iii) Peak_large_city(): Peeks and displays the name and country of the top
city on the stack without removing it. Displays "Stack Empty" when there are no
elements in the stack.
CList = [
["New York", "USA", 20000],
["Tokyo", "Japan", 15000],
["Mumbai", "India", 18000],
["London", "UK", 17000],
["Beijing", "China", 19000],
["Sydney", "Australia", 14000],
]
#Solution:
def Push_large_cities(CList):
stack = []
for record in CList:
if record[2] > 15000:
stack.append([record[0], record[1]])
return stack
def Pop_large_city(stack):
if not stack:
return "Stack Empty"
else:
return stack.pop()
def Peak_large_city(stack):
if not stack:
return "Stack Empty"
else:
return stack[-1]
# Example usage
cities = Push_large_cities(CList)
print(Peak_large_city(cities))
print(Pop_large_city(cities))
print(Peak_large_city(cities))
print(Pop_large_city(cities))
print(Peak_large_city(cities))
Expected Output:
['Beijing', 'China']
['Beijing', 'China']
['London', 'UK']
['London', 'UK']
['Mumbai', 'India']
****************************************************************************
Write Python functions to perform the following operations on the stack named
employees:
(ii) Pop_high_salary(): Pops and displays the name and department of the
employee from the stack. Displays "Stack Empty" when there are no elements in
the stack.
(iii) Peak_high_salary(): Peeks and displays the name and department of the
top employee on the stack without removing it. Displays "Stack Empty" when
there are no elements in the stack.
EList = [
["Alice Johnson", "HR", 80000],
["Bob Williams", "Engineering", 90000],
["Carol Brown", "Finance", 75000],
["David Davis", "Marketing", 85000],
["Eva Green", "IT", 95000],
["Frank White", "Sales", 70000],
["Gina Miller", "Operations", 82000],
]
#Solution:
def Push_high_salary(EList):
stack = []
for record in EList:
if record[2] > 80000:
stack.append([record[0], record[1]])
return stack
def Pop_high_salary(stack):
if not stack:
return "Stack Empty"
else:
return stack.pop()
def Peak_high_salary(stack):
if not stack:
return "Stack Empty"
else:
return stack[-1]
# Example usage
employees = Push_high_salary(EList)
print(Peak_high_salary(employees))
print(Pop_high_salary(employees))
print(Peak_high_salary(employees))
print(Pop_high_salary(employees))
print(Peak_high_salary(employees))
Expected Output:
['Gina Miller', 'Operations']
['Gina Miller', 'Operations']
['Eva Green', 'IT']
['Eva Green', 'IT']
['David Davis', 'Marketing']
****************************************************************************
Question:
Write Python functions to perform the following operations on the stack named
products:
PList = [
["Laptop", "Electronics", 1200],
["Smartphone", "Electronics", 800],
["Refrigerator", "Home Appliances", 1500],
["Television", "Electronics", 900],
["Washing Machine", "Home Appliances", 700],
["Microwave Oven", "Kitchen Appliances", 400],
["Air Conditioner", "Home Appliances", 1100],
]
#Solution:
def Push_expensive_products(PList):
stack = []
for record in PList:
if record[2] > 1000:
stack.append([record[0], record[1]])
return stack
def Pop_expensive_products(stack):
if not stack:
return "Stack Empty"
else:
return stack.pop()
def Peak_expensive_product(stack):
if not stack:
return "Stack Empty"
else:
return stack[-1]
# Example usage
products = Push_expensive_products(PList)
print(Peak_expensive_product(products))
print(Pop_expensive_products(products))
print(Peak_expensive_product(products))
print(Pop_expensive_products(products))
print(Peak_expensive_product(products))
Expected output: