diff --git a/snippets/decapitalize.md b/snippets/decapitalize.md index 21d02f013..c68a93d6b 100644 --- a/snippets/decapitalize.md +++ b/snippets/decapitalize.md @@ -5,12 +5,13 @@ tags: string,intermediate Decapitalizes the first letter of a string. -- Decapitalize the first letter of the string and then add it with rest of the string. +- Use list slicing and `str.lower()` to decapitalize the first letter of the string. +- Use `str.join()` to combine the lowercase first letter with the rest of the characters. - 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(s, upper_rest=False): - return s[:1].lower() + (s[1:].upper() if upper_rest else s[1:]) +def decapitalize(s, upper_rest = False): + return ''.join([s[:1].lower(), (s[1:].upper() if upper_rest else s[1:])]) ``` ```py diff --git a/snippets/deep_flatten.md b/snippets/deep_flatten.md index 8d489ae02..349c1830d 100644 --- a/snippets/deep_flatten.md +++ b/snippets/deep_flatten.md @@ -7,15 +7,14 @@ Deep flattens a list. - Use recursion. - Use `isinstance()` with `collections.abc.Iterable` to check if an element is iterable. -- If it is, apply `deep_flatten()` recursively, otherwise return `[lst]`. +- If it is iterable, apply `deep_flatten()` recursively, otherwise return `[lst]`. ```py -from collections.abc import Iterable - -def deep_flatten(lst): - return [a for i in lst for a in deep_flatten(i)] if isinstance(lst, Iterable) else [lst] +def deep_flatten(lst): + return ([a for i in lst for a in + deep_flatten(i)] if isinstance(lst, Iterable) else [lst]) ``` ```py -deep_flatten([1, [2], [[3], 4], 5]) # [1,2,3,4,5] +deep_flatten([1, [2], [[3], 4], 5]) # [1, 2, 3, 4, 5] ``` diff --git a/snippets/degrees_to_rads.md b/snippets/degrees_to_rads.md index 816988baf..5426d459f 100644 --- a/snippets/degrees_to_rads.md +++ b/snippets/degrees_to_rads.md @@ -15,5 +15,5 @@ def degrees_to_rads(deg): ``` ```py -degrees_to_rads(180) # 3.141592653589793 +degrees_to_rads(180) # ~3.1416 ``` diff --git a/snippets/delay.md b/snippets/delay.md index 7494c528f..70f4f3b7b 100644 --- a/snippets/delay.md +++ b/snippets/delay.md @@ -16,9 +16,5 @@ def delay(fn, ms, *args): ``` ```py -delay( - lambda x: print(x), - 1000, - 'later' -) # prints 'later' after one second +delay(lambda x: print(x), 1000, 'later') # prints 'later' after one second ``` diff --git a/snippets/dict_to_list.md b/snippets/dict_to_list.md index fedc005e0..5b13d7365 100644 --- a/snippets/dict_to_list.md +++ b/snippets/dict_to_list.md @@ -14,5 +14,6 @@ def dict_to_list(d): ```py d = {'one': 1, 'three': 3, 'five': 5, 'two': 2, 'four': 4} -dict_to_list(d) # [('one', 1), ('three', 3), ('five', 5), ('two', 2), ('four', 4)] +dict_to_list(d) +# [('one', 1), ('three', 3), ('five', 5), ('two', 2), ('four', 4)] ``` diff --git a/snippets/difference.md b/snippets/difference.md index 9db4c4982..cd34674f3 100644 --- a/snippets/difference.md +++ b/snippets/difference.md @@ -3,9 +3,10 @@ title: difference tags: list,beginner --- -Returns the difference between two iterables. +Calculates the difference between two iterables, without filtering duplicate values. -- Create a `set` from `b`, then use list comprehension on `a` to only keep values not contained in the previously created set, `_b`. +- Create a `set` from `b`. +- Use a list comprehension on `a` to only keep values not contained in the previously created set, `_b`. ```py def difference(a, b): diff --git a/snippets/difference_by.md b/snippets/difference_by.md index c70d1e67f..c436dd037 100644 --- a/snippets/difference_by.md +++ b/snippets/difference_by.md @@ -5,7 +5,8 @@ tags: list,function,intermediate 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`. +- Create a `set`, using `map()` to apply `fn` to each element in `b`. +- Use a 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): @@ -15,6 +16,8 @@ def difference_by(a, b, fn): ```py from math import floor + difference_by([2.1, 1.2], [2.3, 3.4], floor) # [1.2] -difference_by([{ 'x': 2 }, { 'x': 1 }], [{ 'x': 1 }], lambda v : v['x']) # [ { x: 2 } ] +difference_by([{ 'x': 2 }, { 'x': 1 }], [{ 'x': 1 }], lambda v : v['x']) +# [ { x: 2 } ] ``` diff --git a/snippets/drop.md b/snippets/drop.md index 523570b15..3770c4ef4 100644 --- a/snippets/drop.md +++ b/snippets/drop.md @@ -6,6 +6,7 @@ tags: list,beginner Returns a list with `n` elements removed from the left. - Use slice notation to remove the specified number of elements from the left. +- Omit the last argument, `n`, to use a default value of `1`. ```py def drop(a, n = 1): diff --git a/snippets/drop_right.md b/snippets/drop_right.md index 357e741d9..5dd30b2cb 100644 --- a/snippets/drop_right.md +++ b/snippets/drop_right.md @@ -6,6 +6,7 @@ tags: list,beginner Returns a list with `n` elements removed from the right. - Use slice notation to remove the specified number of elements from the right. +- Omit the last argument, `n`, to use a default value of `1`. ```py def drop_right(a, n = 1): diff --git a/snippets/every.md b/snippets/every.md index f848f5eee..48fe9f39c 100644 --- a/snippets/every.md +++ b/snippets/every.md @@ -1,14 +1,14 @@ --- title: every -tags: list,function,intermediate +tags: list,intermediate --- -Returns `True` if the provided function returns `True` for every element in the list, `False` otherwise. +Checks if the provided function returns `True` for every element in the list. -- Use `all()` in combination with `map` and `fn` to check if `fn` returns `True` for all elements in the list. +- Use `all()` in combination with `map()` and `fn` to check if `fn` returns `True` for all elements in the list. ```py -def every(lst, fn=lambda x: x): +def every(lst, fn = lambda x: x): return all(map(fn, lst)) ``` diff --git a/snippets/every_nth.md b/snippets/every_nth.md index 5fb99bbe2..a20711d70 100644 --- a/snippets/every_nth.md +++ b/snippets/every_nth.md @@ -3,9 +3,9 @@ title: every_nth tags: list,beginner --- -Returns every nth element in a list. +Returns every `nth` element in a list. -- Use `[nth-1::nth]` to create a new list that contains every nth element of the given list. +- Use slice notation to create a new list that contains every `nth` element of the given list. ```py def every_nth(lst, nth): diff --git a/snippets/fahrenheit_to_celsius.md b/snippets/fahrenheit_to_celsius.md index e1efbaae7..f60947b0e 100644 --- a/snippets/fahrenheit_to_celsius.md +++ b/snippets/fahrenheit_to_celsius.md @@ -5,11 +5,11 @@ tags: math,beginner Converts Fahrenheit to Celsius. -- Use the formula `celsius = (fahrenheit - 32) / 1.8` to convert from Fahrenheit to Celsius. +- Follow the conversion formula `C = (F - 32) * 5/9`. ```py -def fahrenheit_to_celsius(fahrenheit): - return ((fahrenheit - 32) / 1.8) +def fahrenheit_to_celsius(degrees): + return ((degrees - 32) * 5/9) ``` ```py diff --git a/snippets/fibonacci.md b/snippets/fibonacci.md index 5e0cc3bcc..7de53c697 100644 --- a/snippets/fibonacci.md +++ b/snippets/fibonacci.md @@ -12,12 +12,10 @@ Generates a list, containing the Fibonacci sequence, up until the nth term. def fibonacci(n): if n <= 0: return [0] - sequence = [0, 1] while len(sequence) <= n: next_value = sequence[len(sequence) - 1] + sequence[len(sequence) - 2] sequence.append(next_value) - return sequence ``` diff --git a/snippets/filter_non_unique.md b/snippets/filter_non_unique.md index ec6f68763..ac8178ce3 100644 --- a/snippets/filter_non_unique.md +++ b/snippets/filter_non_unique.md @@ -3,10 +3,10 @@ title: filter_non_unique tags: list,beginner --- -Filters out the non-unique values in a list. +Creates a list with the non-unique values filtered out. -- Use a `collections.Counter` to get the count of each value in the list. -- Use list comprehension to create a list containing only the unique values. +- Use `collections.Counter` to get the count of each value in the list. +- Use a list comprehension to create a list containing only the unique values. ```py from collections import Counter diff --git a/snippets/filter_unique.md b/snippets/filter_unique.md index ccc72e5da..a4f9697d7 100644 --- a/snippets/filter_unique.md +++ b/snippets/filter_unique.md @@ -3,10 +3,10 @@ title: filter_unique tags: list,beginner --- -Filters out the unique values in a list. +Creates a list with the unique values filtered out. -- Use a `collections.Counter` to get the count of each value in the list. -- Use list comprehension to create a list containing only the non-unique values. +- Use `collections.Counter` to get the count of each value in the list. +- Use a list comprehension to create a list containing only the non-unique values. ```py from collections import Counter diff --git a/snippets/find.md b/snippets/find.md index 712797881..ac2c2f467 100644 --- a/snippets/find.md +++ b/snippets/find.md @@ -3,9 +3,9 @@ title: find tags: list,beginner --- -Returns the value of the first element in the provided list that satisfies the provided testing function. +Finds the value of the first element in the given list that satisfies the provided testing function. -- Use list comprehension and `next()` to return the first element in `lst` for which `fn` returns `True`. +- Use a list comprehension and `next()` to return the first element in `lst` for which `fn` returns `True`. ```py def find(lst, fn): diff --git a/snippets/find_index.md b/snippets/find_index.md index 9075ea125..4ceac691a 100644 --- a/snippets/find_index.md +++ b/snippets/find_index.md @@ -3,9 +3,9 @@ title: find_index tags: list,intermediate --- -Returns the index of the first element in the provided list that satisfies the provided testing function. +Finds the index of the first element in the given list that satisfies the provided testing function. -- Use list comprehension, `enumerate()` and `next()` to return the index of the first element in `lst` for which `fn` returns `True`. +- Use a list comprehension, `enumerate()` and `next()` to return the index of the first element in `lst` for which `fn` returns `True`. ```py def find_index(lst, fn): diff --git a/snippets/find_index_of_all.md b/snippets/find_index_of_all.md index 1adaae0a9..6bcda46a8 100644 --- a/snippets/find_index_of_all.md +++ b/snippets/find_index_of_all.md @@ -3,7 +3,7 @@ title: find_index_of_all tags: list,intermediate --- -Returns the indexes of all elements in the provided list that satisfy the provided testing function. +Finds the indexes of all elements in the given list that satisfy the provided testing function. - Use `enumerate()` and a list comprehension to return the indexes of the all element in `lst` for which `fn` returns `True`. diff --git a/snippets/find_key.md b/snippets/find_key.md index ad21dd819..cd837fed9 100644 --- a/snippets/find_key.md +++ b/snippets/find_key.md @@ -3,7 +3,7 @@ title: find_key tags: dictionary,intermediate --- -Returns the first key in the provided dictionary that has the given value. +Finds the first key in the provided dictionary that has the given value. - Use `dictionary.items()` and `next()` to return the first key that has a value equal to `val`. @@ -14,9 +14,9 @@ def find_key(dict, val): ```py ages = { - "Peter": 10, - "Isabel": 11, - "Anna": 9, + 'Peter': 10, + 'Isabel': 11, + 'Anna': 9, } -find_key(ages, 11) # "Isabel" +find_key(ages, 11) # 'Isabel' ``` diff --git a/snippets/find_keys.md b/snippets/find_keys.md index 689230ef2..dc1fb098e 100644 --- a/snippets/find_keys.md +++ b/snippets/find_keys.md @@ -3,7 +3,7 @@ title: find_keys tags: dictionary,intermediate --- -Returns all keys in the provided dictionary that have the given value. +Finds all keys in the provided dictionary that have the given value. - Use `dictionary.items()`, a generator and `list()` to return all keys that have a value equal to `val`. @@ -14,9 +14,9 @@ def find_keys(dict, val): ```py ages = { - "Peter": 10, - "Isabel": 11, - "Anna": 10, + 'Peter': 10, + 'Isabel': 11, + 'Anna': 10, } -find_keys(ages, 10) # [ "Peter", "Anna" ] +find_keys(ages, 10) # [ 'Peter', 'Anna' ] ``` diff --git a/snippets/find_last.md b/snippets/find_last.md index 5aa78aaec..9ead95a75 100644 --- a/snippets/find_last.md +++ b/snippets/find_last.md @@ -3,9 +3,9 @@ title: find_last tags: list,beginner --- -Returns the value of the last element in the provided list that satisfies the provided testing function. +Finds the value of the last element in the given list that satisfies the provided testing function. -- Use list comprehension and `next()` to return the last element in `lst` for which `fn` returns `True`. +- Use a list comprehension and `next()` to return the last element in `lst` for which `fn` returns `True`. ```py def find_last(lst, fn): diff --git a/snippets/find_last_index.md b/snippets/find_last_index.md index ef3616cb8..369f71d60 100644 --- a/snippets/find_last_index.md +++ b/snippets/find_last_index.md @@ -3,9 +3,9 @@ title: find_last_index tags: list,beginner --- -Returns the index of the last element in the provided list that satisfies the provided testing function. +Finds the index of the last element in the given list that satisfies the provided testing function. -- Use list comprehension, `enumerate()` and `next()` to return the index of the last element in `lst` for which `fn` returns `True`. +- Use a list comprehension, `enumerate()` and `next()` to return the index of the last element in `lst` for which `fn` returns `True`. ```py def find_last_index(lst, fn): diff --git a/snippets/find_parity_outliers.md b/snippets/find_parity_outliers.md index 487abb626..f90a9719a 100644 --- a/snippets/find_parity_outliers.md +++ b/snippets/find_parity_outliers.md @@ -3,9 +3,10 @@ title: find_parity_outliers tags: list,math,intermediate --- -Given a list, returns the items that are parity outliers. +Finds the items that are parity outliers in a given list. -- Use `collections.Counter` with a list comprehension to count even and odd values in the list, use `collections.Counter.most_common()` to get the most common parity. +- Use `collections.Counter` with a list comprehension to count even and odd values in the list. +- Use `collections.Counter.most_common()` to get the most common parity. - Use a list comprehension to find all elements that do not match the most common parity. ```py diff --git a/snippets/flatten.md b/snippets/flatten.md index 89331dc0c..ff11b6726 100644 --- a/snippets/flatten.md +++ b/snippets/flatten.md @@ -5,7 +5,7 @@ tags: list,intermediate Flattens a list of lists once. -- Use nested list comprehension to extract each value from sub-lists in order. +- Use a list comprehension to extract each value from sub-lists in order. ```py def flatten(lst): @@ -13,5 +13,5 @@ def flatten(lst): ``` ```py -flatten([[1,2,3,4],[5,6,7,8]]) # [1, 2, 3, 4, 5, 6, 7, 8] +flatten([[1, 2, 3, 4], [5, 6, 7, 8]]) # [1, 2, 3, 4, 5, 6, 7, 8] ``` diff --git a/snippets/frequencies.md b/snippets/frequencies.md index f78fe75b5..6786a8983 100644 --- a/snippets/frequencies.md +++ b/snippets/frequencies.md @@ -3,7 +3,7 @@ title: frequencies tags: list,intermediate --- -Returns a dictionary with the unique values of a list as keys and their frequencies as the values. +Creates a dictionary with the unique values of a list as keys and their frequencies as the values. - Use `collections.defaultdict()` to store the frequencies of each unique element. - Use `dict()` to return a dictionary with the unique elements of the list as keys and their frequencies as the values. diff --git a/snippets/from_iso_date.md b/snippets/from_iso_date.md index 99e675fea..fa4b6ed6c 100644 --- a/snippets/from_iso_date.md +++ b/snippets/from_iso_date.md @@ -5,7 +5,7 @@ tags: date,intermediate Converts a date from its ISO-8601 represenation. -- Use `datetime.datetime.fromisoformat()` to convert the given ISO-8601 date to a `datetime` object. +- Use `datetime.datetime.fromisoformat()` to convert the given ISO-8601 date to a `datetime.datetime` object. ```py from datetime import datetime