Chapter 9 Important Questions
Chapter 9 Important Questions
3. Exit
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:
d={“Ramesh”:58, “Umesh”:78, “Vishal”:90, “Khushi”:60, “Ishika”:95}
Then the output will be:
Umesh Vishal Ishika
Solution:
def push(stk,item):
stk.append(item)
def Pop(stk):
if stk==[ ]:
return None
else:
return stk.pop()
stk=[]
d={"Ramesh":58, "Umesh":78, "Vishal":90, "Khushi":60, "Ishika":95}
for i in d:
if d[i]>70:
push(stk,i)
while True:
if stk!=[]:
print(Pop(stk),end=" ")
else:
break
16. Write a function in Python Push (nums), where nums is a list of numbers. From this list push all odd
numbers into a stack implemented by using a list. Display the stack if it has at least one element,
otherwise display appropriate error message.
Solution:
def Push(nums):
li =[]
for i in range(0,len(nums)):
if nums[i]%2!=0:
li.append(nums[i])
if len(li) == 0:
print('Stack is empty!!')
else:
print(li)
Push([10,15,20,25,30,35])
Note : Any correct code giving the same result is also accepted
17. Write a function in Python Popstack (names), where names is a stack implemented by a list of names.
The function returns the name deleted from the stack.
Solution:
def popStack(names) :
L = len(names)
if L==0:
print("Stack Underflow")
else:
val = names[L-1]
names.pop(L-1)
return val
res=popStack(['Harikaran','Balaji','Nikhil','Cathrine'])
print('Value returned:',res)
Note : Any correct code giving the same result is also accepted
18. A stack S contains Name of countries. Write a function POP(A), to remove Name from stack.
Function should handle underflow condition and should return removed element.
Solution:
def POP(A):
if len(A)==0:
print(“UNDERFLOW”)
else:
val=A.pop()
return val
19. Write a function PUSH(STACK,Name) to add new name to the STACK data structure . Also display
all the names having atleast 4 characters from the stack.
Solution:
def PUSH(STACK,Name):
STACK.append(Name)
#code for displaying
if len(STACK)==0:
print(“Empty stack”)
else:
top=len(STACK)-1
for i in range(top,-1,-1):
if len(STACK[i])>=4:
print(STACK[i] )
20. Write the definition of a user defined function PushNV(N) which accepts a list of strings in the
parameter N and pushes all strings which have no vowels present in it, into a list named NoVowel.
● Write a program in Python to input 5 Words and push them one by one into a list named all.
The program should then use the function PushNV( ) to create a stack of words in the list NoVowel so
that it stores only those words which do not have any vowel present in it, from the list All. Thereafter, pop
each word from the list NoVowel and display the popped word. When the stack is empty, display the
message "EmptyStack".
For example:
If the Words accepted and pushed into the list All are
['DRY', 'LIKE', 'RHYTHM', 'WORK', 'GYM']
Then the stack NoVowel should store
['DRY', 'RHYTHM', 'GYM']
And the output should be displayed as
GYM RHYTHM DRY EmptyStack
Solution:
def PushNV(N):
for W in N :
for C in W :
if C.upper() in 'AEIOU':
break
else:
NoVowel.append(W)
All=[]
NoVowel=[]
for i in range(5) :
All.append(input('Enter a Word: '))
PushNV(All)
while NoVowel :
print(NoVowel.pop(), end=' ')
else :
print('EmptyStack')
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')