-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathPython04_Sets
More file actions
183 lines (131 loc) · 3.83 KB
/
Python04_Sets
File metadata and controls
183 lines (131 loc) · 3.83 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
## Python
## Sets
## Introdduction to Sets
def average(array):
# your code goes here
array_set = set(array)
return sum(array_set) / len(array_set)
if __name__ == '__main__':
n = int(input())
arr = list(map(int, input().split()))
result = average(arr)
print(result)
## No Idea!
# Enter your code here. Read input from STDIN. Print output to STDOUT
(n, m), arr_list, A, B = [input().split() for _ in range(4)]
A, B = set(A), set(B) # for time efficiency
happiness = 0
for i in arr_list:
if i in A:
happiness += 1
elif i in B:
happiness -= 1
print(happiness)
## Symmetric Difference
# Enter your code here. Read input from STDIN. Print output to STDOUT
m = int(input())
set_m = set(map(int, input().split()))
n = int(input())
set_n = set(map(int, input().split()))
new_set = set_m.difference(set_n)
temp = set_n.difference(set_m)
new_set.update(temp)
new_set = sorted(new_set)
for e in new_set:
print(e)
## Set .add()
# Enter your code here. Read input from STDIN. Print output to STDOUT
n = int(input())
countries = set()
for _ in range(n):
countries.add(input())
print(len(countries))
## Set .discard(), .remove() & .pop()
n = int(input())
s = set(map(int, input().split()))
N = int(input())
for _ in range(N):
command = input().split()
if command[0] == 'pop':
s.pop()
elif command[0] == 'remove':
s.remove(int(command[1]))
elif command[0] == 'discard':
s.discard(int(command[1]))
total = sum(s)
print(total)
## Set .union() Operation
# Enter your code here. Read input from STDIN. Print output to STDOUT
n = int(input())
set_n = set(map(int, input().split()))
b = int(input())
set_b = set(map(int, input().split()))
new_set = set_n.union(set_b)
print(len(new_set))
## Set .intersection() Operation
# Enter your code here. Read input from STDIN. Print output to STDOUT
n = int(input())
set_n = set(map(int, input().split()))
b = int(input())
set_b = set(map(int, input().split()))
new_set = set_n.intersection(set_b)
print(len(new_set))
## Set .difference() Operation
# Enter your code here. Read input from STDIN. Print output to STDOUT
n = int(input())
set_n = set(map(int, input().split()))
b = int(input())
set_b = set(map(int, input().split()))
new_set = set_n.difference(set_b)
print(len(new_set))
## Set .symmetric_difference() Operation
# Enter your code here. Read input from STDIN. Print output to STDOUT
n = int(input())
set_n = set(map(int, input().split()))
b = int(input())
set_b = set(map(int, input().split()))
new_set = set_n.difference(set_b)
temp = set_b.difference(set_n)
new_set.update(temp)
print(len(new_set))
## Set Mutations
# Enter your code here. Read input from STDIN. Print output to STDOUT
n = int(input())
set_A = set(map(int,input().split()))
N = int(input())
for _ in range(N):
operation = input().split()
new_set = set(map(int,input().split()))
eval('set_A.{}({})'.format(operation[0], new_set))
print(sum(set_A))
## The Captain's Room
# Enter your code here. Read input from STDIN. Print output to STDOUT
from collections import Counter
n = int(input())
room_list = list(map(int, input().split()))
freq = Counter(room_list)
for room in freq.keys():
if freq[room] == 1:
print(room)
break
## Check Subset
# Enter your code here. Read input from STDIN. Print output to STDOUT
N = int(input())
for _ in range(N):
n1 = int(input())
set_a = set(map(int, input().split()))
n2 = int(input())
set_b = set(map(int, input().split()))
print(set_a.intersection(set_b) == set_a)
## Check Strict Superset
# Enter your code here. Read input from STDIN. Print output to STDOUT
set_a = set(map(int, input().split()))
N = int(input())
for _ in range(N):
new_set = set(map(int, input().split()))
if (new_set < set_a):
continue
else:
break
print(new_set < set_a)
## end ##