diff --git a/snippets/geometric_progression.md b/snippets/geometric_progression.md index ca234f7d6..752768b86 100644 --- a/snippets/geometric_progression.md +++ b/snippets/geometric_progression.md @@ -13,8 +13,9 @@ Returns an error if `step` equals `1`. ```py from math import floor, log -def geometric_progression(end, start = 1, step = 2): - return [start * step ** i for i in range(floor(log(end/start) / log(step)) + 1)] +def geometric_progression(end, start=1, step=2): + return [start * step ** i for i in range(floor(log(end / start) + / log(step)) + 1)] ``` ```py diff --git a/snippets/group_by.md b/snippets/group_by.md index 03b8c8184..b7e030243 100644 --- a/snippets/group_by.md +++ b/snippets/group_by.md @@ -5,9 +5,9 @@ tags: list,dictionary,intermediate Groups the elements of a list based on the given function. -- Use `defaultdict()` to initialize a dictionary. +- Use `collections.defaultdict` to initialize a dictionary. - Use `fn` in combination with a `for` loop and `dict.append()` to populate the dictionary. -- Use the `dict()` constructor to convert it to a regular dictionary. +- Use `dict()` to convert it to a regular dictionary. ```py from collections import defaultdict @@ -21,6 +21,7 @@ def group_by(lst, fn): ```py from math import floor + group_by([6.1, 4.2, 6.3], floor) # {4: [4.2], 6: [6.1, 6.3]} group_by(['one', 'two', 'three'], len) # {3: ['one', 'two'], 5: ['three']} ``` diff --git a/snippets/has_duplicates.md b/snippets/has_duplicates.md index be8a1e8e6..a088344de 100644 --- a/snippets/has_duplicates.md +++ b/snippets/has_duplicates.md @@ -3,7 +3,7 @@ title: has_duplicates tags: list,beginner --- -Returns `True` if there are duplicate values in a flat list, `False` otherwise. +Checks if there are duplicate values in a flat list. - Use `set()` on the given list to remove duplicates, compare its length with the length of the list. diff --git a/snippets/have_same_contents.md b/snippets/have_same_contents.md index a188a8e31..16d352926 100644 --- a/snippets/have_same_contents.md +++ b/snippets/have_same_contents.md @@ -3,7 +3,7 @@ title: have_same_contents tags: list,intermediate --- -Returns `True` if two lists contain the same elements regardless of order, `False` otherwise. +Checks if two lists contain the same elements regardless of order. - Use `set()` on the combination of both lists to find the unique values. - Iterate over them with a `for` loop comparing the `count()` of each unique value in each list. diff --git a/snippets/includes_all.md b/snippets/includes_all.md index 80a6e14fe..f102f423f 100644 --- a/snippets/includes_all.md +++ b/snippets/includes_all.md @@ -3,9 +3,10 @@ title: includes_all tags: list,intermediate --- -Returns `True` if all the elements in `values` are included in `lst`, `False` otherwise. +Checks if all the elements in `values` are included in `lst`. -- Check if every value in `values` is contained in `lst` using a `for` loop, returning `False` if any one value is not found, `True` otherwise. +- Check if every value in `values` is contained in `lst` using a `for` loop. +- Return `False` if any one value is not found, `True` otherwise. ```py def includes_all(lst, values): diff --git a/snippets/includes_any.md b/snippets/includes_any.md index 5f14d66e1..aef0d8e9d 100644 --- a/snippets/includes_any.md +++ b/snippets/includes_any.md @@ -3,9 +3,10 @@ title: includes_any tags: list,intermediate --- -Returns `True` if any element in `values` is included in `lst`, `False` otherwise. +Checks if any element in `values` is included in `lst`. -- Check if any value in `values` is contained in `lst` using a `for` loop, returning `True` if any one value is found, `False` otherwise. +- Check if any value in `values` is contained in `lst` using a `for` loop. +- Return `True` if any one value is found, `False` otherwise. ```py def includes_any(lst, values): diff --git a/snippets/initial.md b/snippets/initial.md index 41c3cc373..2afa69374 100644 --- a/snippets/initial.md +++ b/snippets/initial.md @@ -13,5 +13,5 @@ def initial(lst): ``` ```py -initial([1, 2, 3]) # [1,2] +initial([1, 2, 3]) # [1, 2] ``` diff --git a/snippets/initialize_2d_list.md b/snippets/initialize_2d_list.md index c7941c72f..a98aac79d 100644 --- a/snippets/initialize_2d_list.md +++ b/snippets/initialize_2d_list.md @@ -5,14 +5,14 @@ tags: list,intermediate Initializes a 2D list of given width and height and value. -- Use list comprehension and `range()` to generate `h` rows where each is a list with length `h`, initialized with `val`. -- If `val` is not provided, default to `None`. +- Use a list comprehension and `range()` to generate `h` rows where each is a list with length `h`, initialized with `val`. +- Omit the last argument, `val`, to set the default value to `None`. ```py -def initialize_2d_list(w,h, val = None): +def initialize_2d_list(w, h, val = None): return [[val for x in range(w)] for y in range(h)] ``` ```py -initialize_2d_list(2, 2, 0) # [[0,0], [0,0]] +initialize_2d_list(2, 2, 0) # [[0, 0], [0, 0]] ``` diff --git a/snippets/initialize_list_with_range.md b/snippets/initialize_list_with_range.md index 41959f78f..f126a41f5 100644 --- a/snippets/initialize_list_with_range.md +++ b/snippets/initialize_list_with_range.md @@ -5,12 +5,12 @@ tags: list,beginner Initializes a list containing the numbers in the specified range where `start` and `end` are inclusive with their common difference `step`. -- Use `list` and `range()` to generate a list of the appropriate length, filled with the desired values in the given range. +- Use `list()` and `range()` to generate a list of the appropriate length, filled with the desired values in the given range. - Omit `start` to use the default value of `0`. - Omit `step` to use the default value of `1`. ```py -def initialize_list_with_range(end, start=0, step=1): +def initialize_list_with_range(end, start = 0, step = 1): return list(range(start, end + 1, step)) ``` diff --git a/snippets/initialize_list_with_values.md b/snippets/initialize_list_with_values.md index 07314a6c7..6fe5aab73 100644 --- a/snippets/initialize_list_with_values.md +++ b/snippets/initialize_list_with_values.md @@ -5,7 +5,7 @@ tags: list,beginner Initializes and fills a list with the specified value. -- Use list comprehension and `range()` to generate a list of length equal to `n`, filled with the desired values. +- Use a list comprehension and `range()` to generate a list of length equal to `n`, filled with the desired values. - Omit `val` to use the default value of `0`. ```py diff --git a/snippets/intersection.md b/snippets/intersection.md index 5ca26a174..5a1766023 100644 --- a/snippets/intersection.md +++ b/snippets/intersection.md @@ -5,7 +5,8 @@ tags: list,beginner Returns a list of elements that exist in both lists. -- Create a `set` from `a` and `b`, then use the built-in set operator `&` to only keep values contained in both sets, then transform the `set` back into a `list`. +- Create a `set` from `a` and `b`. +- Use the built-in set operator `&` to only keep values contained in both sets, then transform the `set` back into a `list`. ```py def intersection(a, b): diff --git a/snippets/intersection_by.md b/snippets/intersection_by.md index 2a8ef7ad0..ceedde049 100644 --- a/snippets/intersection_by.md +++ b/snippets/intersection_by.md @@ -5,7 +5,8 @@ tags: list,function,intermediate Returns a list of elements that exist in both 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 contained in both lists. +- 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 contained in both lists. ```py def intersection_by(a, b, fn): @@ -15,5 +16,6 @@ def intersection_by(a, b, fn): ```py from math import floor -intersection_by([2.1, 1.2], [2.3, 3.4],floor) # [2.1] -``` \ No newline at end of file + +intersection_by([2.1, 1.2], [2.3, 3.4], floor) # [2.1] +``` diff --git a/snippets/invert_dictionary.md b/snippets/invert_dictionary.md index ac7f34f5d..e8c3a37aa 100644 --- a/snippets/invert_dictionary.md +++ b/snippets/invert_dictionary.md @@ -14,9 +14,9 @@ def invert_dictionary(obj): ```py ages = { - "Peter": 10, - "Isabel": 11, - "Anna": 9, + 'Peter': 10, + 'Isabel': 11, + 'Anna': 9, } -invert_dictionary(ages) # { 10: "Peter", 11 "Isabel", 9: "Anna" } +invert_dictionary(ages) # { 10: 'Peter', 11: 'Isabel', 9: 'Anna' } ``` diff --git a/snippets/is_anagram.md b/snippets/is_anagram.md index 55543f353..c84a38c1e 100644 --- a/snippets/is_anagram.md +++ b/snippets/is_anagram.md @@ -5,7 +5,7 @@ tags: string,intermediate Checks if a string is an anagram of another string (case-insensitive, ignores spaces, punctuation and special characters). -- Use `isalnum()` to filter out non-alphanumeric characters, `lower()` to transform each character to lowercase. +- Use `str.isalnum()` to filter out non-alphanumeric characters, `str.lower()` to transform each character to lowercase. - Use `collections.Counter` to count the resulting characters for each string and compare the results. ```py @@ -20,5 +20,5 @@ def is_anagram(s1, s2): ``` ```py -is_anagram("#anagram", "Nag a ram!") # True +is_anagram('#anagram', 'Nag a ram!') # True ``` diff --git a/snippets/is_contained_in.md b/snippets/is_contained_in.md index 1ec234cb7..d19f9d244 100644 --- a/snippets/is_contained_in.md +++ b/snippets/is_contained_in.md @@ -3,9 +3,10 @@ title: is_contained_in tags: list,intermediate --- -Returns `True` if the elements of the first list are contained in the second one regardless of order, `False` otherwise. +Checks if the elements of the first list are contained in the second one regardless of order. -- Use `count()` to check if any value in `a` has more occurences than it has in `b`, returning `False` if any such value is found, `True` otherwise. +- Use `count()` to check if any value in `a` has more occurences than it has in `b`. +- Return `False` if any such value is found, `True` otherwise. ```py def is_contained_in(a, b): diff --git a/snippets/is_even.md b/snippets/is_even.md index e5951808e..25f18bfd3 100644 --- a/snippets/is_even.md +++ b/snippets/is_even.md @@ -3,10 +3,10 @@ title: is_even tags: math,beginner --- -Returns `True` if the given number is even, `False` otherwise. +Checks if the given number is even. -- Checks whether a number is odd or even using the modulo (`%`) operator. -- Returns `True` if the number is even, `False` if the number is odd. +- Check whether a number is odd or even using the modulo (`%`) operator. +- Return `True` if the number is even, `False` if the number is odd. ```py def is_even(num): diff --git a/snippets/is_odd.md b/snippets/is_odd.md index ea199b423..0f6565f2e 100644 --- a/snippets/is_odd.md +++ b/snippets/is_odd.md @@ -3,7 +3,7 @@ title: is_odd tags: math,beginner --- -Returns `True` if the given number is odd, `False` otherwise. +Checks if the given number is odd. - Checks whether a number is even or odd using the modulo (`%`) operator. - Returns `True` if the number is odd, `False` if the number is even. diff --git a/snippets/is_prime.md b/snippets/is_prime.md index 29f8c3b96..1e2bf61e6 100644 --- a/snippets/is_prime.md +++ b/snippets/is_prime.md @@ -6,7 +6,8 @@ tags: math,intermediate Checks if the provided integer is a prime number. - Return `False` if the number is `0`, `1`, a negative number or a multiple of `2`. -- Use `all()` and `range()` to check numbers from `3` to the square root of the given number, returning `True` if none divides the given number, `False` otherwise. +- Use `all()` and `range()` to check numbers from `3` to the square root of the given number. +- Return `True` if none divides the given number, `False` otherwise. ```py from math import sqrt diff --git a/snippets/is_weekday.md b/snippets/is_weekday.md index d20b587e4..da0b6b55e 100644 --- a/snippets/is_weekday.md +++ b/snippets/is_weekday.md @@ -19,6 +19,6 @@ def is_weekday(d = datetime.today()): ```py from datetime import date -is_weekday(date(2020,10,25)) # False -is_weekday(date(2020,10,28)) # True +is_weekday(date(2020, 10, 25)) # False +is_weekday(date(2020, 10, 28)) # True ``` diff --git a/snippets/is_weekend.md b/snippets/is_weekend.md index f390693e5..8e7d0aa01 100644 --- a/snippets/is_weekend.md +++ b/snippets/is_weekend.md @@ -19,6 +19,6 @@ def is_weekend(d = datetime.today()): ```py from datetime import date -is_weekend(date(2020,10,25)) # True -is_weekend(date(2020,10,28)) # False +is_weekend(date(2020, 10, 25)) # True +is_weekend(date(2020, 10, 28)) # False ``` diff --git a/snippets/kebab.md b/snippets/kebab.md index e9c7dcead..32caf503b 100644 --- a/snippets/kebab.md +++ b/snippets/kebab.md @@ -5,7 +5,9 @@ tags: string,regexp,intermediate Converts a string to kebab case. -- Break the string into words and combine them adding `-` as a separator, using a regexp. +- Use `re.sub()` to replace any `-` or `_` with a space, using the regexp `r"(_|-)+"`. +- Use `re.sub()` to match all words in the string, `str.lower()` to lowercase them. +- Finally, use `str.join()` to combine all word using `-` as the separator. ```py from re import sub @@ -20,6 +22,7 @@ def kebab(s): ```py kebab('camelCase') # 'camel-case' kebab('some text') # 'some-text' -kebab('some-mixed_string With spaces_underscores-and-hyphens') # 'some-mixed-string-with-spaces-underscores-and-hyphens' -kebab('AllThe-small Things') # "all-the-small-things" +kebab('some-mixed_string With spaces_underscores-and-hyphens') +# 'some-mixed-string-with-spaces-underscores-and-hyphens' +kebab('AllThe-small Things') # 'all-the-small-things' ``` diff --git a/snippets/keys_only.md b/snippets/keys_only.md index a959f8872..f7c1c5fc2 100644 --- a/snippets/keys_only.md +++ b/snippets/keys_only.md @@ -3,7 +3,7 @@ title: keys_only tags: dictionary,list,beginner --- -Returns a flat list of all the keys in a flat dictionary. +Creates a flat list of all the keys in a flat dictionary. - Use `dict.keys()` to return the keys in the given dictionary. - Return a `list()` of the previous result. @@ -15,9 +15,9 @@ def keys_only(flat_dict): ```py ages = { - "Peter": 10, - "Isabel": 11, - "Anna": 9, + 'Peter': 10, + 'Isabel': 11, + 'Anna': 9, } keys_only(ages) # ['Peter', 'Isabel', 'Anna'] -``` \ No newline at end of file +``` diff --git a/snippets/last.md b/snippets/last.md index 8d6289598..c8bd073b9 100644 --- a/snippets/last.md +++ b/snippets/last.md @@ -5,7 +5,7 @@ tags: list,beginner Returns the last element in a list. -- use `lst[-1]` to return the last element of the passed list. +- Use `lst[-1]` to return the last element of the passed list. ```py def last(lst): diff --git a/snippets/longest_item.md b/snippets/longest_item.md index d5fe5401e..cf3ad527e 100644 --- a/snippets/longest_item.md +++ b/snippets/longest_item.md @@ -4,13 +4,13 @@ tags: list,string,intermediate --- Takes any number of iterable objects or objects with a length property and returns the longest one. -If multiple objects have the same length, the first one will be returned. -- Use `max()` with `len` as the `key` to return the item with the greatest length. +- Use `max()` with `len()` as the `key` to return the item with the greatest length. +- If multiple objects have the same length, the first one will be returned. ```py def longest_item(*args): - return max(args, key=len) + return max(args, key = len) ``` ```py