Header Ad

HackerRank Set Mutations problem solution in python

In this Set mutations problem, You are given a set A and N number of other sets. These N number of sets have to perform some specific mutation operations on set A. Your task is to execute those operations and print the sum of elements from set A.


HackerRank Set Mutations solution in python


Problem solution in Python 2 programming.

# Enter your code here. Read input from STDIN. Print output to STDOUT
raw_input()
s = set(map(int,raw_input().split()))
n = int(raw_input())
for _ in range(n):
    exec "s." + raw_input().split()[0] + "(map(int,set(raw_input().split())))"
print sum(s)


Problem solution in Python 3 programming.

# Enter your code here. Read input from STDIN. Print output to STDOUT

if __name__ == '__main__':
    (_, A) = (int(input()),set(map(int, input().split())))
    B = int(input())
    for _ in range(B):
        (command, newSet) = (input().split()[0],set(map(int, input().split())))
        getattr(A, command)(newSet)

    print (sum(A))




Problem solution in pypy programming.

# Enter your code here. Read input from STDIN. Print output to STDOUT
(_, A) = (
    raw_input(),
    set(map(int, raw_input().split()))
)

for _ in xrange(input()):
    (command, newSet) = (
        raw_input().split()[0],
        set(map(int, raw_input().split()))
    )

    getattr(A, command)(newSet)

print sum(A)


Problem solution in pypy3 programming.

# Enter your code here. Read input from STDIN. Print output to STDOUT
n = input()
s = set(map(int, input().split())) 

for _ in range(int(input())):
    args = input().strip().split()
    nw= set(map(int, input().split())) 
    eval("s." + args[0] + "(  nw  )")
print(sum(s))


Post a Comment

0 Comments