STACK – 3 MARK PRACTICE QUESTIONS
Four scenarios covering different stack patterns
Question 1
Pattern 1 | Global list | No arguments
Write the definition of a user-defined function to perform the following operations on a stack named
HighTemp, using a global list CityTemps containing temperature readings (in °C) of various cities.
1. push_temp() – push all those temperatures which are above 30 from the list CityTemps into a
Stack named HighTemp.
2. pop_temp() – to pop the topmost temperature from the stack and display it. If the stack is
already empty, the function should display "Empty".
3. Disp_temp() – to display all elements of the stack without deleting them. If the stack is empty,
the function should display "None".
4. peep_temp() – This function displays the topmost element of the stack without deleting it. If the
stack is empty, the function should display "None".
5. pop_all_temp() – This function pops the items from the stack and displays them. Also, display
"Stack Empty" when there are no elements in the stack.
6. isempty() – This function checks whether the stack is empty or not. The function should return
True if the stack is empty, otherwise return False.
For example:
If the temperatures input into the list CityTemps are:
CityTemps = [28, 34, 30, 36, 25, 40]
Then the stack HighTemp should store:
[34, 36, 40]
Question 2
Pattern 2 | Arguments | List of Lists
A stack named PatientQueue stores patient records. Each record is a List containing three
elements – [PatientName, Age, Temperature].
Example record: ['Ravi', 45, 101.2]
Records are picked from a global list of lists named PatientData, selecting only those patients
whose Temperature is above 100 (fever cases). Write the following user-defined Python functions,
each receiving the stack PatientQueue as an argument, to perform stack operations:
1. push_Patient(PatientQueue, new_Patient) – This function receives the stack PatientQueue
and a new record new_Patient as arguments and pushes the new record onto the stack.
2. pop_Patient(PatientQueue) – This function receives the stack as an argument, pops and
displays the topmost patient record from it. If the stack is empty, it should display "Underflow -
No patient record".
3. Disp_Patient(PatientQueue) – This function receives the stack as an argument and displays all
records without deleting them. If the stack is empty, it should display "No Records".
4. peep_Patient(PatientQueue) – This function receives the stack as an argument and displays the
topmost record without deleting it. If the stack is empty, it should display "No Records".
5. pop_all_Patient(PatientQueue) – This function receives the stack as an argument, pops the
records from it and displays them one by one. Also, display "Queue Empty" when there are no
records left in the stack.
6. isEmpty(PatientQueue) – This function receives the stack as an argument and checks whether
it is empty or not. The function should return True if the stack is empty, otherwise return False.
For example:
PatientData = [['Ravi',45,101.2], ['Sonia',30,98.4], ['Kabir',60,102.5], ['Meena',22,99.1],
['Farhan',50,100.6]]
Then the stack PatientQueue should store:
[['Ravi',45,101.2], ['Kabir',60,102.5], ['Farhan',50,100.6]]
Question 3
Pattern 3 | Arguments | List of Dictionaries
A stack named OrderStack stores food order records. Each record is a Dictionary with keys
'Item', 'Table', and 'Amount'.
Example record: {'Item': 'Pizza', 'Table': 5, 'Amount': 450}
Records are picked from a global list of dictionaries named OrderData, selecting only those orders
whose Amount is greater than or equal to 300. Write the following user-defined Python
functions, each receiving the stack OrderStack as an argument, to perform stack operations:
1. push_Order(OrderStack, new_Order) – This function receives the stack OrderStack and a new
record new_Order as arguments and pushes the new record onto the stack.
2. pop_Order(OrderStack) – This function receives the stack as an argument, pops and displays
the topmost order from it. If the stack is empty, it should display "Underflow - No order to
remove".
3. Disp_Order(OrderStack) – This function receives the stack as an argument and displays all
orders without deleting them. If the stack is empty, it should display "No Orders".
4. peep_Order(OrderStack) – This function receives the stack as an argument and displays the
topmost order without deleting it. If the stack is empty, it should display "No Orders".
5. pop_all_Order(OrderStack) – This function receives the stack as an argument, pops the orders
from it and displays them one by one. Also, display "Stack Empty" when there are no orders left
in the stack.
6. isEmpty(OrderStack) – This function receives the stack as an argument and checks whether it
is empty or not. The function should return True if the stack is empty, otherwise return False.
For example:
OrderData = [{'Item':'Pizza','Table':5,'Amount':450},
{'Item':'Coffee','Table':2,'Amount':120},
{'Item':'Burger','Table':3,'Amount':300}, {'Item':'Tea','Table':1,'Amount':80},
{'Item':'Pasta','Table':4,'Amount':350}]
Then the stack OrderStack should store:
[{'Item':'Pizza','Table':5,'Amount':450}, {'Item':'Burger','Table':3,'Amount':300},
{'Item':'Pasta','Table':4,'Amount':350}]
Question 3a
Pattern 3a | Global list | No arguments | List of Tuples
A stack named EmpStack stores employee records. Each record is a Tuple containing (EmpName,
Department, Salary).
Example record: ('Anita', 'Sales', 55000)
Records are picked from a global list of tuples named EmpData, selecting only those employees
who belong to the 'IT' department. Write the definition of the following user-defined Python
functions to perform stack operations on EmpStack:
1. push_Emp() – push all those employee records belonging to the 'IT' department from the list
EmpData into a Stack named EmpStack.
2. pop_Emp() – to pop the topmost employee record from the stack and display it. If the stack is
already empty, the function should display "Underflow - No employee record".
3. Disp_Emp() – to display all records of the stack without deleting them. If the stack is empty, the
function should display "No Records".
4. peep_Emp() – This function displays the topmost record of the stack without deleting it. If the
stack is empty, the function should display "No Records".
5. pop_all_Emp() – This function pops the records from the stack and displays them. Also, display
"Stack Empty" when there are no records in the stack.
6. isEmpty() – This function checks whether the stack is empty or not. The function should return
True if the stack is empty, otherwise return False.
For example:
EmpData = [('Anita','IT',55000), ('Ravi','Sales',40000), ('Kumar','IT',48000),
('Priya','HR',35000), ('Suresh','IT',52000)]
Then the stack EmpStack should store:
[('Anita','IT',55000), ('Kumar','IT',48000), ('Suresh','IT',52000)]