diff --git a/snippets/max_n.md b/snippets/max_n.md index 05953faeb..6fe3f5092 100644 --- a/snippets/max_n.md +++ b/snippets/max_n.md @@ -5,14 +5,8 @@ Returns the `n` maximum elements from the provided list. If `n` is greater than Use `list.sort()` combined with the `deepcopy` function from the inbuilt `copy` module to create a shallow clone of the list and sort it in ascending order and then use `list.reverse()` reverse it to make it descending order. Use `[:n]` to get the specified number of elements. Omit the second argument, `n`, to get a one-element array ```python -from copy import deepcopy - - -def max_n(arr, n=1): - numbers = deepcopy(arr) - numbers.sort() - numbers.reverse() - return numbers[:n] +def max_n(arr, n=1, reverse=True): + return sorted(arr, reverse=reverse)[:n] ``` ```python