N Largest Values

mediumnumpy_indexing

Implement n_largest(x, n).

Given a 1D array x and an integer n (with 1 <= n <= len(x)), return the n largest values of x, sorted in descending order. No for/while loops. (Hint: np.argpartition finds the top n without a full sort.)

Constraints
  • no scipy, sklearn
  • no for/while loops

Your solution

Edit n_largest and run the real pytest suite in your browser — no install required. Your code is saved locally.

⌘/Ctrl + ↵

Loading the Python runtime (first run only)…

Hints

Hint 1

np.argpartition puts the n largest in front (unordered); take them, then sort descending.