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.
Set .discard(), .remove() & .pop()
Set .discard(), .remove() & .pop()
Sort by
recency
|
1097 Discussions
|
Please Login in order to post a comment
if name == 'main': n = int(input()) myset = set(map(int,input().split())) N = int(input())
for _ in range(N): cmd, *args = input().split() getattr(myset,cmd)(*map(int,args)) print(sum(myset))
If pop() removes an arbitrary item from the set, how can the answer be determinative?
set.pop is meh, so convert to list and pop consistently
I se all these solutions using command = input().split() and command[0] and command[1] to access the names and values, but where do we obtain them in the first place.
When I print s, the set only has values in them. Is it assumed that with each input() we write in our code, the user will provide an input like the example?
Isn't it very vaguely indicated in the task?
n = int(input()) s = set(map(int, input().split())) y = int(input())
for _ in range(y): command = input().split() if command[0] == 'pop': s.pop() elif command[0] == 'remove': s.remove(int(command[1])) elif command[0] == 'discard': s.discard(int(command[1]))
print(sum(s))