Refactor snippets
This commit is contained in:
@ -6,13 +6,13 @@ tags: list,intermediate
|
||||
Splits values into two groups.
|
||||
If an element in `filter` is `True`, the corresponding element in the collection belongs to the first group; otherwise, it belongs to the second group.
|
||||
|
||||
- Use list comprehension and `enumerate()` to add elements to groups, based on `filter`.
|
||||
- Use list comprehension and `zip()` to add elements to groups, based on `filter`.
|
||||
|
||||
```py
|
||||
def bifurcate(lst, filter):
|
||||
return [
|
||||
[x for i, x in enumerate(lst) if filter[i] == True],
|
||||
[x for i, x in enumerate(lst) if filter[i] == False]
|
||||
[x for x, flag in zip(lst, filter) if flag],
|
||||
[x for x, flag in zip(lst, filter) if not flag]
|
||||
]
|
||||
```
|
||||
|
||||
|
||||
@ -15,7 +15,7 @@ from math import ceil
|
||||
def chunk(lst, size):
|
||||
return list(
|
||||
map(lambda x: lst[x * size:x * size + size],
|
||||
list(range(0, ceil(len(lst) / size)))))
|
||||
list(range(ceil(len(lst) / size)))))
|
||||
```
|
||||
|
||||
```py
|
||||
|
||||
@ -14,7 +14,7 @@ Returns an error if `step` equals `1`.
|
||||
from math import floor, log
|
||||
|
||||
def geometric_progression(end, start = 1, step = 2):
|
||||
return [start * step ** i for i in range(0, floor(log(end/start) / log(step)) + 1)]
|
||||
return [start * step ** i for i in range(floor(log(end/start) / log(step)) + 1)]
|
||||
```
|
||||
|
||||
```py
|
||||
|
||||
Reference in New Issue
Block a user