diff --git a/snippets/capitalize.md b/snippets/capitalize.md index 2c65e007f..3d19f53e7 100644 --- a/snippets/capitalize.md +++ b/snippets/capitalize.md @@ -1,14 +1,16 @@ --- title: capitalize -tags: string +tags: string,intermediate --- + 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 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 diff --git a/snippets/capitalize_every_word.md b/snippets/capitalize_every_word.md index d734b6f62..d571f36d6 100644 --- a/snippets/capitalize_every_word.md +++ b/snippets/capitalize_every_word.md @@ -1,14 +1,15 @@ --- title: capitalize_every_word -tags: string +tags: string,beginner --- + 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 def capitalize_every_word(string): - return string.title() + return string.title() ``` ```py diff --git a/snippets/chunk.md b/snippets/chunk.md index a998fa95b..f502ad729 100644 --- a/snippets/chunk.md +++ b/snippets/chunk.md @@ -1,19 +1,21 @@ --- 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 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))))) + return list( + map(lambda x: lst[x * size:x * size + size], + list(range(0, ceil(len(lst) / size))))) ``` ```py diff --git a/snippets/compact.md b/snippets/compact.md index 38186b48e..6536fdcf8 100644 --- a/snippets/compact.md +++ b/snippets/compact.md @@ -1,14 +1,15 @@ --- title: compact -tags: list +tags: list,beginner --- + 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 def compact(lst): - return list(filter(bool, lst)) + return list(filter(bool, lst)) ``` ```py diff --git a/snippets/count_by.md b/snippets/count_by.md index e15e431a4..f53b6d67a 100644 --- a/snippets/count_by.md +++ b/snippets/count_by.md @@ -1,20 +1,20 @@ --- 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. -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 def count_by(arr, fn=lambda x: x): - key = {} - for el in map(fn, arr): - key[el] = 0 if el not in key else key[el] - key[el] += 1 - return key + key = {} + for el in map(fn, arr): + key[el] = 0 if el not in key else key[el] + key[el] += 1 + return key ``` ```py