KURINJI SENIOR SECONDARY SCHOOL, NAMAKKAL
Grade: 12 Computer Science
Stack Implementation – Worksheet 1
I .Answer the following:
1. A dictionary contains the names of some cities and their population in crore. Write a python
function push(stack, data), that accepts an empty list, which is the stack and data, which is the
dictionary and pushes the names of those countries onto the stack whose population is greater than
25 crores.
For example : The data is having the contents
{'India':140, 'USA':50, 'Russia':25, 'Japan':10} then the execution of the function push() should
push India and USA on the stack.
2. Write a function in Python PUSH(Num), where Num is a list of numbers. From this list push all
numbers divisible by 5 into a stack implemented by using a list. Display the stack if it has atleast
one element, otherwise display appropriate error message.
For example: If the list Num is: [66, 75, 40, 32, 10, 54]
The stack should contain: [75, 40, 10]
3. 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:
[“Ashok”, “9999999999”,”Goa”]
[“Avinash”, “8888888888”,”Mumbai”]
[“Mahesh”,”77777777777”,”Cochin”]
[“Rakesh”, “66666666666”,”Goa”]
The stack should contain:
[“Rakesh”,”66666666666”]
[“Ashok”,” 99999999999”]
The output should be:
[“Rakesh”,”66666666666”]
[“Ashok”,”99999999999”]
Stack Empty
4. Pramod has created a dictionary containing EMPCODE and SALARY as key value pairs of 5
Employees of Parthivi Constructions.
Write a program, with separate user defined functions to perform the following operations:
● Push the keys (Employee code) of the dictionary into a stack, where the corresponding value
(Salary) is less than 25000.
● Pop and display the content of the stack.
For example: If the sample content of the dictionary is as follows:
EMP={"EOP1":16000, "EOP2":28000, "EOP3":19000, "EOP4":15000, EOP5":30000}
The output from the program should be: EOP4 EOP3 EOP1
5. Write a function in python named PUSH(STACK, SET) where STACK is list of some numbers
forming a stack and SET is a list of some numbers. The function will push all the EVEN elements
from the SET into a STACK implemented by using a list. Display the stack after push operation
6. Write a function in Python PUSH(Arr), where Arr is a list of numbers. From this list push all
numbers divisible by 5 into a stack implemented by using a list. Display the stack if it has at least one
element, otherwise display appropriate error message.
7. Write a function in Python, Push(BItem) where , BItem is a dictionary containing the details of
bakery items–{Bname:price}.
The function should push the names of those items in the stack,S who have price less than 50.
For example:
If the dictionary contains the following data:
Bitem={"Bread":40,"Cake":250,"Muffins":80,"Biscuits":25}
The stack should contain
Bread
Biscuits
8. A list contains following record of a customer:
[Customer_name, Phone_number, City]
Write the following user defined functions to perform given operationson 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
9. A dictionary contains records of a tourist place like :
Tour_dict = { ‘Name’: ‘Goa’ , ‘PeakSeason’ : ‘December’ , ‘Budget’ :
15000 ,‘Famous’:’Beaches’}
Write the following user defined functions to perform given operations on the stack named‘tour’:
(i) Push_tour(Tour_dict) – To Push a list containing values for Name and PeakSeason where value
of budget is less than 10000 into the tour stack
(ii) Pop_tour() – To Pop the list objects from the stack and display them. Also, display “No more
tours” when there are no elements in the stack.
For example if the following dictionaries are passed to Push_tour( ) function in the following
sequence:
{ ‘Name’: ‘Goa’ , ‘PeakSeason’ : ‘December’ , ‘Budget’ : 15000 , ‘Famous’:’Beaches’}
{ ‘Name’: ‘Nainital’ , ‘PeakSeason’ : ‘May’ , ‘Budget’ : 9000 , ‘Famous’:’Nature’}
{ ‘Name’: ‘Sikkim’ , ‘PeakSeason’ : ‘May’ , ‘Budget’ : 9500 , ‘Famous’:’Mountains’}
{ ‘Name’: ‘Kerala’ , ‘PeakSeason’ : ‘November’ , ‘Budget’ : 15000 , ‘Famous’:’Back Waters’}
{ ‘Name’: ‘Orissa’ , ‘PeakSeason’ : ‘January’ , ‘Budget’ : 8000 , ‘Famous’:’Temples’}
Then the stack ‘tour’ will contain :
[ ‘Orrisa’ , ‘January’]
[‘Sikkim’, ‘May’]
[‘Nainital’ ,’ May’]
The output produced when calling pop_tour( ) function should be :
[ ‘Orrisa’ , ‘January’]
[‘Sikkim’, ‘May’]
[‘Nainital’ ,’ May’]
No more tours
10. Write a function in python named PUSH(STACK, SET) where STACK is list of some numbers
forming a stack and SET is a list of some numbers. The function will push all the EVEN elements
from the SET into a STACK implemented by using a list. Display the stack after push operation
11. Write a function in Python PUSH(Arr), where Arr is a list of numbers. From this list push all
numbers divisible by 5 into a stack implemented by using a list. Display the stack if it has at least one
element, otherwise display appropriate error message.
12. Write a function in Python, Push(BItem) where , BItem is a dictionary containing the details of
bakery items–{Bname:price}.
The function should push the names of those items in the stack,S who have price less than 50.
For example:
If the dictionary contains the following data:
Bitem={"Bread":40,"Cake":250,"Muffins":80,"Biscuits":25}
The stack should contain
Bread
Biscuits
13. A list contains following record of a customer:
[Customer_name, Phone_number, City]
Write the following user defined functions to perform given operationson 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
14. A dictionary contains records of a tourist place like :
Tour_dict = { ‘Name’: ‘Goa’ , ‘PeakSeason’ : ‘December’ , ‘Budget’ :
15000 ,‘Famous’:’Beaches’}
Write the following user defined functions to perform given operations on the stack named‘tour’:
(i) Push_tour(Tour_dict) – To Push a list containing values for Name and PeakSeason where value
of budget is less than 10000 into the tour stack
(ii) Pop_tour() – To Pop the list objects from the stack and display them. Also, display “No more
tours” when there are no elements in the stack.
For example if the following dictionaries are passed to Push_tour( ) function in the following
sequence:
{ ‘Name’: ‘Goa’ , ‘PeakSeason’ : ‘December’ , ‘Budget’ : 15000 , ‘Famous’:’Beaches’}
{ ‘Name’: ‘Nainital’ , ‘PeakSeason’ : ‘May’ , ‘Budget’ : 9000 , ‘Famous’:’Nature’}
{ ‘Name’: ‘Sikkim’ , ‘PeakSeason’ : ‘May’ , ‘Budget’ : 9500 , ‘Famous’:’Mountains’}
{ ‘Name’: ‘Kerala’ , ‘PeakSeason’ : ‘November’ , ‘Budget’ : 15000 , ‘Famous’:’Back Waters’}
{ ‘Name’: ‘Orissa’ , ‘PeakSeason’ : ‘January’ , ‘Budget’ : 8000 , ‘Famous’:’Temples’}
Then the stack ‘tour’ will contain :
[ ‘Orrisa’ , ‘January’]
[‘Sikkim’, ‘May’]
[‘Nainital’ ,’ May’]
The output produced when calling pop_tour( ) function should be :
[ ‘Orrisa’ , ‘January’]
[‘Sikkim’, ‘May’]
[‘Nainital’ ,’ May’]
No more tours