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

Sort by

recency

|

1099 Discussions

|

  • + 0 comments
    n = int(input())
    s = set(map(int, input().split()))
    N = int(input())
    for _ in range(N):
        inp = input()
        try:
            if inp == 'pop':
                s.pop()
            else:
                command, number = inp.split()
                if command == 'remove':
                    s.remove(int(number))
                if command == 'discard':
                    s.discard(int(number))
        except KeyError:
            continue
    
    print(sum(s))
    
  • + 0 comments

    n = int(input()) s = set(map(int, input().split())) ctr = int(input())

    for _ in range(ctr): actions = input().split() method = getattr(s, actions[0])

    if len(actions) == 1:
        method()
    else:
        method(int(actions[1]))
    

    print(sum(s))

  • + 0 comments

    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))

  • + 0 comments

    If pop() removes an arbitrary item from the set, how can the answer be determinative?

  • + 0 comments

    set.pop is meh, so convert to list and pop consistently

    n = input()
    s = set(map(int, input().split()))
    for i in range(int(input())):
        c = input().split()
        if c[0] == 'pop':
            s_list = list(s)
            s_list.pop(0)
            s = set(s_list)
        elif c[0] == 'remove':
            s.remove(int(c[1]))
        else:
            s.discard(int(c[1]))
    print(sum(s))