Update snippet descriptions

This commit is contained in:
Isabelle Viktoria Maciohsek
2020-11-02 19:27:53 +02:00
parent a2cd6db3b0
commit 0ab4b3dbc5
26 changed files with 64 additions and 62 deletions

View File

@ -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

View File

@ -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]
```

View File

@ -15,5 +15,5 @@ def degrees_to_rads(deg):
```
```py
degrees_to_rads(180) # 3.141592653589793
degrees_to_rads(180) # ~3.1416
```

View File

@ -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
```

View File

@ -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)]
```

View File

@ -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):

View File

@ -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 } ]
```

View File

@ -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):

View File

@ -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):

View File

@ -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))
```

View File

@ -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):

View File

@ -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

View File

@ -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
```

View File

@ -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

View File

@ -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

View File

@ -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):

View File

@ -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):

View File

@ -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`.

View File

@ -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'
```

View File

@ -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' ]
```

View File

@ -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):

View File

@ -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):

View File

@ -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

View File

@ -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]
```

View File

@ -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.

View File

@ -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