Question: Set Mutations [Python Sets]
You are given a set and number of other sets. These number of sets have to perform some specific mutation operations on set .
Your task is to execute those operations and print the sum of elements from set .
Input Format
The first line contains the number of elements in set .
The second line contains the space separated list of elements in set .
The third line contains integer , the number of other sets.
The next lines are divided into parts containing two lines each.
The first line of each part contains the space separated entries of the operation name and the length of the other set.
The second line of each part contains space separated list of elements in the other set.
0 < len(set(A))) < 1000
0 < len(otherSets) < 100
0 < N < 100
Output Format
Output the sum of elements in set .
Sample Input
16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 24 52
4
intersection_update 10
2 3 5 6 8 9 1 4 7 11
update 2
55 66
symmetric_difference_update 5
22 7 35 62 58
difference_update 7
11 22 35 55 58 62 66
Sample Output
38
Explanation
After the first operation, (intersection_update operation), we get:set A = set([1,2,3,4,5,6,7,8,9,11])
After the second operation, (update operation), we get:set A = set([1,2,3,4,5,6,7,8,9,11,55,66])
After the third operation, (symmetric_difference_update operation), we get:set A = set([1,2,3,4,5,6,8,9,11,22,35,55,58,62,66])
After the fourth operation, ( difference_update operation), we get:set A = set([1,2,3,4,5,6,8,9])
The sum of elements in set A  after these operations is 38.
If you are new to python then I would recommend reading out Python Sets before attempting this question.
Possible Solutions
1. Using Direct Set Methods
In this solution, we begin by reading the number of elements in set A
and then the elements themselves, converting them into a set of integers. We then read the number of operations to be performed using for loop. For each operation, we read the operation name and the elements of another set, applying the appropriate set method (intersection_update
, update
, symmetric_difference_update
, or difference_update
) to set A
using if else statement. Finally, we compute and print the sum of the elements in set A
.
# Read input
n = int(input())
A = set(map(int, input().split()))
num_operations = int(input())
# Perform each operation on set A
for _ in range(num_operations):
operation, _ = input().split()
other_set = set(map(int, input().split()))
if operation == "intersection_update":
A.intersection_update(other_set)
elif operation == "update":
A.update(other_set)
elif operation == "symmetric_difference_update":
A.symmetric_difference_update(other_set)
elif operation == "difference_update":
A.difference_update(other_set)
# Output the sum of elements in set A
print(sum(A))
2. Using eval
for Dynamic Method Calls
In this solution, we start by reading the number of elements in set A
and converting them into a set of integers. We also read the number of operations using for loop. For each operation, we read the operation name and the elements of another set. We then dynamically call the relevant set method using eval
, which allows us to construct and execute the method call as a string. After performing all operations, we compute and print the sum of the elements in set A
.
# Read input
n = int(input())
A = set(map(int, input().split()))
num_operations = int(input())
# Perform each operation on set A
for _ in range(num_operations):
operation, _ = input().split()
other_set = set(map(int, input().split()))
eval(f"A.{operation}({other_set})")
# Output the sum of elements in set A
print(sum(A))
3.Using a Dictionary of Operations
In this solution, we begin by reading the number of elements in set A
and then the elements themselves, converting them into a set of integers. We also read the number of operations. We define a dictionary mapping operation names to their corresponding set methods. For each operation, we read the operation name and the elements of another set, then use the dictionary to call the appropriate method on set A
. Finally, we compute and print the sum of the elements in set A
. This method organizes operations in a dictionary for a clear and efficient approach.
# Read input
n = int(input())
A = set(map(int, input().split()))
num_operations = int(input())
# Define operations in a dictionary
operations = {
"intersection_update": A.intersection_update,
"update": A.update,
"symmetric_difference_update": A.symmetric_difference_update,
"difference_update": A.difference_update
}
# Perform each operation on set A
for _ in range(num_operations):
operation, _ = input().split()
other_set = set(map(int, input().split()))
operations[operation](other_set)
# Output the sum of elements in set A
print(sum(A))
When we run the code from all three possible solutions on Hacker Rank, both the sample Test case are shown as successful:
Summary
In this tutorial we covered different methods for performing set mutations based on various operations. The first solution reads the input sets and operations, then directly applies the appropriate set method (intersection_update
, update
, symmetric_difference_update
, or difference_update
) for each operation, and prints the sum of elements in set A
. The second solution uses eval
to dynamically call the relevant set method, providing flexibility in method invocation. The third solution employs a dictionary to map operation names to their corresponding set methods, ensuring clarity and efficiency. Each approach effectively demonstrates the handling of set mutations in Python.
Further Reading
Question on Hacker Rank: Python Set Mutations [Sets]