From bff03eb7f4ed18e3b93fc4ae6947c9e275b23d12 Mon Sep 17 00:00:00 2001 From: Isabelle Viktoria Maciohsek Date: Mon, 2 Nov 2020 19:28:35 +0200 Subject: [PATCH] Update snippet descriptions --- snippets/shuffle.md | 6 +++--- snippets/similarity.md | 2 +- snippets/snake.md | 9 ++++++--- snippets/some.md | 8 ++++---- snippets/sort_by_indexes.md | 8 +++++--- snippets/sort_dict_by_key.md | 3 ++- snippets/sort_dict_by_value.md | 3 ++- snippets/split_lines.md | 5 +++-- snippets/sum_by.md | 7 ++++--- snippets/sum_of_powers.md | 3 ++- snippets/symmetric_difference.md | 10 ++++++---- snippets/symmetric_difference_by.md | 15 +++++++++------ snippets/tail.md | 5 +++-- snippets/to_dictionary.md | 2 +- snippets/to_iso_date.md | 4 ++-- snippets/to_roman_numeral.md | 3 ++- snippets/transpose.md | 7 ++++--- snippets/unfold.md | 2 +- snippets/union.md | 2 +- snippets/union_by.md | 6 ++++-- snippets/values_only.md | 6 +++--- snippets/weighted_average.md | 2 +- snippets/words.md | 5 +++-- 23 files changed, 72 insertions(+), 51 deletions(-) diff --git a/snippets/shuffle.md b/snippets/shuffle.md index 103209a51..5c6c1fbbf 100644 --- a/snippets/shuffle.md +++ b/snippets/shuffle.md @@ -1,6 +1,6 @@ --- title: shuffle -tags: list,random,intermediate +tags: list,random,advanced --- Randomizes the order of the values of an list, returning a new list. @@ -23,6 +23,6 @@ def shuffle(lst): ``` ```py -foo = [1,2,3] -shuffle(foo) # [2,3,1], foo = [1,2,3] +foo = [1, 2, 3] +shuffle(foo) # [2, 3, 1], foo = [1, 2, 3] ``` diff --git a/snippets/similarity.md b/snippets/similarity.md index d5996cc1a..7c3d3e117 100644 --- a/snippets/similarity.md +++ b/snippets/similarity.md @@ -5,7 +5,7 @@ tags: list,beginner Returns a list of elements that exist in both lists. -- Use list comprehension on `a` to only keep values contained in both lists. +- Use a list comprehension on `a` to only keep values contained in both lists. ```py def similarity(a, b): diff --git a/snippets/snake.md b/snippets/snake.md index ae67573a4..5a19b3152 100644 --- a/snippets/snake.md +++ b/snippets/snake.md @@ -5,7 +5,9 @@ tags: string,regexp,intermediate Converts a string to snake case. -- Break the string into words and combine them adding `_` as a separator, using a regexp. +- Use `re.sub()` to match all words in the string, `str.lower()` to lowercase them. +- Use `re.sub()` to replace any `-` characters with spaces. +- Finally, use `str.join()` to combine all words using `-` as the separator. ```py from re import sub @@ -20,6 +22,7 @@ def snake(s): ```py snake('camelCase') # 'camel_case' snake('some text') # 'some_text' -snake('some-mixed_string With spaces_underscores-and-hyphens') # 'some_mixed_string_with_spaces_underscores_and_hyphens' -snake('AllThe-small Things') # "all_the_small_things" +snake('some-mixed_string With spaces_underscores-and-hyphens') +# 'some_mixed_string_with_spaces_underscores_and_hyphens' +snake('AllThe-small Things') # 'all_the_small_things' ``` diff --git a/snippets/some.md b/snippets/some.md index 25915566f..283cef604 100644 --- a/snippets/some.md +++ b/snippets/some.md @@ -1,14 +1,14 @@ --- title: some -tags: list,function,intermediate +tags: list,intermediate --- -Returns `True` if the provided function returns `True` for at least one element in the list, `False` otherwise. +Checks if the provided function returns `True` for at least one element in the list. -- Use `any()` in combination with `map()` and `fn` to check if `fn` returns `True` for any element in the list. +- Use `any()` in combination with `map()` to check if `fn` returns `True` for any element in the list. ```py -def some(lst, fn=lambda x: x): +def some(lst, fn = lambda x: x): return any(map(fn, lst)) ``` diff --git a/snippets/sort_by_indexes.md b/snippets/sort_by_indexes.md index dda61e33a..e227e096e 100644 --- a/snippets/sort_by_indexes.md +++ b/snippets/sort_by_indexes.md @@ -10,13 +10,15 @@ Sorts one list based on another list containing the desired indexes. - Use the `reverse` parameter in `sorted()` to sort the dictionary in reverse order, based on the third argument. ```py -def sort_by_indexes(lst, indexes, reverse = False): - return [val for _, val in sorted(zip(indexes, lst), key = lambda x: x[0], reverse = reverse)] +def sort_by_indexes(lst, indexes, reverse=False): + return [val for (_, val) in sorted(zip(indexes, lst), key=lambda x: \ + x[0], reverse=reverse)] ``` ```py a = ['eggs', 'bread', 'oranges', 'jam', 'apples', 'milk'] b = [3, 2, 6, 4, 1, 5] sort_by_indexes(a, b) # ['apples', 'bread', 'eggs', 'jam', 'milk', 'oranges'] -sort_by_indexes(a, b, True) # ['oranges', 'milk', 'jam', 'eggs', 'bread', 'apples'] +sort_by_indexes(a, b, True) +# ['oranges', 'milk', 'jam', 'eggs', 'bread', 'apples'] ``` diff --git a/snippets/sort_dict_by_key.md b/snippets/sort_dict_by_key.md index 8d7fcc5c5..b5d5b7611 100644 --- a/snippets/sort_dict_by_key.md +++ b/snippets/sort_dict_by_key.md @@ -17,5 +17,6 @@ def sort_dict_by_key(d, reverse = False): ```py d = {'one': 1, 'three': 3, 'five': 5, 'two': 2, 'four': 4} sort_dict_by_key(d) # {'five': 5, 'four': 4, 'one': 1, 'three': 3, 'two': 2} -sort_dict_by_key(d, True) # {'two': 2, 'three': 3, 'one': 1, 'four': 4, 'five': 5} +sort_dict_by_key(d, True) +# {'two': 2, 'three': 3, 'one': 1, 'four': 4, 'five': 5} ``` diff --git a/snippets/sort_dict_by_value.md b/snippets/sort_dict_by_value.md index 04299bac0..f2224a186 100644 --- a/snippets/sort_dict_by_value.md +++ b/snippets/sort_dict_by_value.md @@ -18,5 +18,6 @@ def sort_dict_by_value(d, reverse = False): ```py d = {'one': 1, 'three': 3, 'five': 5, 'two': 2, 'four': 4} sort_dict_by_value(d) # {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5} -sort_dict_by_value(d, True) # {'five': 5, 'four': 4, 'three': 3, 'two': 2, 'one': 1} +sort_dict_by_value(d, True) +# {'five': 5, 'four': 4, 'three': 3, 'two': 2, 'one': 1} ``` diff --git a/snippets/split_lines.md b/snippets/split_lines.md index f0481112b..e447c7b1a 100644 --- a/snippets/split_lines.md +++ b/snippets/split_lines.md @@ -5,7 +5,7 @@ tags: string,beginner Splits a multiline string into a list of lines. -- Use `s.split()` and `'\n'` to match line breaks and create a list. +- Use `str.split()` and `'\n'` to match line breaks and create a list. - [`str.splitlines()`](https://docs.python.org/3/library/stdtypes.html#str.splitlines) provides similar functionality to this snippet. ```py @@ -14,5 +14,6 @@ def split_lines(s): ``` ```py -split_lines('This\nis a\nmultiline\nstring.\n') # ['This', 'is a', 'multiline', 'string.' , ''] +split_lines('This\nis a\nmultiline\nstring.\n') +# ['This', 'is a', 'multiline', 'string.' , ''] ``` diff --git a/snippets/sum_by.md b/snippets/sum_by.md index 11c7982a6..3cd5ae8f0 100644 --- a/snippets/sum_by.md +++ b/snippets/sum_by.md @@ -1,11 +1,12 @@ --- title: sum_by -tags: math,list,function,beginner +tags: math,list,beginner --- -Returns the sum of a list, after mapping each element to a value using the provided function. +Calculates the sum of a list, after mapping each element to a value using the provided function. -- Use `map()` with `fn` to map each element to a value using the provided function, use `sum()` to return the sum of the values. +- Use `map()` with `fn` to map each element to a value using the provided function. +- Use `sum()` to return the sum of the values. ```py def sum_by(lst, fn): diff --git a/snippets/sum_of_powers.md b/snippets/sum_of_powers.md index b62fe7bd1..1e85bb7d1 100644 --- a/snippets/sum_of_powers.md +++ b/snippets/sum_of_powers.md @@ -5,7 +5,8 @@ tags: math,intermediate Returns the sum of the powers of all the numbers from `start` to `end` (both inclusive). -- Use `range()` in combination with a list comprehension to create a list of elements in the desired range raised to the given `power`, `sum()` to add the values together. +- Use `range()` in combination with a list comprehension to create a list of elements in the desired range raised to the given `power`. +- Use `sum()` to add the values together. - Omit the second argument, `power`, to use a default power of `2`. - Omit the third argument, `start`, to use a default starting value of `1`. diff --git a/snippets/symmetric_difference.md b/snippets/symmetric_difference.md index 5646223ab..d1d24ebd0 100644 --- a/snippets/symmetric_difference.md +++ b/snippets/symmetric_difference.md @@ -1,16 +1,18 @@ --- title: symmetric_difference -tags: list,beginner +tags: list,intermediate --- Returns the symmetric difference between two iterables, without filtering out duplicate values. -- Create a `set` from each list, then use list comprehension on each one to only keep values not contained in the previously created set of the other. +- Create a `set` from each list. +- Use a list comprehension on each of them to only keep values not contained in the previously created set of the other. ```py def symmetric_difference(a, b): - _a, _b = set(a), set(b) - return [item for item in a if item not in _b] + [item for item in b if item not in _a] + (_a, _b) = (set(a), set(b)) + return [item for item in a if item not in _b] + [item for item in b + if item not in _a] ``` ```py diff --git a/snippets/symmetric_difference_by.md b/snippets/symmetric_difference_by.md index 3d7e22f1c..ee51c2a3a 100644 --- a/snippets/symmetric_difference_by.md +++ b/snippets/symmetric_difference_by.md @@ -1,19 +1,22 @@ --- title: symmetric_difference_by -tags: list,function,intermediate +tags: list,intermediate --- Returns the symmetric 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 every list, then use list comprehension in combination with `fn` on each one to only keep values not contained in the previously created set of the other. +- Create a `set` by applying `fn` to each element in every list. +- Use a list comprehension in combination with `fn` on each of them to only keep values not contained in the previously created set of the other. ```py def symmetric_difference_by(a, b, fn): - _a, _b = set(map(fn, a)), set(map(fn, b)) - return [item for item in a if fn(item) not in _b] + [item for item in b if fn(item) not in _a] + (_a, _b) = (set(map(fn, a)), set(map(fn, b))) + return [item for item in a if fn(item) not in _b] + [item + for item in b if fn(item) not in _a] ``` ```py from math import floor -symmetric_difference_by([2.1, 1.2], [2.3, 3.4],floor) # [1.2, 3.4] -``` \ No newline at end of file + +symmetric_difference_by([2.1, 1.2], [2.3, 3.4], floor) # [1.2, 3.4] +``` diff --git a/snippets/tail.md b/snippets/tail.md index a23277b81..4f46c7394 100644 --- a/snippets/tail.md +++ b/snippets/tail.md @@ -5,7 +5,8 @@ tags: list,beginner Returns all elements in a list except for the first one. -- Return `lst[1:]` if the list's length is more than `1`, otherwise, return the whole list. +- Use slice notation to return the last element if the list's length is more than `1`. +- Otherwise, return the whole list. ```py def tail(lst): @@ -13,6 +14,6 @@ def tail(lst): ``` ```py -tail([1, 2, 3]) # [2,3] +tail([1, 2, 3]) # [2, 3] tail([1]) # [1] ``` diff --git a/snippets/to_dictionary.md b/snippets/to_dictionary.md index 849622a5e..2da19bed7 100644 --- a/snippets/to_dictionary.md +++ b/snippets/to_dictionary.md @@ -6,7 +6,7 @@ tags: list,dictionary,intermediate Combines two lists into a dictionary, where the elements of the first one serve as the keys and the elements of the second one serve as the values. The values of the first list need to be unique and hashable. -- Use `zip()` in combination with the `dict()` constructor to combine the values of the two lists into a dictionary. +- Use `zip()` in combination with `dict()` to combine the values of the two lists into a dictionary. ```py def to_dictionary(keys, values): diff --git a/snippets/to_iso_date.md b/snippets/to_iso_date.md index f192c4867..2caff6055 100644 --- a/snippets/to_iso_date.md +++ b/snippets/to_iso_date.md @@ -5,7 +5,7 @@ tags: date,intermediate Converts a date to its ISO-8601 represenation. -- Use `datetime.datetime.isoformat()` to convert the given `datetime` object to an ISO-8601 date. +- Use `datetime.datetime.isoformat()` to convert the given `datetime.datetime` object to an ISO-8601 date. ```py from datetime import datetime @@ -17,5 +17,5 @@ def to_iso_date(d): ```py from datetime import datetime -to_iso_date(datetime(2020,10,25)) # 2020-10-25T00:00:00 +to_iso_date(datetime(2020, 10, 25)) # 2020-10-25T00:00:00 ``` diff --git a/snippets/to_roman_numeral.md b/snippets/to_roman_numeral.md index 84bc7762f..5644daf3b 100644 --- a/snippets/to_roman_numeral.md +++ b/snippets/to_roman_numeral.md @@ -7,7 +7,8 @@ Converts an integer to its roman numeral representation. Accepts value between `1` and `3999` (both inclusive). - Create a lookup list containing tuples in the form of (roman value, integer). -- Use a `for` loop to iterate over the values in `lookup`, `divmod()` to update `num` with the remainder, adding the roman numeral representation to the result. +- Use a `for` loop to iterate over the values in `lookup`. +- Use `divmod()` to update `num` with the remainder, adding the roman numeral representation to the result. ```py def to_roman_numeral(num): diff --git a/snippets/transpose.md b/snippets/transpose.md index f8f321d99..d020bc382 100644 --- a/snippets/transpose.md +++ b/snippets/transpose.md @@ -3,9 +3,9 @@ title: transpose tags: list,intermediate --- -Returns the transpose of a two-dimensional list. +Transposes a two-dimensional list. -- Use `*lst` to get the passed list as tuples. +- Use `*lst` to get the provided list as tuples. - Use `zip()` in combination with `list()` to create the transpose of the given two-dimensional list. ```py @@ -14,5 +14,6 @@ def transpose(lst): ``` ```py -transpose([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]) # [(1, 4, 7, 10), (2, 5, 8, 11), (3, 6, 9, 12)] +transpose([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]) +# [(1, 4, 7, 10), (2, 5, 8, 11), (3, 6, 9, 12)] ``` diff --git a/snippets/unfold.md b/snippets/unfold.md index ecafd4d15..7489d1b2b 100644 --- a/snippets/unfold.md +++ b/snippets/unfold.md @@ -7,7 +7,7 @@ Builds a list, using an iterator function and an initial seed value. - The iterator function accepts one argument (`seed`) and must always return a list with two elements ([`value`, `nextSeed`]) or `False` to terminate. - Use a generator function, `fn_generator`, that uses a `while` loop to call the iterator function and `yield` the `value` until it returns `False`. -- Use list comprehension to return the list that is produced by the generator, using the iterator function. +- Use a list comprehension to return the list that is produced by the generator, using the iterator function. ```py def unfold(fn, seed): diff --git a/snippets/union.md b/snippets/union.md index 3e52908ab..57bd56567 100644 --- a/snippets/union.md +++ b/snippets/union.md @@ -13,5 +13,5 @@ def union(a, b): ``` ```py -union([1, 2, 3], [4, 3, 2]) # [1,2,3,4] +union([1, 2, 3], [4, 3, 2]) # [1, 2, 3, 4] ``` diff --git a/snippets/union_by.md b/snippets/union_by.md index 4ece1bb06..b0b98b464 100644 --- a/snippets/union_by.md +++ b/snippets/union_by.md @@ -1,11 +1,12 @@ --- title: union_by -tags: list,function,intermediate +tags: list,intermediate --- Returns every element that exists in any of the two lists once, after applying the provided function to each element of both. -- Create a `set` by applying `fn` to each element in `a`, then use list comprehension in combination with `fn` on `b` to only keep values not contained in the previously created set, `_a`. +- Create a `set` by applying `fn` to each element in `a`. +- Use a list comprehension in combination with `fn` on `b` to only keep values not contained in the previously created set, `_a`. - Finally, create a `set` from the previous result and `a` and transform it into a `list` ```py @@ -16,5 +17,6 @@ def union_by(a, b, fn): ```py from math import floor + union_by([2.1], [1.2, 2.3], floor) # [2.1, 1.2] ``` diff --git a/snippets/values_only.md b/snippets/values_only.md index 13cf189a8..fa74e6a7f 100644 --- a/snippets/values_only.md +++ b/snippets/values_only.md @@ -15,9 +15,9 @@ def values_only(flat_dict): ```py ages = { - "Peter": 10, - "Isabel": 11, - "Anna": 9, + 'Peter': 10, + 'Isabel': 11, + 'Anna': 9, } values_only(ages) # [10, 11, 9] ``` diff --git a/snippets/weighted_average.md b/snippets/weighted_average.md index 91ace1ece..f9b3bb518 100644 --- a/snippets/weighted_average.md +++ b/snippets/weighted_average.md @@ -14,5 +14,5 @@ def weighted_average(nums, weights): ``` ```py -weighted_average([1, 2, 3], [0.6,0.2,0.3]) # 1.72727 +weighted_average([1, 2, 3], [0.6, 0.2, 0.3]) # 1.72727 ``` diff --git a/snippets/words.md b/snippets/words.md index 7a4e58de4..e9182de16 100644 --- a/snippets/words.md +++ b/snippets/words.md @@ -3,7 +3,7 @@ title: words tags: string,regexp,beginner --- -Converts a given string into an array of words. +Converts a given string into a list of words. - Use `re.findall()` with the supplied `pattern` to find all matching substrings. - Omit the second argument to use the default regexp, which matches alphanumeric and hyphens. @@ -18,5 +18,6 @@ def words(s, pattern = '[a-zA-Z-]+'): ```py words('I love Python!!') # ['I', 'love', 'Python'] words('python, javaScript & coffee') # ['python', 'javaScript', 'coffee'] -words('build -q --out one-item', r'\b[a-zA-Z-]+\b') # ['build', 'q', 'out', 'one-item'] +words('build -q --out one-item', r'\b[a-zA-Z-]+\b') +# ['build', 'q', 'out', 'one-item'] ```