Pages

Wednesday, 9 April 2025

8. Write a program in Python to input 5 words and push them one by one into a list named All.

  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 store 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:

Words=[]
for i in range(5):
    w=input("Enter any word: ")
    Words.append(w)

print(Words)
Stk=[]
V='aeiouAEIOU'

def  PushNV():
    for w in Words:
        c=0
        for v in V:
            if v in w:
                c=c+1
        if c==0:
            Stk.append(w)

def PopNV():
    while Stk!=[]:
        print(Stk.pop())
    else:
        print("Stack is Empty")


PushNV()
PopNV()

No comments:

Post a Comment