min_n and max_n

This commit is contained in:
Rohit Tanwar
2018-01-19 15:02:18 +05:30
parent 9cc6625032
commit 0c281b9dc1
4 changed files with 26 additions and 0 deletions

View File

@ -97,6 +97,7 @@ def spread(arg):
```
```python
@ -193,6 +194,7 @@ Use `list.sort()` combined with the `deepcopy` function from the inbuilt `copy`
```python
from copy import deepcopy
@ -203,6 +205,11 @@ def max_n(arr, n=1):
return numbers[:n]
```
```python
max_n([1, 2, 3]) # [3]
max_n([1, 2, 3], 2) # [3,2]
```
### 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).
@ -211,6 +218,7 @@ Use `list.sort()` combined with the `deepcopy` function from the inbuilt `copy`
```python
from copy import deepcopy
@ -220,6 +228,11 @@ def min_n(arr, n=1):
return numbers[:n]
```
```python
min_n([1, 2, 3]) # [1]
min_n([1, 2, 3], 2) # [1,2]
```
### spread
Implements javascript's spread syntax as a function. Flattens the list(non-deep) and returns an list.

View File

@ -23,6 +23,7 @@ def spread(arg):
```
```python

View File

@ -6,6 +6,7 @@ Use `list.sort()` combined with the `deepcopy` function from the inbuilt `copy`
```python
from copy import deepcopy
@ -15,4 +16,9 @@ def max_n(arr, n=1):
numbers.reverse()
return numbers[:n]
```
```python
max_n([1, 2, 3]) # [3]
max_n([1, 2, 3], 2) # [3,2]
```

View File

@ -6,6 +6,7 @@ Use `list.sort()` combined with the `deepcopy` function from the inbuilt `copy`
```python
from copy import deepcopy
@ -14,4 +15,9 @@ def min_n(arr, n=1):
numbers.sort()
return numbers[:n]
```
```python
min_n([1, 2, 3]) # [1]
min_n([1, 2, 3], 2) # [1,2]
```