Update some snippets

This commit is contained in:
Angelos Chalaris
2019-08-20 10:24:16 +03:00
parent a340ba4492
commit 1c93753ade
5 changed files with 30 additions and 24 deletions

View File

@ -1,14 +1,16 @@
--- ---
title: capitalize title: capitalize
tags: string tags: string,intermediate
--- ---
Capitalizes the first letter of a string. Capitalizes the first letter of a string.
Capitalizes the first letter of the string and then adds it with rest of the string. Omit the `lower_rest` parameter to keep the rest of the string intact, or set it to `true` to convert to lowercase. Capitalize the first letter of the string and then add it with rest of the string.
Omit the `lower_rest` parameter to keep the rest of the string intact, or set it to `True` to convert to lowercase.
```py ```py
def capitalize(string, lower_rest=False): def capitalize(string, lower_rest=False):
return string[:1].upper() + (string[1:].lower() if lower_rest else string[1:]) return string[:1].upper() + (string[1:].lower() if lower_rest else string[1:])
``` ```
```py ```py

View File

@ -1,14 +1,15 @@
--- ---
title: capitalize_every_word title: capitalize_every_word
tags: string tags: string,beginner
--- ---
Capitalizes the first letter of every word in a string. Capitalizes the first letter of every word in a string.
Uses `str.title` to capitalize first letter of every word in the string. Use `string.title()` to capitalize first letter of every word in the string.
```py ```py
def capitalize_every_word(string): def capitalize_every_word(string):
return string.title() return string.title()
``` ```
```py ```py

View File

@ -1,19 +1,21 @@
--- ---
title: chunk title: chunk
tags: list tags: list,intermediate
--- ---
Chunks an list into smaller lists of a specified size.
Uses `range` to create a list of desired size. Then use `map` on this list and fill it with splices of `lst`. Chunks a list into smaller lists of a specified size.
Use `list()` and `range()` to create a list of the desired `size`.
Use `map()` on the list and fill it with splices of the given list.
Finally, return use created list.
```py ```py
from math import ceil 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(0, ceil(len(lst) / size)))))
``` ```
```py ```py

View File

@ -1,14 +1,15 @@
--- ---
title: compact title: compact
tags: list tags: list,beginner
--- ---
Removes falsey values from a list. Removes falsey values from a list.
Use `filter()` to filter out falsey values (False, None, 0, and ""). Use `filter()` to filter out falsey values (`False`, `None`, `0`, and `""`).
```py ```py
def compact(lst): def compact(lst):
return list(filter(bool, lst)) return list(filter(bool, lst))
``` ```
```py ```py

View File

@ -1,20 +1,20 @@
--- ---
title: count_by title: count_by
tags: list tags: list,intermediate
--- ---
:information_source: Already implemented via `collections.Counter`
Groups the elements of a list based on the given function and returns the count of elements in each group. Groups the elements of a list based on the given function and returns the count of elements in each group.
Use `map()` to map the values of the list using the given function. Iterate over the map and increase the the elements count each time it occurs. Use `map()` to map the values of the given list using the given function.
Iterate over the map and increase the element count each time it occurs.
```py ```py
def count_by(arr, fn=lambda x: x): def count_by(arr, fn=lambda x: x):
key = {} key = {}
for el in map(fn, arr): for el in map(fn, arr):
key[el] = 0 if el not in key else key[el] key[el] = 0 if el not in key else key[el]
key[el] += 1 key[el] += 1
return key return key
``` ```
```py ```py