CBSE · 083Class XII · 2022Section B3 marks

Question 08

7 June 2022 · Computer Science (083)

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'.

Answer

python
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')

Explanation

The code iterates through the input list, checks each word for vowels using a for-else construct, and pushes vowel-free words onto the stack. The while-else then pops every word and prints 'EmptyStack' when done.