Skip to content

Commit

Permalink
Update snippet descriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Trinityyi committed Nov 2, 2020
1 parent 0a2f799 commit 98b5f67
Show file tree
Hide file tree
Showing 23 changed files with 68 additions and 59 deletions.
4 changes: 2 additions & 2 deletions snippets/map_dictionary.md
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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 }
```
3 changes: 1 addition & 2 deletions snippets/map_values.md
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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}
```
5 changes: 3 additions & 2 deletions snippets/max_by.md
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
2 changes: 1 addition & 1 deletion snippets/max_element_index.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
11 changes: 6 additions & 5 deletions snippets/max_n.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
```
7 changes: 4 additions & 3 deletions snippets/median.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
```
9 changes: 5 additions & 4 deletions snippets/merge.md
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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]]
```
11 changes: 6 additions & 5 deletions snippets/merge_dictionaries.md
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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 }
```
5 changes: 3 additions & 2 deletions snippets/min_by.md
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
2 changes: 1 addition & 1 deletion snippets/min_element_index.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
11 changes: 6 additions & 5 deletions snippets/min_n.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
```
7 changes: 4 additions & 3 deletions snippets/most_frequent.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion snippets/n_times_string.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
6 changes: 3 additions & 3 deletions snippets/none.md
Original file line number Diff line number Diff line change
@@ -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)
```

Expand Down
5 changes: 3 additions & 2 deletions snippets/num_to_range.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion snippets/offset.md
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion snippets/pad_number.md
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
8 changes: 4 additions & 4 deletions snippets/palindrome.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -18,4 +18,4 @@ def palindrome(s):

```py
palindrome('taco cat') # True
```
```
6 changes: 3 additions & 3 deletions snippets/powerset.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions snippets/rads_to_degrees.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
2 changes: 1 addition & 1 deletion snippets/reverse.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ def reverse(itr):

```py
reverse([1, 2, 3]) # [3, 2, 1]
reverse("snippet") # "teppins"
reverse('snippet') # 'teppins'
```
8 changes: 4 additions & 4 deletions snippets/reverse_number.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions snippets/rgb_to_hex.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 98b5f67

Please sign in to comment.