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
|
1066 Discussions
|
Please Login in order to post a comment
n = int(input()) s = set(map(int, input().split()))
N = int(input())
for _ in range(N): command = input() if command == 'pop': s.pop() if command.startswith('remove'): s.remove(int(command.split()[1])) if command.startswith('discard'): s.discard(int(command.split()[1]))
print(sum(s))
n = int(input()) s = set(map(int, input().split())) N = int(input()) for i in range(N): a, *b = input().split() getattr(s, a)(*map(int,b)) print(sum(s))
This works in Python3
n = int(input()) s = set(map(int, input().split())) N = int(input())
for _ in range(N): command = input().split()
print(sum(s))
for those wondering why their code doesn't work, is all because how pop() method is handled in both Python3 and PyPy3 (the default version of python in hackerrank). in Python3 it deletes the last index. in PyPy3, pop() method deletes the first number. all you gotta do i switch to python3 and it will work <3.
for those wondering why their code doesn't work, is all because how pop() method is handled in both Python3 and PyPy3 (the default version of python in hackerrank). in Python3 it deletes the last index. in PyPy3, pop() method deletes the first number. all you gotta do i switch to python3 and it will work <3.