Set Mutations

Sort by

recency

|

849 Discussions

|

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

    _ = input() room = list(map(int,input().split())) uroom = set(room) for x in uroom: room.remove(x) print(uroom.difference(set(room)).pop())

  • + 1 comment
    _,A,N = int(input()),set(map(int,input().split())),int(input())
    operations = {
        "update" : A.update,
        "intersection_update" : A.intersection_update,
        "difference_update" : A.difference_update,
        "symmetric_difference_update" : A.symmetric_difference_update
    }
    for _ in range(N):
        opr = input().split()[0]
        B = set(map(int,input().split()))
        operations[opr]
    print(sum(A))