0% found this document useful (0 votes)
2 views20 pages

Practical Programs

Uploaded by

mm718982
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)
2 views20 pages

Practical Programs

Uploaded by

mm718982
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
You are on page 1/ 20

Practical Programs

1.Stack Manipulation with Student Records

Question:

Write Python functions to perform the following operations on the stack named
students:

(i) Push_high_scorers(SList): Pushes names and roll numbers of students


who scored more than 85 marks.

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

#Default List (SList):

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:

['Ryan Thompson', 109]


['James Wilson', 107]
['David Lee', 105]
['Emily Brown', 104]

****************************************************************************

2. Stack Operations with Course Records

Question:

Write Python functions to perform the following operations on the stack named
courses:

(i) Push_short_courses(CList): Pushes titles and instructors of courses with a


duration of less than 6 months.

(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:

['Network Security', 'Michael Davis']


['Network Security', 'Michael Davis']
['Machine Learning', 'Alice Brown']
['Machine Learning', 'Alice Brown']
['Data Science', 'David Johnson']

****************************************************************************

3. Stack Manipulation with City Records

Question:

Write Python functions to perform the following operations on the stack named
cities:

(i) Push_large_cities(CList): Pushes names and countries of cities with a


population of more than 15000.

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

#Default List (CList):

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']

****************************************************************************

4. Stack Manipulation with Movie Records

Question:
Write Python functions to perform the following operations on the stack named
movies:

(i) Push_top_rated(MList): Pushes titles and directors of movies with a rating


of 8.5 or higher.

(ii) Pop_top_rated(): Pops and displays titles and directors of top-rated


movies. Displays "Stack Empty" when there are no elements in the stack.

#Default List (MList):

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:

['Joker', 'Todd Phillips']


['The Godfather', 'Francis Ford Coppola']
['Parasite', 'Bong Joon-ho']
['Interstellar', 'Christopher Nolan']

****************************************************************************

5. Stack Operations with Employee Records

Question:

Write Python functions to perform the following operations on the stack named
employees:

(i) Push_high_salary(EList): Pushes names and departments of employees


who earn more than 80000.

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

#Default List (EList):

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']

****************************************************************************

6. Stack Manipulation with Sports Records

Question:

Write Python functions to perform the following operations on the stack named
sports:

(i) Push_top_players(SList): Pushes names and sports of players with more


than 50 wins.

(ii) Pop_top_players(): Pops and displays names and sports of top players.
Displays "Stack Empty" when there are no elements in the stack.

#Default List (SList):

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:

['Michael Phelps', 'Swimming']


['Serena Williams', 'Tennis']
['Cristiano Ronaldo', 'Football']
['LeBron James', 'Basketball']

****************************************************************************

7. Stack Manipulation with Product Records

Question:

Write Python functions to perform the following operations on the stack named
products:

(i) Push_expensive_products(PList): Pushes names and categories of


products that cost more than 1000.

(ii) Pop_expensive_products(): Pops and displays the name and category of


the product from the stack. Displays "Stack Empty" when there are no elements
in the stack.

(iii) Peak_expensive_product(): Peeks and displays the name and category of


the top product on the stack without removing it. Displays "Stack Empty" when
there are no elements in the stack.

#Default List (PList):

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']

****************************************************************************

8. Stack Manipulation with Course Records

Question:

Write Python functions to perform the following operations on the stack named
courses:

(i) Push_short_courses(CList): Pushes titles and instructors of courses with a


duration of less than 6 months.

(ii) Pop_short_courses(): Pops and displays titles and instructors of short


courses. 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()

# 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']

****************************************************************************

9. Stack Operations on Workshop Records

Question:

Write Python functions to perform the following operations on the stack named
workshops:

(i) Push_popular_workshops(WList): Pushes titles and facilitators of


workshops with more than 30 participants.
(ii) Pop_popular_workshops(): Pops and displays titles and facilitators of
popular workshops. Displays "Stack Empty" when there are no elements in the
stack.

#Default List (WList):

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:

['Project Management', 'Eva Green']


['Leadership Skills', 'Carol Brown']
['Digital Marketing', 'Alice Johnson']
Stack Empty

****************************************************************************

9. Stack Manipulation with City Records

Write Python functions to perform the following operations on the stack named
cities:

(i) Push_large_cities(CList): Pushes names and countries of cities with a


population of more than 15000.

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

#Default List (CList):

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']

****************************************************************************

10. Stack Operations with Employee Records


Question:

Write Python functions to perform the following operations on the stack named
employees:

(i) Push_high_salary(EList): Pushes names and departments of employees


who earn more than 80000.

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

#Default List (EList):

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']

****************************************************************************

11. Stack Manipulation with Product Records

Question:

Write Python functions to perform the following operations on the stack named
products:

(i) Push_expensive_products(PList): Pushes names and categories of


products that cost more than 1000.

(ii) Pop_expensive_products(): Pops and displays the name and category of


the product from the stack. Displays "Stack Empty" when there are no elements
in the stack.

(iii) Peak_expensive_product(): Peeks and displays the name and category of


the top product on the stack without removing it. Displays "Stack Empty" when
there are no elements in the stack.
#Default List (PList):

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']
****************************************************************************

You might also like