Test Index
Stack
© examsnet.com
Question : 12 of 12
Marks:
+1,
-0
• 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 asGYM RHYTHM DRY EmptyStackOR• 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 as30 18 6 10 StackEmpty
[CBSE 2022 Code 91]
Solution:
(A)
NoVowel = [] # the stackdef PushNV(N): for W in N: # take each word of the list vowel = False for ch in W: if ch.upper() in "AEIOU": vowel = True break if not vowel: # no vowel in the word NoVowel.append(W) # pushAll = []for i in range(5): W = input("Enter a word: ") All.append(W) # push the word into AllPushNV(All)while NoVowel: # pop till the stack is empty print(NoVowel.pop(), end=" ")print("EmptyStack")Explanation: For ['DRY', 'LIKE', 'RHYTHM', 'WORK', 'GYM'], LIKE and WORK contain vowels, so NoVowel becomes ['DRY', 'RHYTHM', 'GYM'] (Y is not a vowel). Popping gives the last word first (LIFO):GYM RHYTHM DRY EmptyStackOR (B)Only3_5 = [] # the stackdef Push3_5(N): for x in N: if x % 3 == 0 or x % 5 == 0: Only3_5.append(x) # pushNUM = []for i in range(5): n = int(input("Enter an integer: ")) NUM.append(n)Push3_5(NUM)while Only3_5: # pop till the stack is empty print(Only3_5.pop(), end=" ")print("StackEmpty")Explanation: For [10, 6, 14, 18, 30], 14 is divisible by neither 3 nor 5, so Only3_5 becomes [10, 6, 18, 30]. Popping gives 30 18 6 10 StackEmpty.© examsnet.com
Go to Question: