The problem is not only that questions that only show the desired inputs and outputs as code examples are not good signposts, the problem is also that examples can only ever show what the code should do for those specific examples. Examples can never be general rules.
For example: in the example in the question, the OP shows the Sno
value becoming the outer key of the resulting dictionary-of-dictionaries, and further shows the key value being unique. However, in a comment, the OP indicated that the key can be variable, i.e. that the key is not always Sno
(in the comment, the OP mentions age
), and that the key could have duplicate values (age
s are not necessarily unique).
This requires a completely different kind of solution; in fact, it means that the OP's desired output is actually impossible to achieve in general! However, there is absolutely no way to figure this out from the single example input and output provided.
Here is another example from the Ruby tag. The OP wants to sort an array-of-hashes (list-of-dictionaries in Python terms) which contains an id
key based on the order of IDs specified in a separate array. Not all IDs need to be present in that array, and for IDs that are missing from the array, the only thing the OP specifies is that they "just don't need to sort them and let them be as they were."
However, in the provided example input and output, it just so happens that all the IDs which are not specified in the ordering array happen to be at the end of the array-of-hashes in both the input and the output and they happen to be in ascending order in both the input and the output. It also happens to be the case that all IDs specified in the ordering array are present in the input, and it happens to be the case that the keys are unique. It is absolutely not clear from the provided example which of those facts are coincidences, and which of those can be relied upon to always be the case.
It is unclear what guarantees can be assumed of the input (are the keys unique, are the missing keys always at the end, are they always in ascending order, are they always greater than the keys which are present in the ordering array) and what the requirements are for the output (do the missing keys need to be sorted).
It required several hours of back and forth to clarify that, in fact, all the OP needs is to have the missing keys at the end of the array in an arbitrary order. It is still not clear whether it can be assumed that the keys are unique, for example.
Note that there are two different answers, each of which have inferred different rules from the single provided example, and neither of which are actually correct according to comments by the OP.
Ideally, such a data structure transformation question should include:
- A specification of the rules for transforming the input into the output, including not just the "normal" ("happy") case but also any and all special cases, edge cases, corner cases, boundary cases, and exceptional cases. (Classic examples: no input, empty input, singleton input, duplicate keys, duplicate values, missing keys, missing values, irregular input shapes, …)
- A specification of the possible valid inputs.
- A specification for the possible valid outputs.
- Examples of input / output pairs demonstrating both the normal case as well as the special cases, edge cases, corner cases, boundary cases, and exceptional cases.
- A skeleton API for how the data transformation code is to be used.
- Ideally, the input / output examples should come in form of a test suite using the skeleton API. That way, all an answerer needs to do is fill out the missing bits of the skeleton API and run the test suite to know whether the answer is correct.
The human-readable specification serves as fodder for the search engine, the test suite makes it easy for answerers to check the correctness of the answer. In addition, having two separate specifications (in words and in code) serves as a form of sanity check akin to double-entry bookkeeping.