Update snippet descriptions

This commit is contained in:
Isabelle Viktoria Maciohsek
2020-11-02 19:28:27 +02:00
parent 0a2f7993f7
commit 98b5f67412
23 changed files with 68 additions and 59 deletions

View File

@ -1,6 +1,6 @@
--- ---
title: map_dictionary 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. 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.

View File

@ -1,6 +1,6 @@
--- ---
title: map_values 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. 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 }, 'fred': { 'user': 'fred', 'age': 40 },
'pebbles': { 'user': 'pebbles', 'age': 1 } 'pebbles': { 'user': 'pebbles', 'age': 1 }
} }
map_values(users, lambda u : u['age']) # {'fred': 40, 'pebbles': 1} map_values(users, lambda u : u['age']) # {'fred': 40, 'pebbles': 1}
``` ```

View File

@ -1,11 +1,12 @@
--- ---
title: max_by 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. 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 ```py
def max_by(lst, fn): def max_by(lst, fn):

View File

@ -1,6 +1,6 @@
--- ---
title: max_element_index title: max_element_index
tags: list,beginner tags: math,list,beginner
--- ---
Returns the index of the element with the maximum value in a list. Returns the index of the element with the maximum value in a list.

View File

@ -4,10 +4,11 @@ tags: list,math,beginner
--- ---
Returns the `n` maximum elements from the provided list. 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. - 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 ```py
def max_n(lst, n = 1): def max_n(lst, n = 1):

View File

@ -5,7 +5,8 @@ tags: math,beginner
Finds the median of a list of numbers. 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. - [`statistics.median()`](https://docs.python.org/3/library/statistics.html#statistics.median) provides similar functionality to this snippet.
```py ```py

View File

@ -1,11 +1,11 @@
--- ---
title: merge 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. 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. - 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`). - 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. - [`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.
@ -24,5 +24,6 @@ def merge(*args, fill_value=None):
```py ```py
merge(['a', 'b'], [1, 2], [True, False]) # [['a', 1, True], ['b', 2, False]] 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]) # [['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]]
``` ```

View File

@ -5,7 +5,7 @@ tags: dictionary,intermediate
Merges two or more dictionaries. 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 ```py
def merge_dictionaries(*dicts): def merge_dictionaries(*dicts):
@ -17,11 +17,12 @@ def merge_dictionaries(*dicts):
```py ```py
ages_one = { ages_one = {
"Peter": 10, 'Peter': 10,
"Isabel": 11, 'Isabel': 11,
} }
ages_two = { 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 }
``` ```

View File

@ -1,11 +1,12 @@
--- ---
title: min_by 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. 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 ```py
def min_by(lst, fn): def min_by(lst, fn):

View File

@ -1,6 +1,6 @@
--- ---
title: min_element_index title: min_element_index
tags: list,beginner tags: math,list,beginner
--- ---
Returns the index of the element with the minimum value in a list. Returns the index of the element with the minimum value in a list.

View File

@ -4,10 +4,11 @@ tags: list,math,beginner
--- ---
Returns the `n` minimum elements from the provided list. 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. - 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 ```py
def min_n(lst, n = 1): def min_n(lst, n = 1):

View File

@ -5,11 +5,12 @@ tags: list,beginner
Returns the most frequent element in a list. 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 ```py
def most_frequent(list): def most_frequent(lst):
return max(set(list), key=list.count) return max(set(lst), key = lst.count)
``` ```
```py ```py

View File

@ -3,7 +3,7 @@ title: n_times_string
tags: string,beginner 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. - Repeat the string `n` times, using the `*` operator.

View File

@ -1,9 +1,9 @@
--- ---
title: none 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. - Use `all()` and `fn` to check if `fn` returns `False` for all the elements in the list.

View File

@ -5,11 +5,12 @@ tags: math,beginner
Maps a number from one range to another range. 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 ```py
def num_to_range(num, inMin, inMax, outMin, outMax): 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 ```py

View File

@ -5,7 +5,7 @@ tags: list,beginner
Moves the specified amount of elements to the end of the list. 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 ```py
def offset(lst, offset): def offset(lst, offset):

View File

@ -5,7 +5,7 @@ tags: string,math,beginner
Pads a given number to the specified length. 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 ```py
def pad_number(n, l): def pad_number(n, l):

View File

@ -3,10 +3,10 @@ title: palindrome
tags: string,intermediate 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. - 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. - Then, compare the new string with its reverse, using slice notation.
```py ```py
from re import sub from re import sub

View File

@ -5,9 +5,9 @@ tags: math,list,advanced
Returns the powerset of a given iterable. Returns the powerset of a given iterable.
- Use the `list()` constructor to convert the given value to a `list`. - Use `list()` to convert the given value to a list.
- Use `range()` and `combinations()` to create a generator the returns all subsets. - Use `range()` and `itertools.combinations()` to create a generator that returns all subsets.
- Use `chain.from_iterable()` and the `list()` constructor to consume the generator and return a list. - Use `itertools.chain.from_iterable()` and `list()` to consume the generator and return a list.
```py ```py
from itertools import chain, combinations from itertools import chain, combinations

View File

@ -11,10 +11,11 @@ Converts an angle from radians to degrees.
from math import pi from math import pi
def rads_to_degrees(rad): def rads_to_degrees(rad):
return (rad * 180.0) / math.pi return (rad * 180.0) / pi
``` ```
```py ```py
from math import pi from math import pi
rads_to_degrees(math.pi / 2) # 90.0
rads_to_degrees(pi / 2) # 90.0
``` ```

View File

@ -14,5 +14,5 @@ def reverse(itr):
```py ```py
reverse([1, 2, 3]) # [3, 2, 1] reverse([1, 2, 3]) # [3, 2, 1]
reverse("snippet") # "teppins" reverse('snippet') # 'teppins'
``` ```

View File

@ -1,12 +1,12 @@
--- ---
title: reverse_number title: reverse_number
tags: math,string,intermediate tags: math,intermediate
--- ---
Reverses a number. 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 `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 `copysign()` to copy the original sign. - Use `float()` to convert the result to a number and `math.copysign()` to copy the original sign.
```py ```py
from math import copysign from math import copysign

View File

@ -5,12 +5,12 @@ tags: string,math,intermediate
Converts the values of RGB components to a hexadecimal color code. 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. - Use `str.format()` on the resulting string to replace the placeholders with the given values.
```py ```py
def rgb_to_hex(r, g, b): def rgb_to_hex(r, g, b):
return ("{:02X}" * 3).format(r, g, b) return ('{:02X}' * 3).format(r, g, b)
``` ```
```py ```py