HackerRank Python - Set Mutations




We have seen the applications of union, intersection, difference and symmetric difference operations, but these operations do not make any changes or mutations to the set.

We can use the following operations to create mutations to a set:

.update() or |=
Update the set by adding elements from an iterable/another set.



  1. # Enter your code here. Read input from STDIN. Print output to STDOUT
  2. n = int(input())
  3. a = set(map(int,input().split()))
  4. N = int(input())
  5. for i in range(N):
  6. cmd = input().split()
  7. opt = cmd[0]
  8. s = set(map(int,input().split()))
  9. if (opt == 'update'):
  10. a |= s
  11. elif (opt == 'intersection_update'):
  12. a &= s
  13. elif (opt == 'difference_update'):
  14. a -= s
  15. elif (opt == 'symmetric_difference_update'):
  16. a ^= s
  17. print(sum(a))




codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube