forked from mate-academy/py_dictionaries
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdictles5
51 lines (38 loc) · 1.17 KB
/
dictles5
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
# 1
def sum_values(my_dictionary):
return sum(my_dictionary.values())
print(sum_values({"milk": 5, "eggs": 2, "flour": 3}))
print(sum_values({10: 1, 100: 2, 1000: 3}))
#2
def sum_even_keys(my_dictionary):
even = 0
for key, value in my_dictionary.items():
if key % 2 == 0:
even += value
return even
print(sum_even_keys({1:5, 2:2, 3:3}))
print(sum_even_keys({10:1, 100:2, 1000:3}))
#3
def add_ten(my_dictionary):
for key, value in my_dictionary.items():
if key in my_dictionary:
my_dictionary[key] = value + 10
return my_dictionary
print(add_ten({1:5, 2:2, 3:3}))
print(add_ten({10:1, 100:2, 1000:3}))
#4
def values_that_are_keys(my_dictionary):
list = []
for key in my_dictionary:
if key in my_dictionary.values():
list.append(key)
return list
print(values_that_are_keys({1:100, 2:1, 3:4, 4:10}))
print(values_that_are_keys({"a":"apple", "b":"a", "c":100}))
#5
def max_key(my_dictionary):
for key, value in my_dictionary.items():
if value >= max(my_dictionary.values()):
return key
print(max_key({1:100, 2:1, 3:4, 4:10}))
print(max_key({"a":100, "b":10, "c":1000}))