[Link] has created a dictionary with names as keys and marks as values for 6 students.
Write a
program with separate user-defined functions to perform the following operations.
(a) Push the keys (name of the student) of the dictionary into a stack, where the corresponding value
(marks) is greater than 75.
(b) 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
Ans:
# Function to push keys with marks > 75 into stack
def push_students(student_dict, stack):
for name, marks in student_dict.items():
if marks > 75:
[Link](name)
# Function to pop and display the stack
def pop_students(stack):
while stack:
print([Link](), end=" ")
# Main program
R = {"OM": 76, "JAI": 45, "BOB": 89, "ALI": 65, "ANU": 90, "TOM": 82}
stack = []
# Push operation
push_students(R, stack)
# Pop and display
pop_students(stack)
2. Rohit has a list of 10 integers. Write a program with separate user-defined functions to
perform the specified operations on this list.. Page 6 Sample Paper 01 NODIA APP
(a) Traverse the content of the list and push the even numbers into a stack.
(b) 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
Ans:
# Function to push even numbers into the stack
def push_even_numbers(num_list, stack):
for num in num_list:
if num % 2 == 0:
[Link](num)
# Function to pop and display stack content
def pop_stack(stack):
while stack:
print([Link](), end=" ")
# Main program
N = [12, 13, 34, 56, 21, 79, 98, 22, 35, 38]
stack = []
# Push operation
push_even_numbers(N, stack)
# Pop and display
pop_stack(stack)
3. Consider the following stack of characters, where STACK is allocated N = 8 memory cells.
STACK : A, C, D, F, K,...,...,...
Describe the STACK at the end of the following operations. Here, Pop and Push are algorithms for
deleting
and adding an element to the stack.
(i) Pop (STACK, ITEM)
(ii) Pop (STACK, ITEM)
(iii) Push (STACK, L)
(iv) Push (STACK, P)
(v) Pop (STACK, ITEM)
(vi) Push (STACK, R)
Ans:
Step-by-step operations
(i) Pop(STACK, ITEM)
Removes K from the top.
ITEM = K
New STACK: [A, C, D, F, ..., ..., ..., ...]
Top → F
(ii) Pop(STACK, ITEM)
Removes F from the top.
ITEM = F
New STACK: [A, C, D, ..., ..., ..., ..., ...]
Top → D
(iii) Push(STACK, L)
Adds L to the top.
STACK: [A, C, D, L, ..., ..., ..., ...]
Top → L
(iv) Push(STACK, P)
Adds P to the top.
STACK: [A, C, D, L, P, ..., ..., ...]
Top → P
(v) Pop(STACK, ITEM)
Removes P from the top.
ITEM = P
STACK: [A, C, D, L, ..., ..., ..., ...]
Top → L
(vi) Push(STACK, R)
Adds R to the top.
Final STACK:
mathematica
CopyEdit
[A, C, D, L, R, ..., ..., ...]
Top → R
4. Consider the following sequence of numbers:
1, 2, 3, 4
These are supposed to be operated through a stack to produce the following sequence of numbers:
2, 1, 4, 3
List the Push and Pop operations to get the required output.
Ans:
Step-by-step operations
1. Push 1
Stack: [1]
2. Push 2
Stack: [1, 2]
3. Pop → output 2
Stack: [1]
4. Pop → output 1
Stack: []
5. Push 3
Stack: [3]
6. Push 4
Stack: [3, 4]
7. Pop → output 4
Stack: [3]
8. Pop → output 3
Stack: []
5. Write Push(contents) and Pop() methods in Python to add and remove numbers, simulating
stack operations.
Ans:
lass Stack:
def __init__(self):
[Link] = []
def Push(self, value):
[Link](value)
def Pop(self, n=1):
# Pop n times
for _ in range(n):
if [Link]:
[Link]()
else:
print("Stack is empty!")
# Example stack
s = Stack()
6. Find the final contents of a stack on which the following operations are done.
1. Push(100)
2. Push(200)
3. Push(50)
4. Push(50)
5. Pop()
6. Push()
7. Pop(2)
8. Pop()
Ans:
Step-by-Step Trace of Given Operations
Given sequence (assuming the missing value in step 6 is 300 just to proceed; otherwise it’s
incomplete):
1. Push(100) → [100]
2. Push(200) → [100, 200]
3. Push(50) → [100, 200, 50]
4. Push(50) → [100, 200, 50, 50]
5. Pop() → removes top 50 → [100, 200, 50]
6. Push(??) → Here a value is missing; I’ll assume 300 → [100, 200, 50, 300]
7. Pop(2) → removes 300 then 50 → [100, 200]
8. Pop() → removes 200 → [100]
7. Write a function to pop an element from a stack “s” using a function stackpop().
Ans:
def stackpop(s):
if len(s) == 0:
print("Stack is empty!")
return None
else:
element = [Link]()
print(f"Popped element: {element}")
return element
# Example usage
s = [10, 20, 30, 40] # Initial stack
stackpop(s) # Pops 40
stackpop(s) # Pops 30
stackpop([])