forked from Chalarangelo/30-seconds-of-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
159 changed files
with
1,024 additions
and
706 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
Oops, something went wrong.