Chapter 9 Important Questions-1
Chapter 9 Important Questions-1
ST=[]
for k in N:
if k%2!=0:
PUSH(ST,k)
while True:
if ST!=[]:
print(POP(ST),end=" ")
else:
break
3. YASH MOTORS have a dictionary of top performer EMPLOYEES and their SALES of as key value
pairs of COMPANY. Write a program, with separate user defined functions to perform the following
operations:
Push the keys (name of the EMPLOYEE) of the dictionary into a stack, where the corresponding
value (SALES) is greater than 500000.
Pop and display the content of the stack.
For Example: If the sample content of the dictionary is as follows:
SALES= {"SUNIL":700000,"ROHIT":400000, "RAJEEV":350000,"MAYANK":750000,
"RAHUL":1000000}
The output from the program should be:
RAHUL MAYANK SUNIL
Solution:
SALES={"SUNIL":700000,"ROHIT":400000, RAJEEV":350000,"MAYANK":750000,
RAHUL":1000000}
def PUSH(STK,S):
STK.append(S)
def POP(STK):
if STK!=[]:
return STK.pop()
else:
return None
ST=[]
for k in SALES:
if SALES[k]>500000:
PUSH(ST,k)
while True:
if ST!=[]:
print(POP(ST),end=" ")
else:
break
4. Saroj have a list of 10 numbers. 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 numbers into a stack, which are divisible by 5.
Pop and display the content of the stack.
For Example:
If the sample Content of the list is as
6. JAVED has created a dictionary containing names and marks as key value pairs of 5 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) are more than 79.
Pop and display the content of the stack.
For example:
If the sample content of the dictionary is as follows:
R={"RAKESH":70, "OMESH":50,"VISWAS":70, "ANITA":80,"ANUSHRI":90}
Solution:
R={"RAKESH":70, "OMESH":50,"VISWAS" :70, "ANITA":80,"ANUSHRI":90}
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
7. Write a function push(student) and pop(student) to add a new student name and remove a student name
from a list student, considering them to act as PUSH and POP operations of stack Data Structure in
Python.
Solution:
st=[ ]
def push(st):
sn=input("Enter name of student")
st.append(sn)
def pop(st):
if(st==[]):
print("Stack is empty")
else:
print("Deleted student name :",st.pop())
#________Main ______
push(st)
print('After push :',st)
pop(st)
print('After pop: ',st)
elif(op==3):
break
ch=input("Do you want to enter more(Y/N) :")
OUTPUT
1. Add Record
2. Display Record
3. Exit
Enter the Choice :1
Enter book name: COMPUTER SCIENCE
Enter book number: 1001
Enter book price: 450
Do you want to enter more(Y/N): Y
1. Add Record
2. Display Record
3. Exit
10. Write a function in Python PUSH(Arr), where Arr is a list of numbers. From this list push all numb
divisible by 5 into a stack implemented by using a list. Display the stack if it has at least one element,
other display appropriate error message.
Solution:
def PUSH(Arr, value):
SM=[ ]
for x in range(0, len(Arr)):
if Arr[x]%5== 0:
s.append(Arr[x])
if len(s)==0:
print("Empty Stack")
else:
print(s)
12. Write a function in python, MakePush(Package) and MakePop(Package) to add a new Package and
delete Package from a List of Package Description, considering them to act as push and pop operations of
the Stack data structure.
Solution:
def MakePush(Package):
a=int(input("Enter package title:"))
Package.append(a)
def MakePop(Package):
if (Package==[ ]):
print("Stack empty")
else:
print ("Deleted element:",Package.pop( ))
13. Write Addnew(Book) and Remote(Book) methods in Python Added and Remove a Book from a list
of Books, considering them to act as PUSH and POP operations of the data structure Stack.
Solution:
Book=[ ]
def Addnew(s):
Name=Input("Enter Book Name:")
Book.append(Name)
def Remove(self):
if (Book == [ ]):
print ("Stack Empty, UNDERFLOW!")
else:
print ("Deleted Book Is", Book.pop())
14. Write a program to implement a stack for these book-details (bookno, book name). That is, now each
item node of the stack contains two types of information - a book and its name. Just implement Push and
display operations.
Solution:
“ “ “ Stack: implemented as a list
top: integer having position of topmost element in Stack ” ” ”
def cls():
print("\n"*100)
15. Vedika has created a dictionary containing names and marks as key-value pairs of 5 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 70.
Pop and display the content of the stack.
The dictionary should be as follows:
21. 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:
If the integers input into the list NUM are :
[10,6,14,18,30]
Then the stack Only3_5 should store
[10,6,18,30]
And the output should be displayed as
30 18 6 10 StackEmpty
Solution:
def Push3_5(N):
for i in N :
if i%3==0 or i%5==0 :
Only3_5.append(i)
NUM=[]
Only3_5=[]
for i in range(5):
NUM.append(int(input('Enter an Integer: ')))
Push3_5(NUM)
while Only3_5 :
print(Only3_5.pop(), end=' ')
else :
print('StackEmpty')