arr1 arr2 arr3 string_arr abracadabra somethingnewforus testandtestaga

  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
arr1 = [6, 5, 3, 5, 9, 5, 4, 4, 3, 2, 7, 3, 6, 7, 2]
arr2 = [7, 7, 4, 2, 8, 9, 2, 6, 4, 8, 8, 5, 7, 3, 2]
arr3 = [4, 6, 4, 4, 6, 5, 7, 9, 6, 6, 5, 7, 5, 5, 3]
string_arr = ["abracadabra", "somethingnewforus", "testandtestagain"]
dict1 = {2: 8, 5: 11, 7: 3, 1: 5, 45: 14}
dict2 = {3: 13, 5: 10, 8: 2, 1: 3, 45: 8, 4: 14}
words1 = ["q", "qq", "w", "w", "ww", "as"]
words2 = ["a", "q", "qq", "qq", "ww"]
# Exercise 1
def count_numbers(arr):
counts = {}
for i in arr:
if i in counts:
counts[i] += 1
else:
counts[i] = 1
return counts
print('Exercise 1:')
print(count_numbers(arr1))
print(count_numbers(arr2))
# Exercise 2
def last_index(arr):
indexes = {}
for i in range(0, len(arr)):
indexes[arr[i]] = i
return indexes
print('Exercise 2:')
print(last_index(arr2))
print(last_index(arr3))
# Exercise 3
def all_indexes(arr):
all_ind = {}
for i in range(0, len(arr)):
if arr[i] in all_ind:
all_ind[arr[i]].append(i)
else:
all_ind[arr[i]] = [i]
return all_ind
print('Exercise 3:')
print(all_indexes(arr3))
# Exercise 4
def count_letters(arr):
arr_letters = []
for word in arr:
letters = {}
for let in word:
if let in letters:
letters[let] += 1
else:
letters[let] = 1
arr_letters.append(letters)
return arr_letters
print('Exercise 4:')
print(count_letters(string_arr))
# Exercise 5
def add_dicts(d1, d2):
for i in d2:
if i in d1:
d1[i] += d2[i]
else:
d1[i] = d2[i]
return d1
print('Exercise 5:')
print(add_dicts(dict1, dict2))
# Exercise 6
def words_difference(w1, w2):
diff = {}
res = []
for i in w1:
if i in diff:
diff[i] += 1
else:
diff[i] = 1
for i in w2:
if i in diff:
diff[i] -= 1
for i in diff:
if diff[i] > 0:
res.append(i)
return res
print('Exercise 6:')
print (words_difference(words1, words2))