Q 1.
Write a menu-driven program to implement a stack to store book
name.
1. Push
2. Pop
3. Peek
4. Display
5. Exit
Answer :
def isEmpty(stk):
if stk == []:
return True
else:
return False
def push(stk,item):
stk.append(item)
top = len(stk)-1
def pop(stk):
if(stk==[]):
print("Stack empty;UNderflow")
else:
print("Deleted Book is :",stk.pop())
def display(stk):
if isEmpty(stk):
print("Stack empty ")
else :
top = len(stk)-1
print(stk[top],"<-top")
for a in range(top-1,-1,-1):
print(stk[a])
stack=[]
top = None
while True:
print("STACK OPERATION:")
print("1.ADD BOOK")
print("2.Display STACK")
print("3.REMOVE BOOK")
print("4.Exit")
ch = int(input("Enter your choice(1-4):"))
if ch==1:
bno = int(input("Enter BOOK No to be inserted :"))
bname = input("Enter BOOK Name to be inserted :")
item = [bno,bname]
push(stack,item)
input()
elif ch==2:
display(stack)
input()
elif ch==3:
pop(stack)
input()
elif ch==4:
break
else:
print("Invalid choice ")
input()
Q2 ) 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
Answer :
R={"OM":76, "JAI":45, "BOB":89, "ALI":65, "ANU":90, "TOM":82}
def PUSH(S,N):
S.append(N)
def POP(S):
if S!=[]:
return S.pop()
else:
return None
ST=[]
for k in R:
if R[k]>=75:
PUSH(ST,k)
while True:
if ST!=[]:
print(POP(ST),end=" ")
else:
break
Q3) 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
Answer:
N=[12, 13, 34, 56, 21, 79, 98, 22, 35, 38]
def PUSH(S,N):
S.append(N)
def POP(S):
if S!=[ ]:
return S.pop()
else:
return None
ST=[ ]
for k in N:
if k%2==0:
PUSH(ST,k)
while True:
if ST!=[ ]:
print(POP(ST),end=" ")
else:
break
Q4) Julie has created a dictionary containing Roll No 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 (roll no of the student) of the dictionary into a stack in
ascending order of marks.
● Pop and display the content of the stack.
For example
Original Dictionary = {1 : 25, 2 : 21, 3 : 23, 4: 20, 5: 24, 6:22}
Expected Output = [25, 24, 23, 22, 21]
Answer:
a) d={1 : 25, 2 : 21, 3 : 23, 4: 20, 5: 24, 6:22}
print(sorted(d.values()))
print(d)
b) stack = [1 : 25, 2 : 21, 3 : 23, 4: 20, 5: 24, 6:22]
print('Initial stack')
print(stack)
# LIFO order
print('\nElements popped from stack:')
print(stack.pop())
print(stack.pop())
print(stack.pop())
print('\nStack after elements are popped:')
print(stack)
Q5) Write a program in python to push the duplicate values from the
dictionary into stack called duplicate. Write disp() function to print all the
values from the stack.
For example:
Original dictionary = {1 : “Aman”, 2: “Suman”, 3: “Aman”}
duplicate = [“Aman”]
Answers:
d1={1:”Aman”,2:”Suman”,3:”Aman”}
nd1={ }
for k,v in d1.items():
if v not in nd1.values():
nd1[k]=v
print(nd1)
Q6) HR Dept in the organization maintains name and salary of each
employee as value of a dictionary with id as keys. The management
wants record of all the employees drawing salary less than 20,000.
● Push the keys (id of the emp) of the dictionary into a stack,
where the corresponding value (salary) is less than 20000.
● Pop and display the content of the stack along with
corresponding name.
Data is stored in the dictionary in the following format
{Empid : (Empname, EmpSalary}
Q7) Write a program in python which reads a list and
push all the numbers in the list.
Perform pop operation and print sum total of all numbers
L = [‘Keyboard’, 7, ‘Ram’ , 9, ‘Monitor’ , 11 , ‘Mouse’]
Answers:
a)
b)
1) d1= [‘Keyboard’, 7, ‘Ram’ , 9, ‘Monitor’ , 11 , ‘Mouse’]
k=int(input(“Enter any key”))
if k in d1:
d1.pop(k)
print(d1)
else:
print(“Key not found”)
2)
print sum total of all numbers
L = ['Keyboard', 7, 'Ram' , 9, 'Monitor' , 11 , 'Mouse']
s=0
for i in L:
if str(i).isdigit():
s=s+i
print(s)
Q8) Read a list of n elements. Pass this list to a function which reverses
this list using stack.
For Ex
Original List is : [1, 2, 3, 4, 5]
List after reversing is: [5, 4, 3, 2, 1]
Answer:
array=[1,2,3,4,5]
for i in reversed(array):
print(i)
Q9 ) 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 POP(Arr), where Arr is a stack implemented by a
list of numbers. The function returns the value deleted from the stack.
Answer:
a) def PUSH(Arr):
s=[]
for i in range(0,len(Arr)):
if Arr[i]%5==0 :
s.append(Arr[x])
if len(s)==0:
print("Empty Stack")
else:
print(s)
b) def popStack(st) : # If stack is empty
if len(st)==0:
print("Underflow")
else:
L = len(st)
val=st[L-1]
print(val)
st.pop(L-1)