We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
This is for discard, pop and remove method. In this method, I have used getattr techniques as it handles the missing attribute and also it returns the value of an attribute.
n = int(input())
s = set(map(int, input().split()))
N= int(input())
for i in range(N):
output = input().split()
if len(output)==1:
getattr(s,output[0])()
try:
if len(output)==2:
output1 = getattr(s,output[0])
output1(int(output[1]))
except:
pass
print(sum(s))
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Set .discard(), .remove() & .pop()
You are viewing a single comment's thread. Return to all comments →
This is for discard, pop and remove method. In this method, I have used getattr techniques as it handles the missing attribute and also it returns the value of an attribute.
n = int(input()) s = set(map(int, input().split())) N= int(input()) for i in range(N): output = input().split() if len(output)==1: getattr(s,output[0])() try: if len(output)==2: output1 = getattr(s,output[0]) output1(int(output[1])) except: pass
print(sum(s))