Set .discard(), .remove() & .pop()

  • + 0 comments

    **Why this works? .pop() removes an element randomly. But hackrrank assumes always min element is removed. **

    n = int(input())
    s = set(map(int, input().split()))
    N = int(input())
    
    for _ in range(N):
        cmd = input().split()
        if cmd[0] == 'pop':
            # remove the smallest item to match HackerRank's pop behavior
            s.remove(min(s))
        elif cmd[0] == 'remove':
            s.remove(int(cmd[1]))
        elif cmd[0] == 'discard':
            s.discard(int(cmd[1]))
    
    print(sum(s))