Browse Source

docs(merge_two): use stdin to generate the second dictionary

pull/1779/head
Michele Sessa 2 years ago committed by Michele
parent
commit
ada74ada7b
  1. 33
      subjects/devops/merge_two/README.md

33
subjects/devops/merge_two/README.md

@ -4,12 +4,15 @@ One very useful data structure in Python are the dictionaries, in this exercise
### Instructions ### Instructions
Create a file `merge_two.py` which will have a function named `merge_two()`. This function will accept two dictionaries, create a third one which will be the merge of the two inputs and return it as a serialized JSON string. Create a file `merge_two.py` which will have a function named `merge_two()`. This function will accept one dictionary.
It will prompt the user to create a new dictionary asking for keys and values.
As a return it will create a third one which will be the merge of the two dictionaries and return it as a serialized JSON string.
Here is the prototype of the function: Here is the prototype of the function:
```python ```python
def merge_two(first_dict, second_dict): def merge_two(first_dict):
# this is a function, # this is a function,
# write your code here # write your code here
``` ```
@ -26,31 +29,43 @@ first = {
"Louise": 23, "Louise": 23,
"Lea": 34 "Lea": 34
} }
second = {
"Louise": 44, print(merge_two(first))
"Romolo": 30,
"Lea": 22
}
print(merge_two(first, second))
``` ```
Run your test file with the following command: Run your test file with the following command:
```console ```console
$ python3 test.py $ python3 test.py
Add a new entry:
key: Louise
value: 44
Add a new entry:
key: Romolo
value: 30
Add a new entry:
key: Lea
value: 22
Add a new entry:
key: exit
{"Bob": 36, "Louise": 44, "Lea": 22, "Romolo": 30} {"Bob": 36, "Louise": 44, "Lea": 22, "Romolo": 30}
$ $
``` ```
### Hints ### Hints
- If a key is repeated in both dictionaries the value retained will be the one in `second_dict`. - If a key is repeated in both dictionaries the value retained will be the last one.
- There are different ways to merge dictionaries, take the time to understand the differences in between those techniques and try more than one technique to better retain it. - There are different ways to merge dictionaries, take the time to understand the differences in between those techniques and try more than one technique to better retain it.
- Add `import json` to use the standard functions for JSON manipulation. - Add `import json` to use the standard functions for JSON manipulation.
- Use the function `input()` to read from `stdin` and `int()` to convert the value to a number.
> Your solution will be tested only for valid inputs (all the values will be convertible to `int`).
### References ### References
- [Merging dictionaries in Python](https://www.geeksforgeeks.org/python-merging-two-dictionaries/) - [Merging dictionaries in Python](https://www.geeksforgeeks.org/python-merging-two-dictionaries/)
- [JSON library in Python](https://docs.python.org/3/library/json.html) - [JSON library in Python](https://docs.python.org/3/library/json.html)
- [Function input() in Python](https://www.w3schools.com/python/ref_func_input.asp)
Loading…
Cancel
Save