add update_dict task

This commit is contained in:
Yesenya 2023-11-22 17:56:46 +03:00
parent 5a4a11fc12
commit 64a3f5ae28
4 changed files with 30 additions and 0 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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]}
```

View File

@ -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]