Yevhenii Mianovskyi - correctiomns

 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
#що буде якщо треба знайти середнє - а масив порожній
#my_list = [1, 3, 5, 6, 8]
#sum = 0
#for item in my_list:
# sum += item
#average = sum / len(my_list)
#print(average)
# передбачив exception щоб уникнути ділення на нуль
my_list = [1, 3, 5, 6, 8]
total = 0
for item in my_list:
total += item
try:
average = total / len(my_list)
print(average)
except:
print('The list is empty')
##############
#my_list = [1, 3, 5, 6, 8]
#multiplied = []
#for item in my_list:
# if my_list.index(item) % 2 == 0:
# item = item * 2
# else:
# item = item * 3
# multiplied.append(item)
#print(multiplied)
#
#спробуй для масиву [1, 2, 3, 4, 2, 1, 3, 2, 4, 1, 3]
# замінив індекс на змінну position
my_list = [1, 2, 3, 4, 2, 1, 3, 2, 4, 1, 3]
multiplied = []
position = 0
for item in my_list:
if position % 2 == 0:
item = item * 2
elif position % 2 != 0:
item = item * 3
multiplied.append(item)
position += 1
print(multiplied)
#####################
#from collections import Counter #трошки чітерства
#dict1 = {2: 8, 5: 11, 7: 3, 1: 5, 45: 14}
#dict2 = {3: 13, 5: 10, 8: 2, 1: 3, 45: 8, 4: 14}
#dict3 = Counter(dict1) + Counter(dict2)
#print(dict3)
# а якщо руками а не з бібліотеками
dict1 = {2: 8, 5: 11, 7: 3, 1: 5, 45: 14}
dict2 = {3: 13, 5: 10, 8: 2, 1: 3, 45: 8, 4: 14}
dict3 = {}
for key in dict1.keys():
if key in dict2.keys(): # sum of the intersection of two dicts
dict3[key] = dict1[key] + dict2[key]
else: # appending items in dict1 that are missing in dict2
dict3[key] = dict1[key]
for key in dict2.keys(): # appending missing items from dict 2
if key not in dict3.keys():
dict3[key] = dict2[key]
print(dict3)