#C13035. Merge and Sort Dictionaries
Merge and Sort Dictionaries
Merge and Sort Dictionaries
You are given two dictionaries provided as JSON strings on separate lines. Your task is to merge these two dictionaries into one and sort the resulting dictionary in ascending order based on their values. In case two or more keys have the same value, the keys should be sorted in lexicographical (ASCII) order.
More formally, let \(D_1\) and \(D_2\) be two dictionaries. The merged dictionary \(D\) is defined as:
[ D = D_1 \cup D_2 ]
The dictionary \(D\) must then be sorted by value and then by key (if values are the same). The final output should use the Python dictionary literal format, e.g.,
{'key1': value1, 'key2': value2, ...}
Note that the input will be given via standard input (stdin) and the output must be printed to standard output (stdout).
inputFormat
Input consists of two lines. Each line is a valid JSON string representing a dictionary where keys are strings and values are integers. For example:
{"a": 3, "b": 1} {"c": 2, "d": 2}
outputFormat
Output the merged dictionary in a single line following the Python dictionary literal format. The dictionary should be sorted in ascending order by value, and if two keys have the same value, then by key in ascending order. For example, for the input above the correct output is:
{'b': 1, 'c': 2, 'd': 2, 'a': 3}## sample
{"a": 3, "b": 1}
{"c": 2, "d": 2}
{'b': 1, 'c': 2, 'd': 2, 'a': 3}