From 64a3f5ae28b0e78afdca10bca20a36b91aa1f52b Mon Sep 17 00:00:00 2001 From: Yesenya Date: Wed, 22 Nov 2023 17:56:46 +0300 Subject: [PATCH] add update_dict task --- practica/README.md | 1 + practica/stepik/README.md | 1 + practica/stepik/stepik_tasks/README.md | 15 +++++++++++++++ practica/stepik/stepik_tasks/update_dictionary.py | 13 +++++++++++++ 4 files changed, 30 insertions(+) create mode 100644 practica/stepik/stepik_tasks/update_dictionary.py diff --git a/practica/README.md b/practica/README.md index 25161d5..5e7aad5 100644 --- a/practica/README.md +++ b/practica/README.md @@ -33,6 +33,7 @@ Among them there are: - [sum_num_nearby](stepik/stepik_tasks/sum_num_nearby.py) - [Sum of squared numbers](stepik/stepik_tasks/sum_of_squared_numbers.py) - [Tickets](stepik/stepik_tasks/tickets.py) + - [Function of updating dictionary](stepik/stepik_tasks/update_dictionary.py) diff --git a/practica/stepik/README.md b/practica/stepik/README.md index 0325d9a..8bab473 100644 --- a/practica/stepik/README.md +++ b/practica/stepik/README.md @@ -28,5 +28,6 @@ different tasks from various courses - [sum_num_from_last](stepik_tasks/sum_num_from_last.py) - [sum_num_nearby](stepik_tasks/sum_num_nearby.py) - [Tickets](stepik_tasks/tickets.py) +- [Function of updating dictionary](stepik_tasks/update_dictionary.py) \ No newline at end of file diff --git a/practica/stepik/stepik_tasks/README.md b/practica/stepik/stepik_tasks/README.md index a7db23b..bf297d5 100644 --- a/practica/stepik/stepik_tasks/README.md +++ b/practica/stepik/stepik_tasks/README.md @@ -61,3 +61,18 @@ __*Plan of solution*__: - [Tickets](tickets.py) A program that issues a “Lucky” or "regular" ticket, the number of which we entered. If all the numbers are the same - the ticket is lucky + +- [Function of updating dictionary](update_dictionary.py) + + Function that searches the key in dictionary and in case key is exist append value, if it doesnt exist - searches key* 2 and update, if key* 2 doesnt exist too, so add it to the dictionary with value. + + The coorect output: + ```python + d = {} + print(update_dictionary(d, 1, -1)) # None + print(d) # {2: [-1]} + update_dictionary(d, 2, -2) + print(d) # {2: [-1, -2]} + update_dictionary(d, 1, -3) + print(d) # {2: [-1, -2, -3]} + ``` diff --git a/practica/stepik/stepik_tasks/update_dictionary.py b/practica/stepik/stepik_tasks/update_dictionary.py new file mode 100644 index 0000000..2552364 --- /dev/null +++ b/practica/stepik/stepik_tasks/update_dictionary.py @@ -0,0 +1,13 @@ +# function of update dict d +d = {} +def update_dictionary(d,key,value): + if key not in d: + new_key = int(key) *2 + if new_key not in d: + d[new_key] = [value] + else: + d[new_key] += [value] + else: + d[key] += [value] + +