Set Mutations

  • + 0 comments

    Since the commands are the exact name of the method calls on the Set you can use python's getattr function as shown below. NOTE - do not do this in production code unless you are guaranteed that the method calls are correct!!

    input() # throwaway
    A = set(map(int, input().split()))
    
    N = int(input())
    
    for _ in range(N):
        cmd = input().split() #throwaway cmd[1]
        s = set(map(int, input().split()))
        # get the cmd[0] method from A and call with param s
        getattr(A,cmd[0])(s)
        
    print(sum(A))