-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathset_mutations.py
121 lines (93 loc) · 3.07 KB
/
set_mutations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
"""
Submissions
Leaderboard
Discussions
Editorial
Problem Statement
We have seen the applications of union, intersection, difference and symmetric difference operations, but these operations did not make any changes or mutation to the set.
We can use the following operations to create mutations to set :
.update() or |=
Update the set, adding elements from an iterable/another set.
>>> H = set("Hacker")
>>> R = set("Rank")
>>> H.update(R)
>>> print H
set(['a', 'c', 'e', 'H', 'k', 'n', 'r', 'R'])
.intersection_update() or &=
Update the set, keeping only elements found in it and an iterable/another set.
>>> H = set("Hacker")
>>> R = set("Rank")
>>> H.intersection_update(R)
>>> print H
set(['a', 'k'])
.difference_update() or -=
Update the set, removing elements found in an iterable/another set.
>>> H = set("Hacker")
>>> R = set("Rank")
>>> H.difference_update(R)
>>> print H
set(['c', 'e', 'H', 'r'])
.symmetric_difference_update() or ^=
Update the set, keeping only elements found in either set, but not in both.
>>> H = set("Hacker")
>>> R = set("Rank")
>>> H.symmetric_difference_update(R)
>>> print H
set(['c', 'e', 'H', 'n', 'r', 'R'])
TASK
You are given a set A and N numbers of other sets. These N sets have to perform some specific mutation operations to set A.
Your task is to execute those operations and print the sum of elements of set A.
Input Format
First line contains, number of elements in set A.
Second line contains, space separated list of elements of set A.
Third line contains, N, number of other sets.
Next 2*N lines are divided into N parts of two lines each.
First line of each part contains, space separated entries of operation name and length of other set.
Second line of each part contains, space separated list of elements of other set.
0 < len(set(A)) < 1000
0 < len(otherSets) < 100
0 < N < 100
Output Format
Output the sum of elements of set A.
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 first operation, i.e., intersection_update operation, we get :
set A = set([1, 2, 3, 4, 5, 6, 7, 8, 9, 11])
After second operation, i.e., update operation, we get :
set A = set([1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 55, 66])
After third operation, i.e., 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 fourth operation, i.e., difference_update operation, we get :
set A = set([1, 2, 3, 4, 5, 6, 8, 9])
Sum of elements of set A after these operations is 38.
"""
LA = input()
A = set(map(int,raw_input().split()))
N = input()
for i in xrange(N):
k = raw_input().split()
ktemp = set(map(int,raw_input().split()))
if k[0] == 'update':
A.update(ktemp)
elif k[0] == 'intersection_update':
A.intersection_update(ktemp)
elif k[0] == 'difference_update':
A.difference_update(ktemp)
elif k[0] == 'symmetric_difference_update':
A.symmetric_difference_update(ktemp)
else:
print 'Incorrect parameters'
print A