Sort the Dictionary
Your task is to write a function called sort_dictionary that is capable of sorting a given dictionary either by its keys or its values.
The function sort_dictionary takes three arguments:
dictionary: a dictionary to be sorted. The dictionary keys are strings, while values are numbers.by: a string that indicates whether the sorting should be performed by 'keys' or by 'values'.reverse: a boolean keyword-only argument that determines the sorting order. IfreverseisFalse, the sorting is done in ascending order (increasing). IfreverseisTrue, the sorting is done in descending order (decreasing). The default value ofreverseshould beFalse.
The function should return the sorted dictionary as a list of tuples. Each tuple should contain two elements - the key and its corresponding value in the dictionary. The format of the tuple should be: (key, value).
Note that, the sorting should be case-insensitive.
Input | Output |
|---|---|
|
|
|
|
|
|
|
|
Constraints
Time limit: 2 seconds
Memory limit: 512 MB
Output limit: 1 MB