STACK ASSIGNMENT
Q1. A list contains following record of customer :
[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
Q2. 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
Q3. 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.
Q4. 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.
Q5. 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
Q6. 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.
Q7. Write a function in Python POP(Arr), where Arr is a stack implemented by a list of
numbers. The function returns the value deleted from the stack.
Q8. 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 stack. For 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
Q9. Write the definition of a user defined function Push3_5(N) which accepts a list of
integers in a parameter N and pushes all those integers which are divisible by 3 or
divisible by 5 from the list N into a stack named Only3_5.
Q10 . A list containing records of products as
L = [("Laptop", 90000), ("Mobile", 30000), ("Pen", 50), ("Headphones", 1500)]
Write the following user-defined functions to perform operations on a stack named
Product to:
(i) Push_element() – To push an item containing the product name and price of
products costing more than 50 into the stack.
Output: [('Laptop', 90000), ('Mobile', 30000), ('Headphones', 1500)]
(ii) Pop_element() – To pop the items from the stack and display them. Also, display
"Stack Empty" when there are no elements in the stack.
Output: ('Headphones', 1500)
('Mobile', 30000)
('Laptop', 90000)
Stack Emply
Q11. You have a stack named BooksStack that contains records of books. Each book record is
represented as a list containing book_title, author_name, and publication_year.
Write the following user-defined functions in Python to perform the specified operations on
the stack BooksStack:
(i) push_book(BooksStack, new_book): This function takes the stack BooksStack
and a new book record new_book as arguments and pushes the new book record
onto the stack.
(ii) pop_book(BooksStack): This function pops the topmost book record from the stack
and returns it. If the stack is already empty, the function should display
"Underflow".
(iii) peep(BookStack): This function displays the topmost element of the stack without
deleting it. If the stack is empty, the function should display 'None'.
Q12. (i) Write the definition of a user-defined function `push_even(N)` which accepts a list of
integers in a parameter `N` and pushes all those integers which are even from the
list `N` into a Stack named `EvenNumbers`.
(ii) Write function pop_even() to pop the topmost number from the stack and returns it.
If the stack is already empty, the function should display "Empty". Write function
Disp_even() to display all element of the stack without deleting them. If the stack is
empty, the function should display 'None'.
For example: If the integers input into the list `VALUES` are: [10, 5, 8, 3, 12]
Then the stack `EvenNumbers` should store: [10, 8, 12]
Q13.