Q.
1- Write functions AddPlayer(player) and DeletePlayer(player) in
python to add and remove a player by considering them as push and
pop operations in a stack.
def AddPlayer(player):
pn=input("enter player name:")
[Link](pn)
def DeletePlayer(player):
if player==[]:
print("No player found")
else:
return [Link]()
Q.2- Vedika has created a dictionary containing names and marks as
key-value pairs of 5 students. Write a program, with separate user-
defined functions to perform the following operations:
1. Push the keys (name of the student) of the dictionary into a
stack, where the corresponding value (marks) is greater than 70.
2. Pop and display the content of the stack.
The dictionary should be as follows:
d={“Ramesh”:58, “Umesh”:78, “Vishal”:90, “Khushi”:60, “Ishika”:95}
Then the output will be: Umesh Vishal Ishika
Ans-
def push(stk,item):
[Link](item)
def Pop(stk):
if stk==[]:
return None
else:
return [Link]()
stk=[]
d={"Ramesh":58, "Umesh":78, "Vishal":90, "Khushi":60, "Ishika":95}
for i in d:
if d[i]>70:
push(stk,i)
while True:
if stk!=[]:
print(Pop(stk),end=" ")
else:
break
Q.3-A list, NList contains following record as list elements:
[City, Country, distance from Delhi]
Each of these records are nested together to form a nested list. Write the
following user defined functions in Python to perform the specified operations on
the stack named travel.
(i) Push_element(NList): It takes the nested list as an argument and pushes
a list object containing name of the city and country, which are not in
India and distance is less than 3500 km from Delhi.
(ii) (ii) Pop_element(): It pops the objects from the stack and displays them.
Also, the function should display “Stack Empty” when there are no
elements in the stack.
For example: If the nested list contains the following data:
NList=[["New York", "U.S.A.", 11734],
["Naypyidaw", "Myanmar", 3219],
["Dubai", "UAE", 2194],
["London", "England", 6693],
["Gangtok", "India", 1580],
["Columbo", "Sri Lanka", 3405]]
The stack should contain:
['Naypyidaw', 'Myanmar'],
['Dubai', 'UAE'],
['Columbo', 'Sri Lanka']
The output should be:
['Columbo', 'Sri Lanka']
['Dubai', 'UAE']
['Naypyidaw', 'Myanmar']
Stack Empty
Ans-
travel=[]
def Push_element(NList):
for L in NList:
if L[1] != “India” and L[2]<3500 :
[Link]([L[0],L[1]])
def Pop_element():
while len(travel):
print([Link]())
else:
print(“Stack Empty”)
Q.4- 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) (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
Ans:
status=[]
def Push_element(cust):
for L in cust:
if L[2]=="Goa":
L1=[L[0],L[1]]
[Link](L1)
def Pop_element ():
while len(status)!=0:
dele=[Link]()
print(dele)
else:
print("Stack Empty")
Q.5- Write a function in Python, Push(SItem) where , SItem is a dictionary
containing the details of stationary items– {Sname:price}.
The function should push the names of those items in the stack who have price
greater than 75. Also display the count of elements pushed into the stack.
For example: If the dictionary contains the following data:
Sitem={"Pen":106,"Pencil":59,"Notebook":80,"Eraser":25}
The stack should contain
Notebook
Pen
The output should be:
The count of elements in the stack is 2
Ans:
stackItem=[]
def Push(SItem):
count=0
for k in SItem:
if (SItem[k]>=75):
[Link](k)
count=count+1
print("The count of elements in the stack is : ", count)