Skip to content

Commit

Permalink
Bake dates into snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
Trinityyi committed Jun 13, 2021
1 parent 4c07c3c commit cbc78ee
Show file tree
Hide file tree
Showing 159 changed files with 1,024 additions and 706 deletions.
2 changes: 2 additions & 0 deletions snippets/add_days.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
title: add_days
tags: date,intermediate
firstSeen: 2020-10-28T16:19:04+02:00
lastUpdated: 2020-10-28T16:19:04+02:00
---

Calculates the date of `n` days from the given date.
Expand Down
2 changes: 2 additions & 0 deletions snippets/all_equal.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
title: all_equal
tags: list,beginner
firstSeen: 2019-08-20T11:39:18+03:00
lastUpdated: 2020-10-11T13:40:42+03:00
---

Checks if all elements in a list are equal.
Expand Down
2 changes: 2 additions & 0 deletions snippets/all_unique.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
title: all_unique
tags: list,beginner
firstSeen: 2018-04-01T11:03:09+03:00
lastUpdated: 2021-01-07T23:30:28+02:00
---

Checks if all the values in a list are unique.
Expand Down
2 changes: 2 additions & 0 deletions snippets/arithmetic_progression.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
title: arithmetic_progression
tags: math,beginner
firstSeen: 2020-07-28T13:57:33+03:00
lastUpdated: 2020-11-02T19:27:07+02:00
---

Generates a list of numbers in the arithmetic progression starting with the given positive integer and up to the specified limit.
Expand Down
2 changes: 2 additions & 0 deletions snippets/average.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
title: average
tags: math,list,beginner
firstSeen: 2018-01-27T07:16:41+02:00
lastUpdated: 2020-11-02T19:27:07+02:00
---

Calculates the average of two or more numbers.
Expand Down
2 changes: 2 additions & 0 deletions snippets/average_by.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
title: average_by
tags: math,list,intermediate
firstSeen: 2019-08-20T11:55:10+03:00
lastUpdated: 2020-11-02T19:27:07+02:00
---

Calculates the average of a list, after mapping each element to a value using the provided function.
Expand Down
2 changes: 2 additions & 0 deletions snippets/bifurcate.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
title: bifurcate
tags: list,intermediate
firstSeen: 2019-08-20T12:37:06+03:00
lastUpdated: 2020-11-02T19:27:07+02:00
---

Splits values into two groups, based on the result of the given `filter` list.
Expand Down
2 changes: 2 additions & 0 deletions snippets/bifurcate_by.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
title: bifurcate_by
tags: list,intermediate
firstSeen: 2019-08-20T12:41:21+03:00
lastUpdated: 2020-11-02T19:27:07+02:00
---

Splits values into two groups, based on the result of the given filtering function.
Expand Down
2 changes: 2 additions & 0 deletions snippets/binomial_coefficient.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
title: binomial_coefficient
tags: math,beginner
firstSeen: 2020-10-04T11:56:31+03:00
lastUpdated: 2020-11-02T19:27:07+02:00
---

Calculates the number of ways to choose `k` items from `n` items without repetition and without order.
Expand Down
2 changes: 2 additions & 0 deletions snippets/byte_size.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
title: byte_size
tags: string,beginner
firstSeen: 2018-02-01T10:19:59+02:00
lastUpdated: 2020-11-02T19:27:07+02:00
---

Returns the length of a string in bytes.
Expand Down
2 changes: 2 additions & 0 deletions snippets/camel.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
title: camel
tags: string,regexp,intermediate
firstSeen: 2019-08-21T08:59:54+03:00
lastUpdated: 2020-11-02T19:27:07+02:00
---

Converts a string to camelcase.
Expand Down
2 changes: 2 additions & 0 deletions snippets/capitalize.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
title: capitalize
tags: string,intermediate
firstSeen: 2018-02-01T10:19:59+02:00
lastUpdated: 2020-11-02T19:27:07+02:00
---

Capitalizes the first letter of a string.
Expand Down
2 changes: 2 additions & 0 deletions snippets/capitalize_every_word.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
title: capitalize_every_word
tags: string,beginner
firstSeen: 2018-02-01T10:19:59+02:00
lastUpdated: 2020-11-02T19:27:07+02:00
---

Capitalizes the first letter of every word in a string.
Expand Down
2 changes: 2 additions & 0 deletions snippets/cast_list.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
title: cast_list
tags: list,intermediate
firstSeen: 2019-08-20T12:47:43+03:00
lastUpdated: 2020-11-02T19:27:07+02:00
---

Casts the provided value as a list if it's not one.
Expand Down
38 changes: 20 additions & 18 deletions snippets/celsius_to_fahrenheit.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
---
title: celsius_to_fahrenheit
tags: math,beginner
unlisted: true
---

Converts Celsius to Fahrenheit.

- Follow the conversion formula `F = 1.8 * C + 32`.

```py
def celsius_to_fahrenheit(degrees):
return ((degrees * 1.8) + 32)
```

```py
celsius_to_fahrenheit(180) # 356.0
```
---
title: celsius_to_fahrenheit
tags: math,beginner
unlisted: true
firstSeen: 2020-04-05T12:29:03+03:00
lastUpdated: 2021-01-04T12:47:04+02:00
---

Converts Celsius to Fahrenheit.

- Follow the conversion formula `F = 1.8 * C + 32`.

```py
def celsius_to_fahrenheit(degrees):
return ((degrees * 1.8) + 32)
```

```py
celsius_to_fahrenheit(180) # 356.0
```
40 changes: 21 additions & 19 deletions snippets/check_prop.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
---
title: check_prop
tags: function,intermediate
---

