Refactor snippets

This commit is contained in:
Veronica Guo
2020-10-18 23:11:56 -04:00
parent 69e5e3776b
commit edb914b41d
3 changed files with 5 additions and 5 deletions

View File

@ -6,13 +6,13 @@ tags: list,intermediate
Splits values into two groups. 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. 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 ```py
def bifurcate(lst, filter): def bifurcate(lst, filter):
return [ return [
[x for i, x in enumerate(lst) if filter[i] == True], [x for x, flag in zip(lst, filter) if flag],
[x for i, x in enumerate(lst) if filter[i] == False] [x for x, flag in zip(lst, filter) if not flag]
] ]
``` ```

View File

@ -15,7 +15,7 @@ from math import ceil
def chunk(lst, size): def chunk(lst, size):
return list( return list(
map(lambda x: lst[x * size:x * size + size], map(lambda x: lst[x * size:x * size + size],
list(range(0, ceil(len(lst) / size))))) list(range(ceil(len(lst) / size)))))
``` ```
```py ```py

View File

@ -14,7 +14,7 @@ Returns an error if `step` equals `1`.
from math import floor, log from math import floor, log
def geometric_progression(end, start = 1, step = 2): 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 ```py