Python Set discard, remove and pop functions

Author: Al-mamun Sarkar Date: 2020-03-24 17:39:05

Python Set discard, remove and pop functions. Following code is the solution of a HackerRank Problem Set .discard(), .remove() & .pop()

Code:


n = int(input())
s = set( map(int, input().split()) )
N = int( input() )

for i in range(0,N):
	input_data = input().split()
	if input_data[0] == 'pop':
		s.pop()
	if input_data[0] == 'remove':
		s.remove( int(input_data[1]) )
	if input_data[0] == 'discard':
		s.discard( int(input_data[1]) )

print(sum(s))
		

 

Test Input:

9
1 2 3 4 5 6 7 8 9
10
pop
remove 9
discard 9
discard 8
remove 7
pop 
discard 6
remove 5
pop 
discard 5

Test Output:

4