Ad

Turn sort into a simple lambda

Code
Diff
  • sort_values = lambda x: sorted(x)
    
    • from __future__ import annotations
    • from collections import Counter
    • def sort_values(vals:list[int]) -> list[int]:
    • #Given an array of size N containing only 0s, 1s, and 2s;
    • #sort the array in ascending order.
    • assert set(vals) <= {0,1,2}
    • return counting_sort(vals)
    • # O(n+k) instead of n log(n)
    • def counting_sort(vals:list[T]) -> list[T]:
    • res = []
    • c = Counter(vals)
    • for k,amt in sorted(c.items()):
    • res += [k]*amt
    • return res
    • sort_values = lambda x: sorted(x)