Stacks
Stacks
3 Class XII
Question 1:
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.
Question 2:
Consider a list named Nums which contains random integers.
Write the following user-defined functions in Python and perform the specified operations on
a stack named BigNums:
(i) PushBig(): It checks every number from the list Nums and pushes all such numbers which
have 5 or more digits into the stack, BigNums.
(ii) PopBig(): It pops the numbers from the stack, BigNums and displays them. The function
should also display "Stack Empty" when there are no more numbers left in the stack.
Question 3:
A list contains the following record of course details for a University:
[Course_name, Fees, Duration]
Write the following user-defined functions to perform given operations on the stack named
Univ:(i) (i) Push_element() – To push an object containing the Course_name, Fees, and
Duration of a course, which has fees greater than 100000 to the stack.
(ii) Pop_element() – To pop the object from the stack and display it. Also, display
"Underflow" when there is no element in the stack.
For example:
If the list of course details are:
["MCA", 200000, 3]
["MBA", 500000, 2]
["BA", 100000, 3]
The stack should contain:
["MBA", 500000, 2]
["MCA", 200000, 3]
Question 4:
Write the following user defined functions :
(i) pushEven(N) This function accepts a list of integers named N as parameter. It then
pushes only even numbers into the stack named EVEN.
1|Page
(ii) popEven(EVEN) This function pops each integer from the stack EVEN and displays 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]
And the output should be
4 8 10 Stack Empty
Question 5:
Write separate user defined functions for the following :
(i) PUSH(N) This function accepts a list of names, N as parameter.
It then pushes only those names in the stack named OnlyA which contain the letter 'A'.
(ii) POPA(OnlyA) This function pops each name from the stack OnlyA and displays it.
When the stack is empty, the message
"EMPTY" is displayed.
For example :
If the names in the list N are
['ANKITA', 'NITISH', 'ANWAR', 'DIMPLE', 'HARKIRAT']
Then the stack OnlyA should store
['ANKITA', 'ANWAR', 'HARKIRAT']
And the output should be displayed as
HARKIRAT ANWAR ANKITA EMPTY
Question 6:
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 list named Only3_5.
Write a program in Python to input 5 integers into a list named NUM.The program should
then use the function Push3_5() to create the stack of the list Only3_5. Thereafter, pop each
integer from the list Only3_5 and display the popped value. When the list is empty, display
the message "StackEmpty".
For example:
30 18 6 10 StackEmpty
Question 7:
Write the definition of a user-defined function PushNV(N) which accepts a list of strings in
the parameter N and pushes all strings which have no vowels present in it, into a list named
NoVowel.
2|Page
Question 8:
Write a program in Python to input 5 words and push them one by one into a list named All.
The program should then use the function PushNV() to create a stack of words in the list
NoVowel so that it stores only those words which do not have any vowel present in it, from
the list All. Thereafter, pop each word from the list NoVowel and display the popped word.
When the stack is empty display the message "EmptyStack".
For example:
If the words accepted and pushed into the list All are:
Question 9:
3|Page
Question 10:
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. 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.
Question 11:
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
Question 12:
Alam has a list containing 10 integers. You need to help him create a program with separate
user defined functions to perform the following operations based on this list.
● Traverse the content of the list and push the even numbers into a stack.
● Pop and display the content of the stack.
For Example:
If the sample Content of the list is as follows:
N=[12, 13, 34, 56, 21, 79, 98, 22, 35, 38]
Sample Output of the code should be:
38 22 98 56 34 12
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:
[“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
4|Page
Question 13:
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
**************
5|Page