STACK REVISION TEST
1. Write the following user defined func5ons
(i) pushEven(N) - This func5on accepts a list of integers named N as parameter. It then
pushes only even numbers into the stack named EVEN.
(i) popEven(EVEN) - This func5on pops each integer from the stack EVEN and display the
popped value. When the stack is empty the message "Stack Empty" is displayed
For example: If the list N contains: [10,5,3,8, 15,4]
Then the stack, EVEN should store [10,8,4]
2. A Dictionary containing records of students marks in computer science as
D = {“Ravi”:38,”Ramya”:99,”Somu”:78,”Jeetesh”:67,”Sangeetha”:95}
Write the following user-defined functions to perform operations on a stack named Dist to:
i. Push_element() – To push name of the students whose marks are more than 70 into the
stack named Dist.
ii. Pop_element() – To pop the items from the stack named Dist and display them. Also,
display "Stack Empty" message when there are no elements in the stack.
3. You have a stack named ProductStack that contains records of Products. Each Product
record is represented as a list containing Product_name, Product_Price and Brand. Write
the following user-defined functions in Python to perform the specified
operations on the stack ProductStack:
a) push_Product(ProductStack, new_Product): This function takes the stack ProductStack
and a new product record new_Product as arguments and pushes the new Product record
onto the stack.
b) pop_Product(ProductStack): This function pops the topmost Product record from the
stack and returns it. If the, stack is already empty, the function should display "Underflow".
c) peek(ProductStack): This function displays the topmost element of the stack
without deleting it. If the stack is empty, the function should display 'None'.
4. A dictionary, d_city contains the records in the following format:{state:city}
Define the following functions with the given specifications:
(i) push_city(d_city) : It takes the dictionary as an argument and pushes all the cities in the
stack CITY whose states are of more than 4 characters.
(ii) pop_city(): this function pops the cities and displays “Stack empty” when there are no
more cities in the stack.
(iii) display(d_city) : This function displays the elements of the stack without deleting them.
If the stack is empty the function should return ‘None’.
5. A list, FList contains the following record as list elements: [Fname, Type, Price ] 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 food.
(i) Push_element(FList): It takes the nested list as an argument and pushes a list object
containing name of the food and Type, whose price is more that 500. For example: If the
nested list contains the following data: FList=[ ["PIZZA", "VEG", 650], ["BURGER", "SPICY",
219], ["NOODLES", "NON VEG", 890], ["CHILLY CHICKEN", "SPICY", 693], ["VADA PAV",
"VEG", 150], ["CHATS", "SPICY", 345] ]
The stack should contain:
["PIZZA", "VEG"]
["NOODLES", "NON VfEG"]
["CHILLY CHICKEN", "SPICY"]
(ii) Pop_element(): The function should pop all the elements from the stack and displays
them. Also, the function should display “Food Empty” when there are no elements in the
stack.
The output should be:
["CHILLY CHICKEN", "SPICY"]
["NOODLES", "NON VEG"]
["PIZZA", "VEG"]
(iii) Display_element(): The function should display all the elements of the stack . Also, the
function should return None when there are no elements in the stack
6. Given a Dictionary Stu_dict containing marks of students for three test-series in the form
Stu_ID:(TS1, TS2, TS3) as key-value pairs.
Write a Python program with the following user-defined functions to perform the specified
operations on a stack named Stu_Stk
(i) Push_elements(Stu_Stk, Stu_dict) : It allows pushing IDs of those students, from the
dictionary Stu_dict into the stack Stu_Stk, who have scored more than or equal to 80 marks
in the TS3 Test.
(ii) Pop_elements(Stu_Stk): It removes all elements present inside the stack in LIFO order
and prints them. Also, the function displays 'Stack Empty' when there are no elements in
the stack.
Call both functions to execute [Link] example:
If the dictionary Stu_dict contains the following data:
Stu_dict ={5:(87,68,89), 10:(57,54,61), 12:(71,67,90), 14:(66,81,80), 18:(80,48,91)}
After executing Push_elements(), Stk_ID should contain [5,12,14,18]
After executing Pop_elements(), The output should be:
18
14
12
5
Stack Empty
7. Tushar received a message(string) that has uppercase and lowercase letters. He
wants to extract all the upper case letters from the string and push them into a
stack. Help him to do this task by performing the following user defined functions
in python.
a. Push the uppercase alphabets of the string into a stack.
b. Pop and display the content of the stack. Once the stack is empty it should
display the message ‘End of Stack’
For example: If the message is “All the Best for your Pre-Board Examination”
The output should be: EBPBA