From 9cc6625032d8bb1b7d0006e044960defcf916274 Mon Sep 17 00:00:00 2001 From: Rohit Tanwar Date: Fri, 19 Jan 2018 14:59:38 +0530 Subject: [PATCH] min_n and max_n --- README.md | 42 ++++++++++++++++++++++++++++++++++++++-- snippets/deep_flatten.md | 3 +++ snippets/max_n.md | 8 ++++++-- snippets/min_n.md | 10 +++++++--- 4 files changed, 56 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index d34a9f7f9..10c854f74 100644 --- a/README.md +++ b/README.md @@ -94,8 +94,11 @@ def spread(arg): result = [] result.extend(spread(list(map(lambda x : deep(x) if type(x) == list else x,arr)))) return result - -``` + + + +``` + ```python deep_flatten([1, [2], [[3], 4], 5]) # [1,2,3,4,5] ``` @@ -181,6 +184,41 @@ def lcm(*args): ``` python lcm(12, 7) # 84 lcm([1, 3, 4], 5) # 60 +``` +### max_n + +Returns the `n` maximum elements from the provided list. If `n` is greater than or equal to the provided list's length, then return the original list(sorted in descending order). + +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] + +``` +### min_n + +Returns the `n` minimum elements from the provided list. If `n` is greater than or equal to the provided list's length, then return the original list(sorted in ascending order). + +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. 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 min_n(arr, n=1): + numbers = deepcopy(arr) + numbers.sort() + return numbers[:n] + ``` ### spread diff --git a/snippets/deep_flatten.md b/snippets/deep_flatten.md index eed6f3be0..49778a251 100644 --- a/snippets/deep_flatten.md +++ b/snippets/deep_flatten.md @@ -20,6 +20,9 @@ def spread(arg): result = [] result.extend(spread(list(map(lambda x : deep(x) if type(x) == list else x,arr)))) return result + + + ``` ```python diff --git a/snippets/max_n.md b/snippets/max_n.md index 670a4e284..e34b60a2e 100644 --- a/snippets/max_n.md +++ b/snippets/max_n.md @@ -4,11 +4,15 @@ 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 -``` py +```python + from copy import deepcopy -def max_n(arr,n = 1): + + +def max_n(arr, n=1): numbers = deepcopy(arr) numbers.sort() numbers.reverse() return numbers[:n] + ``` \ No newline at end of file diff --git a/snippets/min_n.md b/snippets/min_n.md index 4e3ad550c..3298a46f8 100644 --- a/snippets/min_n.md +++ b/snippets/min_n.md @@ -4,10 +4,14 @@ Returns the `n` minimum 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. Use `[:n]` to get the specified number of elements. Omit the second argument, `n`, to get a one-element array -``` py +```python + from copy import deepcopy -def min_n(arr,n = 1): + + +def min_n(arr, n=1): numbers = deepcopy(arr) - numbers.sort( ) + numbers.sort() return numbers[:n] + ``` \ No newline at end of file