#C10901. Sort Dictionary by Value
Sort Dictionary by Value
Sort Dictionary by Value
You are given an input in the form of a valid JSON string. If the input represents a dictionary (i.e. a JSON object) with string keys and integer values, your task is to sort its key-value pairs by the values in ascending order.
If the input is not a dictionary, output an empty list []
.
Formally, let the dictionary be \(D = \{k_1: v_1, k_2: v_2, \ldots, k_n: v_n\}\). You need to output the sorted list of pairs:
[ S = \text{sorted}(D.items(),; \lambda, (k,v): v) = [(k',v'), \ldots]]
Each pair should be printed in the format ('key', value)
and the final output must be enclosed in square brackets.
inputFormat
The input is provided via standard input (stdin) as a single line containing a valid JSON string.
outputFormat
If the input is a valid dictionary, print a list of key-value pairs sorted by the values in ascending order. Each pair should be printed as a tuple in the format ('key', value)
. Otherwise, print []
.
{"c": 3, "b": 1, "a": 2}
[('b', 1), ('a', 2), ('c', 3)]