From 069db3fe9a0801a6093df882e166567dc27842a9 Mon Sep 17 00:00:00 2001 From: Bohdanboiko Date: Tue, 21 Jul 2020 16:21:06 +0300 Subject: [PATCH] BohdanBoiko HM5" --- python_dict.py | 50 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/python_dict.py b/python_dict.py index 7fbc7fc..ec72208 100644 --- a/python_dict.py +++ b/python_dict.py @@ -1,14 +1,16 @@ + """ 1. Sum Values Write a function named sum_values that takes a dictionary named my_dictionary as a parameter. The function should return the sum of the values of the dictionary """ # Write your sum_values function here: - +def sum_values(my_dictionary): +return sum(my_dictionary.values()) # Uncomment these function calls to test your sum_values function: -# print(sum_values({"milk":5, "eggs":2, "flour": 3})) + print(sum_values({"milk":5, "eggs":2, "flour": 3})) # should print 10 -# print(sum_values({10:1, 100:2, 1000:3})) + print(sum_values({10:1, 100:2, 1000:3})) # should print 6 @@ -20,11 +22,16 @@ # Write your sum_even_keys function here: - +def sum_even_keys(my_dictionary): +even_sum = 0 +for key, value in my_dictionary.items(): +if key % 2 == 0: +even_sum += value +return even_sum # Uncomment these function calls to test your function: -# print(sum_even_keys({1:5, 2:2, 3:3})) + print(sum_even_keys({1:5, 2:2, 3:3})) # should print 2 -# print(sum_even_keys({10:1, 100:2, 1000:3})) + print(sum_even_keys({10:1, 100:2, 1000:3})) # should print 6 @@ -34,11 +41,12 @@ The function should add 10 to every value in my_dictionary and return my_dictionary """ # Write your add_ten function here: - +def add_ten(my_dictionary): +return {key: item+10 for key, item in my_dictionary.items()} # Uncomment these function calls to test your function: -# print(add_ten({1:5, 2:2, 3:3})) + print(add_ten({1:5, 2:2, 3:3})) # should print {1:15, 2:12, 3:13} -# print(add_ten({10:1, 100:2, 1000:3})) + print(add_ten({10:1, 100:2, 1000:3})) # should print {10:11, 100:12, 1000:13} @@ -48,11 +56,18 @@ This function should return a list of all values in the dictionary that are also keys. """ # Write your values_that_are_keys function here: - +def values_that_are_keys(my_dictionary) +lst = [] +for val in my_dictionary.values(); +for key in my_dictionary.keys(): +if val == key: +lst.append(val) +break +return lst # Uncomment these function calls to test your function: -# print(values_that_are_keys({1:100, 2:1, 3:4, 4:10})) + print(values_that_are_keys({1:100, 2:1, 3:4, 4:10})) # should print [1, 4] -# print(values_that_are_keys({"a":"apple", "b":"a", "c":100})) + print(values_that_are_keys({"a":"apple", "b":"a", "c":100})) # should print ["a"] @@ -62,9 +77,14 @@ The function should return the key associated with the largest value in the dictionary. """ # Write your max_key function here: - +def max_key(my_dictionary): +return max(my_dictionary, key=my_dictionary.get) # Uncomment these function calls to test your function: -# print(max_key({1:100, 2:1, 3:4, 4:10})) + print(max_key({1:100, 2:1, 3:4, 4:10})) # should print 1 -# print(max_key({"a":100, "b":10, "c":1000})) + print(max_key({"a":100, "b":10, "c":1000})) # should print "c" + + + +