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
|
1077 Discussions
|
Please Login in order to post a comment
Why this works? .pop() removes an element randomly. But hackrrank assumes always min element is removed.
Why would this not work:
import sys
def solution(n, values, lines, N): for i in range(N): if lines[i][0] == "d": values.discard(int(lines[i][-1])) elif lines[i][0] == "r": values.remove(int(lines[i][-1])) else: values.pop()
if name=="main": n = int(input()) values = set(map(int, input().split())) N = int(input())
This code works just fine..
n = int(input()) S = set(map(int,input().split())) N = int(input()) arr = [] for i in range(N): command = list(input().split()) if command[0] == 'pop': S.discard(min(S)) else: S.discard(int(command[1])) print(sum(S))