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

  • + 2 comments

    Here is a more instructive code example.

    # https://www.hackerrank.com/challenges/py-set-discard-remove-pop/problem?h_r=next-challenge&h_v=zen&h_r=next-challenge&h_v=zen&h_r=next-challenge&h_v=zen&h_r=next-challenge&h_v=zen
    
    
    n = int(input())
    # 9
    
    s = set(map(int, input().split()))
    # 1 2 3 4 5 6 7 8 9
    
    methods = {
        'pop' : s.pop,
        'remove' : s.remove,
        'discard' : s.discard
    }
    
    args = None
    for _ in range(int(input())):
    # 5
        method, *args = input().split()
    
        if len(args) > 1:
            [methods[method](*map(int, arg)) for arg in args]
        else:
            methods[method](*map(int, args))
            
    # pop
    # {2, 3, 4, 5, 6, 7, 8, 9}
    # pop
    # {3, 4, 5, 6, 7, 8, 9}
    # discard 1 2 3
    # {4, 5, 6, 7, 8, 9}
    # remove 5 7 9
    # {4, 6, 8}
    # pop
    # {6, 8}
    
    print(sum(s))
    # 14