Set Mutations

Sort by

recency

|

851 Discussions

|

  • + 0 comments

    For Python3 Platform

    n_A = int(input())
    A = set(map(int, input().split()))
    N = int(input())
    for _ in range(N):
        cmd, len = input().split()
        B = set(map(int, input().split()))
        
        if(cmd == "intersection_update"):
            A.intersection_update(B)
        elif(cmd == "update"):
            A |= B
        elif(cmd == "difference_update"):
            A.difference_update(B)
        elif(cmd == "symmetric_difference_update"):
            A ^= B
    
    print(sum(A))
    
  • + 0 comments
    n = int(input())
    arr1 = set(map(int, input().split()))
    m = int(input())
    for _ in range(m):
        key, value = input().split()
        arr2 = set(map(int, input().split()))
        getattr(arr1, key)(arr2)
    print(sum(arr1))
    
  • + 0 comments

    number_element = int(input()) number_element_spaced = set(map(int, input().split())) numbers_of_other_sets = int(input())

    for _ in range(numbers_of_other_sets): operations , no_elements = input().split() other_sets = set(map(int, input().split())) getattr(number_element_spaced,operations)(other_sets)

    print(sum(number_element_spaced))

  • + 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))
        
    
  • + 0 comments
    _,A = int(input()),set(map(int,input().split()))
    operations = {
        "update" : A.update,
        "intersection_update" : A.intersection_update,
        "difference_update" : A.difference_update,
        "symmetric_difference_update" : A.symmetric_difference_update
    }
    for _ in range(int(input())):
        opr = input().split()[0]
        B = set(map(int,input().split()))
        operations[opr](B)
    print(sum(A))