Sets and Uniqueness
~8 mincode completion
Write unique_sorted(values) that returns a list of the distinct values, in ascending order.
[3, 1, 3, 2, 1] returns [1, 2, 3].
Examples
Duplicates are removed and the rest sorted
- Input
- unique_sorted([3, 1, 3, 2, 1])
- Output
- [1, 2, 3]
An already unique list just comes back sorted
- Input
- unique_sorted([5, 4, 6])
- Output
- [4, 5, 6]
All-identical values collapse to a single item
- Input
- unique_sorted([7, 7, 7])
- Output
- [7]
Hints
Hint 1
Sorting first makes the rest straightforward.
Hint 2
Return list rather than set.
Requirements
: a list
Return a sorted list of the distinct values.
Constraints
Standard library only, no imports required
Time limit: 200 ms, Memory: 64 MB
Where this shows up
~8 min
••••••••••••••••
8 employers weight this skill
3 quant funds, 2 frontier labs, 2 big tech firms, 1 AI product company. Top match scores 80.
Python
def unique_sorted(values):
"""
Remove duplicates and sort what is left.
Args:
values: a list
Returns:
A sorted list of the distinct values.
"""
# YOUR CODE HERE
pass