diff --git a/snippets/count_occurences.md b/snippets/count_occurences.md index 87c5bb2be..7c5c4dd6c 100644 --- a/snippets/count_occurences.md +++ b/snippets/count_occurences.md @@ -1,16 +1,15 @@ --- title: count_occurences -tags: list +tags: list,beginner --- -:information_source: Already implemented via `list.count()`. Counts the occurrences of a value in a list. -Uses the list comprehension to increment a counter each time you encounter the specific value inside the list. +Increment a counter for every item in the list that has the given value and is of the same type. ```py def count_occurrences(lst, val): - return len([x for x in lst if x == val and type(x) == type(val)]) + return len([x for x in lst if x == val and type(x) == type(val)]) ``` ```py diff --git a/snippets/count_vowels.md b/snippets/count_vowels.md deleted file mode 100644 index 8198d120c..000000000 --- a/snippets/count_vowels.md +++ /dev/null @@ -1,17 +0,0 @@ ---- -title: count_vowels -tags: string ---- -Retuns `number` of vowels in provided `string`. - -Use a regular expression to count the number of vowels `(A, E, I, O, U)` in a string. - -```py -import re - -def count_vowels(str): - return len(re.findall(r'[aeiou]', str, re.IGNORECASE)) - -count_vowels('foobar') # 3 -count_vowels('gym')# 0 -``` diff --git a/snippets/decapitalize.md b/snippets/decapitalize.md index cb5e53fca..e26758a31 100644 --- a/snippets/decapitalize.md +++ b/snippets/decapitalize.md @@ -1,14 +1,16 @@ --- title: decapitalize -tags: string +tags: string,intermediate --- + Decapitalizes the first letter of a string. -Decapitalizes the first letter of the string and then adds it with rest of the string. Omit the `upper_rest` parameter to keep the rest of the string intact, or set it to `true` to convert to uppercase. +Decapitalize the first letter of the string and then add it with rest of the string. +Omit the `upper_rest` parameter to keep the rest of the string intact, or set it to `True` to convert to uppercase. ```py def decapitalize(string, upper_rest=False): - return str[:1].lower() + (str[1:].upper() if upper_rest else str[1:]) + return str[:1].lower() + (str[1:].upper() if upper_rest else str[1:]) ``` ```py diff --git a/snippets/deep_flatten.md b/snippets/deep_flatten.md index 8a148fe1b..299855bbf 100644 --- a/snippets/deep_flatten.md +++ b/snippets/deep_flatten.md @@ -1,27 +1,30 @@ --- title: deep_flatten -tags: list +tags: list,recursion,intermediate --- + Deep flattens a list. -Use recursion. Use `list.extend()` with an empty list (`result`) and the spread function to flatten a list. Recursively flatten each element that is a list. +Use recursion. +Define a function, `spread`, that uses either `list.extend()` or `list.append()` on each element in a list to flatten it. +Use `list.extend()` with an empty list and the `spread` function to flatten a list. +Recursively flatten each element that is a list. ```py def spread(arg): - ret = [] - for i in arg: - if isinstance(i, list): - ret.extend(i) - else: - ret.append(i) - return ret - + ret = [] + for i in arg: + if isinstance(i, list): + ret.extend(i) + else: + ret.append(i) + return ret def deep_flatten(lst): - result = [] - result.extend( - spread(list(map(lambda x: deep_flatten(x) if type(x) == list else x, lst)))) - return result + result = [] + result.extend( + spread(list(map(lambda x: deep_flatten(x) if type(x) == list else x, lst)))) + return result ``` ```py diff --git a/snippets/difference.md b/snippets/difference.md index afeb8cbcd..4c730cf68 100644 --- a/snippets/difference.md +++ b/snippets/difference.md @@ -1,16 +1,18 @@ --- title: difference -tags: list +tags: list,beginner --- + Returns the difference between two iterables. -Use list comprehension to only keep values not contained in `b`. +Create a `set` fromn `b`, then use list comprehension on `a` to only keep values not contained in the previously created set, `_b`. ```py def difference(a, b): - _b = set(b) - return [item for item in a if item not in _b] + _b = set(b) + return [item for item in a if item not in _b] ``` + ```py difference([1, 2, 3], [1, 2, 4]) # [3] ``` diff --git a/snippets/difference_by.md b/snippets/difference_by.md index 5fae2a2f6..5805b3366 100644 --- a/snippets/difference_by.md +++ b/snippets/difference_by.md @@ -1,15 +1,16 @@ --- title: difference_by -tags: list +tags: list,function,intermediate --- -Returns the difference between two list, after applying the provided function to each list element of both. -Create a `set` by applying `fn` to each element in `b`, then use list comprehension in combination with fn on a to only keep values not contained in the previously created `set`. +Returns the difference between two lists, after applying the provided function to each list element of both. + +Create a `set` by applying `fn` to each element in `b`, then use list comprehension in combination with `fn` on `a` to only keep values not contained in the previously created set, `_b`. ```py def difference_by(a, b, fn): - b = set(map(fn, b)) - return [item for item in a if fn(item) not in b] + _b = set(map(fn, b)) + return [item for item in a if fn(item) not in _b] ``` ```py