Q. Infopedia is an online encyclopaedia which stores detailed information about various countries. In order to provide efficient processing for retrieval and display of names of countries, it has to be digitized, which requires handling additions and deletions through data structures in Python, primarily stacks and queues.


Write a program constituting methods in Python to add, display and remove a name from a given stack of names of countries.


Answer :-


def PUSH (country_name) :
    cname = input ("Enter the name of the country to be added : ")
    country_name.append (cname)

def POP (country_name):
    if country_name != []:
        print (country_name.pop())
    else:
        print ("Empty stack of Encyclopedia")

def SHOW (country_name):
    print (country_name)

STACK = []
Choice = ''
while Choice != 'Q':
    print ("P:PUSH O: POP S: SHOW Q:QUIT")
    Choice = input("Enter your choice : ")
    if Choice == "P":
        PUSH (STACK)
    elif Choice == "O":
        POP (STACK)
    elif Choice == "S":
        SHOW (STACK)
    elif Choice == "Q":
        break


Output :-

P:PUSH O: POP S: SHOW Q:QUIT
Enter your choice : P
Enter the name of the country to be added : INDIA
P:PUSH O: POP S: SHOW Q:QUIT
Enter your choice : P
Enter the name of the country to be added : POK
P:PUSH O: POP S: SHOW Q:QUIT
Enter your choice : P
Enter the name of the country to be added : JAPAN
P:PUSH O: POP S: SHOW Q:QUIT
Enter your choice : P
Enter the name of the country to be added : USA
P:PUSH O: POP S: SHOW Q:QUIT
Enter your choice : S
['INDIA', 'POK', 'JAPAN', 'USA']
P:PUSH O: POP S: SHOW Q:QUIT
Enter your choice : P
Enter the name of the country to be added : UK
P:PUSH O: POP S: SHOW Q:QUIT
Enter your choice : O
UK
P:PUSH O: POP S: SHOW Q:QUIT
Enter your choice : S
['INDIA', 'POK', 'JAPAN', 'USA']
P:PUSH O: POP S: SHOW Q:QUIT
Enter your choice : Q

>>>

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post