From 98b5f6741279b426d92913c22e04a02e4a1d4410 Mon Sep 17 00:00:00 2001 From: Isabelle Viktoria Maciohsek Date: Mon, 2 Nov 2020 19:28:27 +0200 Subject: [PATCH] Update snippet descriptions --- snippets/map_dictionary.md | 4 ++-- snippets/map_values.md | 3 +-- snippets/max_by.md | 5 +++-- snippets/max_element_index.md | 2 +- snippets/max_n.md | 11 ++++++----- snippets/median.md | 7 ++++--- snippets/merge.md | 9 +++++---- snippets/merge_dictionaries.md | 11 ++++++----- snippets/min_by.md | 5 +++-- snippets/min_element_index.md | 2 +- snippets/min_n.md | 11 ++++++----- snippets/most_frequent.md | 7 ++++--- snippets/n_times_string.md | 2 +- snippets/none.md | 6 +++--- snippets/num_to_range.md | 5 +++-- snippets/offset.md | 2 +- snippets/pad_number.md | 2 +- snippets/palindrome.md | 8 ++++---- snippets/powerset.md | 6 +++--- snippets/rads_to_degrees.md | 5 +++-- snippets/reverse.md | 2 +- snippets/reverse_number.md | 8 ++++---- snippets/rgb_to_hex.md | 4 ++-- 23 files changed, 68 insertions(+), 59 deletions(-) diff --git a/snippets/map_dictionary.md b/snippets/map_dictionary.md index 8aea5842d..7cf6adc9a 100644 --- a/snippets/map_dictionary.md +++ b/snippets/map_dictionary.md @@ -1,6 +1,6 @@ --- title: map_dictionary -tags: list,intermediate +tags: list,dictionary,intermediate --- Maps the values of a list to a dictionary using a function, where the key-value pairs consist of the original value as the key and the result of the function as the value. @@ -15,5 +15,5 @@ def map_dictionary(itr, fn): ``` ```py -map_dictionary([1,2,3], lambda x: x * x) # { 1: 1, 2: 4, 3: 9 } +map_dictionary([1, 2, 3], lambda x: x * x) # { 1: 1, 2: 4, 3: 9 } ``` diff --git a/snippets/map_values.md b/snippets/map_values.md index 4388b6883..ec40971cb 100644 --- a/snippets/map_values.md +++ b/snippets/map_values.md @@ -1,6 +1,6 @@ --- title: map_values -tags: dictionary,function,intermediate +tags: dictionary,intermediate --- Creates a dictionary with the same keys as the provided dictionary and values generated by running the provided function for each value. @@ -17,6 +17,5 @@ users = { 'fred': { 'user': 'fred', 'age': 40 }, 'pebbles': { 'user': 'pebbles', 'age': 1 } } - map_values(users, lambda u : u['age']) # {'fred': 40, 'pebbles': 1} ``` diff --git a/snippets/max_by.md b/snippets/max_by.md index 8807fc9ce..1d619d9de 100644 --- a/snippets/max_by.md +++ b/snippets/max_by.md @@ -1,11 +1,12 @@ --- title: max_by -tags: math,list,function,beginner +tags: math,list,beginner --- Returns the maximum value 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 `max()` to return the maximum value. +- Use `map()` with `fn` to map each element to a value using the provided function. +- Use `max()` to return the maximum value. ```py def max_by(lst, fn): diff --git a/snippets/max_element_index.md b/snippets/max_element_index.md index 563c3d660..fc9d02a8f 100644 --- a/snippets/max_element_index.md +++ b/snippets/max_element_index.md @@ -1,6 +1,6 @@ --- title: max_element_index -tags: list,beginner +tags: math,list,beginner --- Returns the index of the element with the maximum value in a list. diff --git a/snippets/max_n.md b/snippets/max_n.md index ed2111a5f..9e1269c16 100644 --- a/snippets/max_n.md +++ b/snippets/max_n.md @@ -4,17 +4,18 @@ tags: list,math,beginner --- Returns the `n` maximum elements from the provided list. -If `n` is greater than or equal to the provided list's length, then return the original list (sorted in descending order). -- Use `sorted()` to sort the list, `[:n]` to get the specified number of elements. +- Use `sorted()` to sort the list. +- Use slice notation to get the specified number of elements. - Omit the second argument, `n`, to get a one-element list. +- If `n` is greater than or equal to the provided list's length, then return the original list (sorted in descending order). ```py -def max_n(lst, n=1): - return sorted(lst, reverse=True)[:n] +def max_n(lst, n = 1): + return sorted(lst, reverse = True)[:n] ``` ```py max_n([1, 2, 3]) # [3] -max_n([1, 2, 3], 2) # [3,2] +max_n([1, 2, 3], 2) # [3, 2] ``` diff --git a/snippets/median.md b/snippets/median.md index 6de64941b..d329ab88a 100644 --- a/snippets/median.md +++ b/snippets/median.md @@ -5,7 +5,8 @@ tags: math,beginner Finds the median of a list of numbers. -- Sort the numbers of the list using `list.sort()` and find the median, which is either the middle element of the list if the list length is odd or the average of the two middle elements if the list length is even. +- Sort the numbers of the list using `list.sort()`. +- Find the median, which is either the middle element of the list if the list length is odd or the average of the two middle elements if the list length is even. - [`statistics.median()`](https://docs.python.org/3/library/statistics.html#statistics.median) provides similar functionality to this snippet. ```py @@ -18,6 +19,6 @@ def median(list): ``` ```py -median([1,2,3]) # 2.0 -median([1,2,3,4]) # 2.5 +median([1, 2, 3]) # 2.0 +median([1, 2, 3, 4]) # 2.5 ``` diff --git a/snippets/merge.md b/snippets/merge.md index 61cb65944..5e200ebaa 100644 --- a/snippets/merge.md +++ b/snippets/merge.md @@ -1,17 +1,17 @@ --- title: merge -tags: list,math,advanced +tags: list,advanced --- Merges two or more lists into a list of lists, combining elements from each of the input lists based on their positions. -- Use `max` combined with list comprehension to get the length of the longest list in the arguments. +- Use `max()` combined with a list comprehension to get the length of the longest list in the arguments. - Use `range()` in combination with the `max_length` variable to loop as many times as there are elements in the longest list. - If a list is shorter than `max_length`, use `fill_value` for the remaining items (defaults to `None`). - [`zip()`](https://docs.python.org/3/library/functions.html#zip) and [`itertools.zip_longest()`](https://docs.python.org/3/library/itertools.html#itertools.zip_longest) provide similar functionality to this snippet. ```py -def merge(*args, fill_value=None): +def merge(*args, fill_value = None): max_length = max([len(lst) for lst in args]) result = [] for i in range(max_length): @@ -24,5 +24,6 @@ def merge(*args, fill_value=None): ```py merge(['a', 'b'], [1, 2], [True, False]) # [['a', 1, True], ['b', 2, False]] merge(['a'], [1, 2], [True, False]) # [['a', 1, True], [None, 2, False]] -merge(['a'], [1, 2], [True, False], fill_value = '_') # [['a', 1, True], ['_', 2, False]] +merge(['a'], [1, 2], [True, False], fill_value = '_') +# [['a', 1, True], ['_', 2, False]] ``` diff --git a/snippets/merge_dictionaries.md b/snippets/merge_dictionaries.md index 34d98f6e7..a0d18f47b 100644 --- a/snippets/merge_dictionaries.md +++ b/snippets/merge_dictionaries.md @@ -5,7 +5,7 @@ tags: dictionary,intermediate Merges two or more dictionaries. -- Create a new `dict()` and loop over `dicts`, using `dictionary.update()` to add the key-value pairs from each one to the result. +- Create a new `dict` and loop over `dicts`, using `dictionary.update()` to add the key-value pairs from each one to the result. ```py def merge_dictionaries(*dicts): @@ -17,11 +17,12 @@ def merge_dictionaries(*dicts): ```py ages_one = { - "Peter": 10, - "Isabel": 11, + 'Peter': 10, + 'Isabel': 11, } ages_two = { - "Anna": 9 + 'Anna': 9 } -merge_dictionaries(ages_one, ages_two) # { "Peter": 10, "Isabel": 11, "Anna": 9 } +merge_dictionaries(ages_one, ages_two) +# { 'Peter': 10, 'Isabel': 11, 'Anna': 9 } ``` diff --git a/snippets/min_by.md b/snippets/min_by.md index 94380173a..289aba7ed 100644 --- a/snippets/min_by.md +++ b/snippets/min_by.md @@ -1,11 +1,12 @@ --- title: min_by -tags: math,list,function,beginner +tags: math,list,beginner --- Returns the minimum value 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 `min()` to return the minimum value. +- Use `map()` with `fn` to map each element to a value using the provided function. +- Use `min()` to return the minimum value. ```py def min_by(lst, fn): diff --git a/snippets/min_element_index.md b/snippets/min_element_index.md index 55aa63248..279f30d17 100644 --- a/snippets/min_element_index.md +++ b/snippets/min_element_index.md @@ -1,6 +1,6 @@ --- title: min_element_index -tags: list,beginner +tags: math,list,beginner --- Returns the index of the element with the minimum value in a list. diff --git a/snippets/min_n.md b/snippets/min_n.md index ec2c27330..dccf3ad24 100644 --- a/snippets/min_n.md +++ b/snippets/min_n.md @@ -4,17 +4,18 @@ tags: list,math,beginner --- Returns the `n` minimum elements from the provided list. -If `n` is greater than or equal to the provided list's length, then return the original list (sorted in ascending order). -- Use `sorted() to sort the list, `[:n]` to get the specified number of elements. +- Use `sorted()` to sort the list. +- Use slice notation to get the specified number of elements. - Omit the second argument, `n`, to get a one-element list. +- If `n` is greater than or equal to the provided list's length, then return the original list (sorted in ascending order). ```py -def min_n(lst, n=1): - return sorted(lst, reverse=False)[:n] +def min_n(lst, n = 1): + return sorted(lst, reverse = False)[:n] ``` ```py min_n([1, 2, 3]) # [1] -min_n([1, 2, 3], 2) # [1,2] +min_n([1, 2, 3], 2) # [1, 2] ``` diff --git a/snippets/most_frequent.md b/snippets/most_frequent.md index 3d932894d..fea931a72 100644 --- a/snippets/most_frequent.md +++ b/snippets/most_frequent.md @@ -5,11 +5,12 @@ tags: list,beginner Returns the most frequent element in a list. -- Use `set(list)` to get the unique values in the `list` combined with `max()` to find the element that has the most appearances. +- Use `set()` to get the unique values in `lst`. +- Use `max()` to find the element that has the most appearances. ```py -def most_frequent(list): - return max(set(list), key=list.count) +def most_frequent(lst): + return max(set(lst), key = lst.count) ``` ```py diff --git a/snippets/n_times_string.md b/snippets/n_times_string.md index 71461600b..0a995a7b6 100644 --- a/snippets/n_times_string.md +++ b/snippets/n_times_string.md @@ -3,7 +3,7 @@ title: n_times_string tags: string,beginner --- -Prints out the same string a defined number of times. +Generates a string with the given string value repeated `n` number of times. - Repeat the string `n` times, using the `*` operator. diff --git a/snippets/none.md b/snippets/none.md index a86c131c2..019aa1aa0 100644 --- a/snippets/none.md +++ b/snippets/none.md @@ -1,14 +1,14 @@ --- title: none -tags: list,function,intermediate +tags: list,intermediate --- -Returns `False` if the provided function returns `True` for at least one element in the list, `True` otherwise. +Checks if the provided function returns `True` for at least one element in the list. - Use `all()` and `fn` to check if `fn` returns `False` for all the elements in the list. ```py -def none(lst, fn=lambda x: x): +def none(lst, fn = lambda x: x): return all(not fn(x) for x in lst) ``` diff --git a/snippets/num_to_range.md b/snippets/num_to_range.md index 05c1dcbd8..750100f78 100644 --- a/snippets/num_to_range.md +++ b/snippets/num_to_range.md @@ -5,11 +5,12 @@ tags: math,beginner Maps a number from one range to another range. -- Returns `num` mapped between `outMin`-`outMax` from `inMin`-`inMax`. +- Return `num` mapped between `outMin`-`outMax` from `inMin`-`inMax`. ```py def num_to_range(num, inMin, inMax, outMin, outMax): - return outMin + ((float(num - inMin) / float((inMax - inMin))) * (outMax - outMin)) + return outMin(float(num - inMin) / float(inMax - inMin) * (outMax + - outMin)) ``` ```py diff --git a/snippets/offset.md b/snippets/offset.md index 2ae627530..ef1132f73 100644 --- a/snippets/offset.md +++ b/snippets/offset.md @@ -5,7 +5,7 @@ tags: list,beginner Moves the specified amount of elements to the end of the list. -- Use `lst[offset:]` and `lst[:offset]` to get the two slices of the list and combine them before returning. +- Use slice notation to get the two slices of the list and combine them before returning. ```py def offset(lst, offset): diff --git a/snippets/pad_number.md b/snippets/pad_number.md index 6fc98fe98..b96f1d7d3 100644 --- a/snippets/pad_number.md +++ b/snippets/pad_number.md @@ -5,7 +5,7 @@ tags: string,math,beginner Pads a given number to the specified length. -- Use `str.zfill()` to pad the number to specified length, after converting it to a string. +- Use `str.zfill()` to pad the number to the specified length, after converting it to a string. ```py def pad_number(n, l): diff --git a/snippets/palindrome.md b/snippets/palindrome.md index e6c9b77bc..9ce5cf975 100644 --- a/snippets/palindrome.md +++ b/snippets/palindrome.md @@ -3,10 +3,10 @@ title: palindrome tags: string,intermediate --- -Returns `True` if the given string is a palindrome, `False` otherwise. +Checks if the given string is a palindrome. -- Use `s.lower()` and `re.sub()` to convert to lowercase and remove non-alphanumeric characters from the given string. -- Then, compare the new string with its reverse. +- Use `str.lower()` and `re.sub()` to convert to lowercase and remove non-alphanumeric characters from the given string. +- Then, compare the new string with its reverse, using slice notation. ```py from re import sub @@ -18,4 +18,4 @@ def palindrome(s): ```py palindrome('taco cat') # True -``` \ No newline at end of file +``` diff --git a/snippets/powerset.md b/snippets/powerset.md index 3ad8ec69f..181301f5f 100644 --- a/snippets/powerset.md +++ b/snippets/powerset.md @@ -5,9 +5,9 @@ tags: math,list,advanced Returns the powerset of a given iterable. -- Use the `list()` constructor to convert the given value to a `list`. -- Use `range()` and `combinations()` to create a generator the returns all subsets. -- Use `chain.from_iterable()` and the `list()` constructor to consume the generator and return a list. +- Use `list()` to convert the given value to a list. +- Use `range()` and `itertools.combinations()` to create a generator that returns all subsets. +- Use `itertools.chain.from_iterable()` and `list()` to consume the generator and return a list. ```py from itertools import chain, combinations diff --git a/snippets/rads_to_degrees.md b/snippets/rads_to_degrees.md index 3ffecbb24..a0c6b0334 100644 --- a/snippets/rads_to_degrees.md +++ b/snippets/rads_to_degrees.md @@ -11,10 +11,11 @@ Converts an angle from radians to degrees. from math import pi def rads_to_degrees(rad): - return (rad * 180.0) / math.pi + return (rad * 180.0) / pi ``` ```py from math import pi -rads_to_degrees(math.pi / 2) # 90.0 + +rads_to_degrees(pi / 2) # 90.0 ``` diff --git a/snippets/reverse.md b/snippets/reverse.md index 3b46217f1..8c19c1791 100644 --- a/snippets/reverse.md +++ b/snippets/reverse.md @@ -14,5 +14,5 @@ def reverse(itr): ```py reverse([1, 2, 3]) # [3, 2, 1] -reverse("snippet") # "teppins" +reverse('snippet') # 'teppins' ``` diff --git a/snippets/reverse_number.md b/snippets/reverse_number.md index 2766bd12d..ccbe8e662 100644 --- a/snippets/reverse_number.md +++ b/snippets/reverse_number.md @@ -1,18 +1,18 @@ --- title: reverse_number -tags: math,string,intermediate +tags: math,intermediate --- Reverses a number. -- Use `str()` to convert the number to a string, slice notation to reverse it and `string.replace()` to remove the sign. -- Use `float()` to convert the result to a number and `copysign()` to copy the original sign. +- Use `str()` to convert the number to a string, slice notation to reverse it and `str.replace()` to remove the sign. +- Use `float()` to convert the result to a number and `math.copysign()` to copy the original sign. ```py from math import copysign def reverse_number(n): - return copysign(float(str(n)[::-1].replace('-','')), n) + return copysign(float(str(n)[::-1].replace('-', '')), n) ``` ```py diff --git a/snippets/rgb_to_hex.md b/snippets/rgb_to_hex.md index 456ff50e6..749952459 100644 --- a/snippets/rgb_to_hex.md +++ b/snippets/rgb_to_hex.md @@ -5,12 +5,12 @@ tags: string,math,intermediate Converts the values of RGB components to a hexadecimal color code. -- Create a placeholder for a zero-padded hexadecimal value using `"{:02X}"`, copy it three times. +- Create a placeholder for a zero-padded hexadecimal value using `'{:02X}'` and copy it three times. - Use `str.format()` on the resulting string to replace the placeholders with the given values. ```py def rgb_to_hex(r, g, b): - return ("{:02X}" * 3).format(r, g, b) + return ('{:02X}' * 3).format(r, g, b) ``` ```py