Set Mutations

Sort by

recency

|

843 Discussions

|

  • + 0 comments
    n= int(input())
    A= set(map(int, input().split()))
    N= int(input())
    for i in range(N):
        operation, no_of_elements = input().split()
        other_set= set(map(int, input().split()))
        getattr(A,operation)(other_set) # This has now modified set A
    print(sum(A))
    
  • + 0 comments

    set_1 = set(map(int,input().split())) b = int(input()) for i in range(b): cmd = list(input().split()) operation = cmd[0] set_2 = set(map(int, input().split())) getattr(set_1, operation)(set_2) print(sum(set_1))

  • + 0 comments

    A=int(input()) L1=set(map(int,input().split())) N=int(input()) for _ in range(N): S=input().split() L2 = set(map(int, input().split())) if S[0]=="intersection_update": L1.intersection_update(L2) elif S[0]=="update": L1.update(L2) elif S[0]=="symmetric_difference_update": L1.symmetric_difference_update(L2) elif S[0]=="difference_update": L1.difference_update(L2) print(sum(L1))

  • + 0 comments

    Here is HackerRank Set Mutations in Python solution - https://programmingoneonone.com/hackerrank-set-mutations-problem-solution-in-python.html

  • + 0 comments
    n = int(input())
    a = set(map(int, input().split()))
    m = int(input())
    
    for _ in range(m):
        operation, _ = input().split()
        other_set = set(map(int, input().split()))
        
        getattr(a, operation)(other_set)
    
    print(sum(a))