Computer Science
Write Addscore(Game) and Delscore(Game) methods in Python to add new Score in the list of score in a game and remove a score from a list of score of a game considering these methods to act as PUSH and POP operation of data structure Stack.
Python
Python Stack
7 Likes
Answer
def Addscore(Game):
score = int(input("Enter the score to add: "))
Game.append(score)
def Delscore(Game):
if Game == []:
print("Game's score list is empty")
else:
print("Deleted score:", Game.pop())
Game = []
Addscore(Game)
Addscore(Game)
Addscore(Game)
print("Initial Game Stack:", Game)
Delscore(Game)
print("Game Stack after deletion:", Game)
Output
Enter the score to add: 10
Enter the score to add: 15
Enter the score to add: 20
Initial Game Stack: [10, 15, 20]
Deleted score: 20
Game Stack after deletion: [10, 15]
Answered By
3 Likes
Related Questions
Write add(Books) and delete(Books) methods in Python to add Books and Remove Books considering them to act as append() and pop() operations in Stack.
Write AddClient(Client) and DeleteClient(Client) methods in Python to add a new client and delete a client from a list client name, considering them to act as insert and delete operations of the Queue data structure.
Write a Python program to sort a Stack in ascending order without using an additional Stack.
A Stack STK and Queue QUE is being maintained. Their maximum size is the same:
(i) check whether they have the same size, i.e., have the same number of elements.
(ii) check for equality of Stack and Queue, i.e.,
- Retrieve an element from the Stack and one element from the Queue.
- Compare the two.
- Stop and report if elements are different.