Set Mutations

Sort by

recency

|

837 Discussions

|

  • + 0 comments

    For Python3 Platform

    Na = int(input())
    A = set(map(int, input().split()))
    s = int(input())
    for i in range(s):
        cmd = input().split()
        B = map(int, input().split())
        
        if(cmd[0] == "intersection_update"):
            A.intersection_update(B)
        elif(cmd[0] == "update"):
            A.update(B)
        elif(cmd[0] == "symmetric_difference_update"):
            A.symmetric_difference_update(B)
        else:
            A.difference_update(B)
    
    print(sum(A))
    
  • + 0 comments

    ![](# Enter your code here. Read input from STDIN. Print output to STDOUT

    n = int(input())

    set1 = set(map(int,input().split()))

    choice = int(input())

    for _ in range(choice): op1, size = input().split() other_set = set(map(int, input().split()))

    if op1 == 'intersection_update':
        set1.intersection_update(other_set)
    elif op1 == 'update':
        set1.update(other_set)
    elif op1 == 'symmetric_difference_update':
        set1.symmetric_difference_update(other_set)
    elif op1 == 'difference_update':
        set1.difference_update(other_set)
    

    print(sum(set1))https://)

  • + 0 comments

    #What separates my solution is the usage of eval function: which can dynamically execute any text representing a particular function. Most solutions set a conditional logic to determine which function to run.

    A_len = int(input()) A_set = set( map(int, input().split(" ")) )

    N_len = int(input()) for n in range(N_len): #iteration of 0 to Nth X_str = input().split(" ") X_fx_str = X_str[0] X_set =set( map(int, input().split(" ")) )
    eval( f"A_set.{X_fx_str}(X_set)" ) #This is where the eval function used

    print(sum(A_set))

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

  • + 0 comments

    N = int(input()) a = set(map(int,input().split()))
    b = int(input()) for _ in range(b): operation = input().split() #print(operation) otherset = set(map(int,input().split())) #print(otherset) if len(otherset) == int(operation[1]): if operation[0] == "update": a.update(otherset)
    #print(a) elif operation[0] == "intersection_update": a.intersection_update(otherset) #print(a) elif operation[0] == "difference_update": a.difference_update(otherset) #print(a) elif operation[0] == "symmetric_difference_update": a.symmetric_difference_update(otherset) #print(a) else: print("Plese enter according to the Number") print(sum(a))