-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule_8.py
More file actions
359 lines (277 loc) · 9.09 KB
/
module_8.py
File metadata and controls
359 lines (277 loc) · 9.09 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# 8. Множества
"""
Модуль посвящен множествам в Python. Рассматриваются математические основы,
операции над множествами, методы, генераторы множеств, а также неизменяемый
тип frozenset и визуализация с помощью диаграмм Эйлера – Венна.
"""
# 8.1 Множества в математике
# 8.2 Операции над множествами, диаграммы Эйлера-Венна
def m_8_2_1(): # Тимур и его команда
n, m, k, x, y, z = [int(input()) for _ in range(6)]
print(n + m + k - x - y + z)
def m_8_2_2(): # Книги на прочтение
n, m, k, x, y, z, t, a = [int(input()) for _ in range(8)]
B1, B2, B3 = (
n,
m,
k,
)
b1or2, b2or3, b3or1 = x, y, z
b123 = t
b12 = B1 + B2 - b1or2 - (b123)
b23 = B2 + B3 - b2or3 - (b123)
b31 = B3 + B1 - b3or1 - (b123)
b1 = B1 - (b12 + b31 + b123)
b2 = B2 - (b12 + b23 + b123)
b3 = B3 - (b23 + b31 + b123)
print(b1 + b2 + b3) # прочитали только одну книгу
print(b12 + b23 + b31) # прочитали только две книги
print(
a - (b1 + b2 + b3 + b12 + b23 + b31 + b123)
) # не прочитали ни одной из рекомендованных книг
# 8.3 Введение в множества в Python
# 8.4 Основы работы с множествами
def m_8_4_1():
numbers = {1.414, 12.5, 3.1415, 2.718, 9.8, 1.414, 1.1618, 1.324, 2.718, 1.324}
res = min(numbers) + max(numbers)
print(res)
def m_8_4_2():
numbers = {
20,
6,
8,
18,
18,
2,
4,
6,
8,
10,
12,
14,
16,
18,
20,
12,
8,
8,
10,
4,
2,
2,
2,
16,
20,
}
average = sum(numbers) / len(numbers)
print(average)
def m_8_4_3():
numbers = {
9089,
-67,
-32,
1,
78,
23,
-65,
99,
9089,
34,
-32,
0,
-67,
1,
11,
111,
111,
1,
23,
}
print(sum(x**2 for x in numbers))
def m_8_4_4():
fruits = {
"apple",
"banana",
"cherry",
"avocado",
"pineapple",
"apricot",
"banana",
"avocado",
"grapefruit",
}
print(*sorted(fruits, reverse=True), sep="\n")
def m_8_4_5(): # Количество различных символов
s = input()
print(len(set(s)))
def m_8_4_6(): # Неповторимые цифры
s = input()
print("YES" if len(s) == len(set(s)) else "NO")
def m_8_4_7(): # Все 10 цифр
s1, s2 = input(), input()
ref = [str(i) for i in range(10)]
print("YES" if sorted(set(s1 + s2)) == ref else "NO")
def m_8_4_8(): # Одинаковые наборы
s1, s2 = input(), input()
print("YES" if sorted(set(s1)) == sorted(set(s2)) else "NO")
def m_8_4_9(): # Три слова
s1, s2, s3 = input().split()
print(
"YES"
if sorted(set(s1)) == sorted(set(s2)) and sorted(set(s1)) == sorted(set(s3))
else "NO"
)
# 8.5 Методы множеств. Часть 1
def m_8_5_1(): # Уникальные символы 1
data = [input().lower() for _ in range(int(input()))]
for d in data:
print(len(set(d)))
def m_8_5_2(): # Уникальные символы 2
data = [input().lower() for _ in range(int(input()))]
print(len(set("".join(data))))
def m_8_5_3(): # Количество слов в тексте
text = input().lower()
for i in ".,;:-?!":
text = text.replace(i, "")
print(len(set(text.split())))
def m_8_5_4(): # Встречалось ли число раньше?
nums = map(int, input().split())
chek = set()
for i in nums:
if i in chek:
print("YES")
else:
print("NO")
chek.add(i)
def m_8_5_5(): # Счетчик верных решений
count = int(input())
users = set()
res = 0
for _ in range(count):
user = input().split(": ")
if user[-1] == "Correct":
users.add(user[0])
res += 1
if res == 0:
print("Вы можете стать первым, кто решит эту задачу")
else:
res = int(res * 1000 / count)
print(
f"Верно решили {len(users)} учащихся\nИз всех попыток {res//10 + (1 if res % 10 >= 5 else 0)}% верных"
)
# 8.6 Методы множеств. Часть 2
def m_8_6_1(): # Количество совпадающих
print(len(set(input().split()) & set(input().split())))
def m_8_6_2(): # Общие числа
print(*sorted(set(map(int, input().split())) & set(map(int, input().split()))))
def m_8_6_3(): # Числа первой строки
print(*sorted(set(map(int, input().split())) - set(map(int, input().split()))))
def m_8_6_4(): # Общие цифры
n = int(input())
nums = [set(int(i) for i in input()) for _ in range(n)]
res = nums[0]
for i in nums[1:]:
res &= i
print(*sorted(res))
# 8.7 Методы множеств. Часть 3
def m_8_7_1(): # Одинаковые цифры
num1, num2 = set(input()), input()
print(("YES", "NO")[num1.isdisjoint(num2)])
def m_8_7_2(): # Все цифры
num1, num2 = input(), set(input())
print(("NO", "YES")[num2.issubset(num1)])
def m_8_7_3(): # Урок информатики
rat1, rat2, rat3 = (set(map(int, input().split())) for _ in range(3))
print(*sorted(rat1 & rat2 - rat3, reverse=True))
def m_8_7_4(): # Урок математики
rat1, rat2, rat3 = (set(map(int, input().split())) for _ in range(3))
print(*sorted((rat1 | rat2 | rat3) - (rat1 & rat2 & rat3)))
def m_8_7_5(): # Урок физики
rat1, rat2, rat3 = (set(map(int, input().split())) for _ in range(3))
print(*sorted(rat3 - (rat1 | rat2), reverse=True))
def m_8_7_6(): # Урок биологии
rat1, rat2, rat3 = (set(map(int, input().split())) for _ in range(3))
print(*sorted(set(range(11)) - (rat3 | rat1 | rat2)))
# 8.8 Генераторы множеств и frozenset
def m_8_8_1():
items = [
10,
"30",
30,
10,
"56",
34,
"12",
90,
89,
34,
45,
"67",
12,
10,
90,
23,
"45",
56,
"56",
1,
5,
"6",
5,
]
print(*sorted({int(i) for i in items}))
def m_8_8_2():
words = [
"Plum",
"Grapefruit",
"apple",
"orange",
"pomegranate",
"Cranberry",
"lime",
"Lemon",
"grapes",
"persimmon",
"tangerine",
"Watermelon",
"currant",
"Almond",
]
print(*sorted({i[0].lower() for i in words}))
def m_8_8_3():
sentence = """My very photogenic mother died in a freak accident (picnic, lightning) when I was three, and, save for a pocket of warmth in the darkest past, nothing of her subsists within the hollows and dells of memory, over which, if you can still stand my style (I am writing under observation), the sun of my infancy had set: surely, you all know those redolent remnants of day suspended, with the midges, about some hedge in bloom or suddenly entered and traversed by the rambler, at the bottom of a hill, in the summer dusk; a furry warmth, golden midges."""
words = set(
sentence.translate(str.maketrans("", "", ".,;:!?()'\"")).lower().split()
)
print(*sorted(words))
def m_8_8_4():
sentence = """My very photogenic mother died in a freak accident (picnic, lightning) when I was three, and, save for a pocket of warmth in the darkest past, nothing of her subsists within the hollows and dells of memory, over which, if you can still stand my style (I am writing under observation), the sun of my infancy had set: surely, you all know those redolent remnants of day suspended, with the midges, about some hedge in bloom or suddenly entered and traversed by the rambler, at the bottom of a hill, in the summer dusk; a furry warmth, golden midges."""
words = {
w.strip(".,;:!?()'\"").lower()
for w in sentence.split()
if len(w.strip(".,;:!?()'\"")) < 4
}
print(*sorted(words))
def m_8_8_5():
files = [
"python.png",
"qwerty.py",
"stepik.png",
"beegeek.org",
"windows.pnp",
"pen.txt",
"phone.py",
"book.txT",
"board.pNg",
"keyBoard.jpg",
"Python.PNg",
"apple.jpeg",
"png.png",
"input.tXt",
"split.pop",
"solution.Py",
"stepik.org",
"kotlin.ko",
"github.git",
]
print(*sorted({i.lower() for i in files if i.lower()[-3:] == "png"}))