Creates a function that will invoke a predicate function for the specified property on a given object.

- Return a `lambda` function that takes an object and applies the predicate function, `fn` to the specified property.

```py
def check_prop(fn, prop):
return lambda obj: fn(obj[prop])
```

```py
check_age = check_prop(lambda x: x >= 18, 'age')
user = {'name': 'Mark', 'age': 18}
check_age(user) # True
```
---
title: check_prop
tags: function,intermediate
firstSeen: 2020-01-02T16:49:25+02:00
lastUpdated: 2020-11-02T19:27:07+02:00
---

Creates a function that will invoke a predicate function for the specified property on a given object.

- Return a `lambda` function that takes an object and applies the predicate function, `fn` to the specified property.

```py
def check_prop(fn, prop):
return lambda obj: fn(obj[prop])
```

```py
check_age = check_prop(lambda x: x >= 18, 'age')
user = {'name': 'Mark', 'age': 18}
check_age(user) # True
```
2 changes: 2 additions & 0 deletions snippets/chunk.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
title: chunk
tags: list,intermediate
firstSeen: 2018-01-09T06:39:42+02:00
lastUpdated: 2020-11-02T19:27:07+02:00
---

Chunks a list into smaller lists of a specified size.
Expand Down
2 changes: 2 additions & 0 deletions snippets/chunk_into_n.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
title: chunk_into_n
tags: list,intermediate
firstSeen: 2020-10-12T22:11:30+03:00
lastUpdated: 2020-10-23T05:35:06+03:00
---

Chunks a list into `n` smaller lists.
Expand Down
2 changes: 2 additions & 0 deletions snippets/clamp_number.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
title: clamp_number
tags: math,beginner
firstSeen: 2019-08-20T12:50:38+03:00
lastUpdated: 2020-11-02T19:27:07+02:00
---

Clamps `num` within the inclusive range specified by the boundary values.
Expand Down
2 changes: 2 additions & 0 deletions snippets/collect_dictionary.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
title: collect_dictionary
tags: dictionary,intermediate
firstSeen: 2020-04-07T21:15:06+03:00
lastUpdated: 2020-11-02T19:27:07+02:00
---

Inverts a dictionary with non-unique hashable values.
Expand Down
58 changes: 30 additions & 28 deletions snippets/combine_values.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
---
title: combine_values
tags: dictionary,intermediate
---

Combines two or more dictionaries, creating a list of values for each key.

- Create a new `collections.defaultdict` with `list` as the default value for each key and loop over `dicts`.
- Use `dict.append()` to map the values of the dictionary to keys.
- Use `dict()` to convert the `collections.defaultdict` to a regular dictionary.

```py
from collections import defaultdict

def combine_values(*dicts):
res = defaultdict(list)
for d in dicts:
for key in d:
res[key].append(d[key])
return dict(res)
```

```py
d1 = {'a': 1, 'b': 'foo', 'c': 400}
d2 = {'a': 3, 'b': 200, 'd': 400}

combine_values(d1, d2) # {'a': [1, 3], 'b': ['foo', 200], 'c': [400], 'd': [400]}
```
---
title: combine_values
tags: dictionary,intermediate
firstSeen: 2021-03-07T12:30:47+02:00
lastUpdated: 2021-04-04T14:32:35+03:00
---

Combines two or more dictionaries, creating a list of values for each key.

- Create a new `collections.defaultdict` with `list` as the default value for each key and loop over `dicts`.
- Use `dict.append()` to map the values of the dictionary to keys.
- Use `dict()` to convert the `collections.defaultdict` to a regular dictionary.

```py
from collections import defaultdict

def combine_values(*dicts):
res = defaultdict(list)
for d in dicts:
for key in d:
res[key].append(d[key])
return dict(res)
```

```py
d1 = {'a': 1, 'b': 'foo', 'c': 400}
d2 = {'a': 3, 'b': 200, 'd': 400}

combine_values(d1, d2) # {'a': [1, 3], 'b': ['foo', 200], 'c': [400], 'd': [400]}
```
2 changes: 2 additions & 0 deletions snippets/compact.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
title: compact
tags: list,beginner
firstSeen: 2018-01-19T12:17:06+02:00
lastUpdated: 2020-11-02T19:27:07+02:00
---

Removes falsy values from a list.
Expand Down
48 changes: 25 additions & 23 deletions snippets/compose.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
---
title: compose
tags: function,advanced
---

Performs right-to-left function composition.

- Use `functools.reduce()` to perform right-to-left function composition.
- The last (rightmost) function can accept one or more arguments; the remaining functions must be unary.

```py
from functools import reduce

def compose(*fns):
return reduce(lambda f, g: lambda *args: f(g(*args)), fns)
```

```py
add5 = lambda x: x + 5
multiply = lambda x, y: x * y
multiply_and_add_5 = compose(add5, multiply)
multiply_and_add_5(5, 2) # 15
```
---
title: compose
tags: function,advanced
firstSeen: 2020-01-02T15:51:20+02:00
lastUpdated: 2020-11-02T19:27:07+02:00
---

Performs right-to-left function composition.

- Use `functools.reduce()` to perform right-to-left function composition.
- The last (rightmost) function can accept one or more arguments; the remaining functions must be unary.

```py
from functools import reduce

def compose(*fns):
return reduce(lambda f, g: lambda *args: f(g(*args)), fns)
```

```py
add5 = lambda x: x + 5
multiply = lambda x, y: x * y
multiply_and_add_5 = compose(add5, multiply)
multiply_and_add_5(5, 2) # 15
```
Loading

0 comments on commit cbc78ee

Please sign in to comment.