Wednesday, 9 April 2025

2. A School has created a dictionary containing top players and their runs as key value pairs of cricket team.

 A School has created a dictionary containing top players and their runs as key value pairs of cricket team. Write a program with separate user defined functions to perform the following operations:

(a)    Push the name of the players(Keys) of the dictionary into a stack, where the corresponding runs (value) is greater than 49.
(b)    Pop and display the content of the stack.
For Example
If dictionary has the following values:
Data={'Rohan':40, 'Rihaan':55, 'Tejas':80,'Ajay':90}
The output should be:
Ajay
Tejas
Rihaan


Solutions

S=[]
Data={'Rohan':40, 'Rihaan':55, 'Tejas':80,'Ajay':90}
def Push_Player():
    for k in Data.keys():
        if Data[k]>49:
            S.append(k)

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



Push_Player()
Pop_Player()

No comments:

Post a Comment