Update snippet descriptions

This commit is contained in:
Isabelle Viktoria Maciohsek
2020-11-02 19:28:05 +02:00
parent 0ab4b3dbc5
commit 0a2f7993f7
24 changed files with 63 additions and 51 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -13,5 +13,5 @@ def initial(lst):
```
```py
initial([1, 2, 3]) # [1,2]
initial([1, 2, 3]) # [1, 2]
```

View File

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

View File

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

View File

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

View File

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

View File

@ -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]
intersection_by([2.1, 1.2], [2.3, 3.4], floor) # [2.1]
```

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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