Computer Science - Class 12
Solutions to CBSE Sample Paper - Computer Science Class 12

A list contains following record of a customer:

[Customer_name, Phone_number, City]

Write the following user defined functions to perform given operations

on the stack named ‘status’:

(i) Push_element() - To Push an object containing name and

Phone number of customers who live in Goa to the stack

(ii) Pop_element() - To Pop the objects from the stack and

display them. Also, display “Stack Empty” when there are no

elements in the stack.

For example:

If the lists of customer details are:

 

[“Gurdas”, “99999999999”,”Goa”]

[“Julee”, “8888888888”,”Mumbai”]

[“Murugan”,”77777777777”,”Cochin”]

[“Ashmit”, “1010101010”,”Goa”]

 

The stack should contain

[“Ashmit”,”1010101010”]

[“Gurdas”,”9999999999”]

 

The output should be:

[“Ashmit”,”1010101010”]

[“Gurdas”,”9999999999”]

Stack Empty

 

Answer:

status=[ ]

 

def Push_element (c):

    if c[2]== "Goa" :

        obj=[c[ 0 ],c[ 1 ]]

        status.append(c)

 

def Pop_element ():

    while len(status)!= 0 :

        d=status.pop()

        print(d)

    print( "Stack empty" )

Slide4.JPG

Go Ad-free
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh has done his B.Tech from Indian Institute of Technology, Kanpur. He has been teaching from the past 14 years. He provides courses for Maths, Science and Computer Science at Teachoo