Set Mutations

  • + 1 comment
    A = int(input())
    set_A = set(map(int,input().split()))
    B = int(input())
    
    for _ in range(B):
    
        command = list(map(str, input().split()))
        set_B = set(map(int,input().split()))
        
        if command[0].lower() == 'intersection_update':
            set_A &= set_B
        elif command[0].lower() == 'update':
            set_A |= set_B
        elif command[0].lower() == 'symmetric_difference_update':
            set_A ^= set_B
        elif command[0].lower() == 'difference_update':
            set_A -= set_B
            
    
    print(sum(set_A))
    
    • + 0 comments

      for _ in range(N): command = input().split() if command[0] == 'update': A.update(set(map(int, input().split()))) elif command[0] == 'intersection_update': A.intersection_update(set(map(int, input().split()))) elif command[0] == 'difference_update': A.difference_update(set(map(int, input().split()))) elif command[0] == 'symmetric_difference_update': A.symmetric_difference_update(set(map(int, input().split())))

      print(sum(A))