SRI KRISH INTERNATIONAL SCHOOL - CBSE
CLASS: XII A & B D.O.I
COMPUTER SCIENCE
D.O.S
CHAPTER 5 WORKSHEET
1. A dictionary contains Cricket Player names and their runs scored. Write user-defined
functions Push_Player(PlayerStack, DataDict) to push names of players who scored
more than 49 runs onto a stack, and Pop_Player(PlayerStack) to pop and display the
names.
2. A list contains tuples with (Employee_ID, Name, Department). Write a program with
Push_Emp() to push the Names of employees who work in the "Admin" department onto
a stack, and Pop_Emp() to remove them.
3. Write user-defined functions to push numbers from a list into a stack called VanishStack
only if they are divisible by 5. Write the corresponding pop function.
4. 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:
● 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.
● 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".
● peep(BookStack): This function displays the topmost element of the stack without
deleting it. If the stack is empty, the function should display 'None'.
5. Thushar received a message(string) that has upper case and lower-case alphabet. He wants
to extract all the upper case letters separately. Help him to do his task by performing the
following user defined function in
a. Push the upper case alphabets in the string into a STACK
b. Pop and display the content of the stack.
For example: If the message is “All the Best for your Pre-board Examination”
The output should be : E P B A
6. Write a function in Python, Push(EventDetails) where , EventDetails is a dictionary
containing the number of persons attending the events– {EventName :
NumberOfPersons}. The function should push the names of those events in the stack
named ‘BigEvents’ which have number of persons greater than 200. Also display the
count of elements pushed o n to the stack.
For example: If the dictionary contains the following data:
EventDetails ={"Marriage":300, "Graduation Party":1500, "Birthday Party":80, "Getogether" :150}
The stack should contain : Marriage Graduation Party
The output should be: The count of elements in the stack is 2
7. A list of numbers is used to populate the contents of a stack using a function push(stack,
data) where stack is an empty list and data is the list of numbers. The function should
push all the numbers that are even to the stack. Also write the function pop(stack) that
removes and returns the top element of the stack on its each call.
8. A list contains following record of a student: [student_name, age, hostel] Write the
following user defined functions to perform given operations on the stack named
‘stud_details’:
(i) Push_element() - To Push an object containing name and age of students who live in
hostel “Ganga” 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: [“Barsat”,17,”Ganga”] [“Ruben”,
16,”Kaveri”] [“Rupesh”,19,”Yamuna”] The output should be: [“Barsat”,17,”Ganga”]
Stack Empty
9. Write a function in Python PUSH_IN(L), where L is a list of numbers. From this list,
push all numbers which are multiple of 3 into a stack which is implemented by using
another list.
10. Write a function in Python, Push(KItem),where KItem is a dictionary containing the
details of Kitchen items– {Item:price}. The function should push the names of those
items in a stack which have price less than 100. Also display the average price of
elements pushed into the stack.
For example: If the dictionary contains the following data:
{"Spoons":116,"Knife":50,"Plates":180,"Glass":60}
The stack should contain Glass Knife
The output should be: The average price of an item is 55.0
11. 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
userdefined 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 queries.
For 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
12. 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. For example: If the list Nums contains the following data:
Nums [213,10025,167,254923,14,1297653,31498,386,92765)
Then on execution of PushBig(), the stack BigNums should store: [10025, 254923,
1297653, 31498, 92765]
And on execution of PopBig (), the following output should be displayed:
92765 31498 1297653 254923 10025 Stack Empty
13. 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.
Answer the following questions:
1. Stack implementation can be performed using a list in Python. (True / False)
2. top operation does not modify the contents of a stack. (True / False)
3. The peek operation refers to accessing/inspecting the top element in the stack
(True/False)
4. len() method used to find the size of stack. (True /False)
5. What is the process of inserting data into a stack called?
a. Create b. Insert c. Push d. Evaluate
6. Which pointer is associated with a stack?
a. First b. Front c. Rear d. Top
7. Assume a stack has size 10. If a user tries to push a 11th element to a stack, which of
the mentioned condition will arise?
a. Underflow b. Overflow c. Crash d. Successful Insertion
8. Which of these is not an application of stack?
a. Parenthesis Balancing program b. Evaluating Arithmetic Expressions c. Reversing
Data d. Data Transfer between Process
a. Both A and R are true, and R is the correct explanation of A
b. Both A and R are true, but R is not the correct explanation of A
c. A is true, but R is false
d. A is false, but R is true
9. Assertion (A) : Stack allows element addition at one end only.
Reason (R) : Stack operations are performed at both end.
10. Assertion (A) : A stack is used to reverse a string.
Reason (R) : Stack follows Last-In, First-Out (LIFO) order.