Merge pull request #447 from 30-seconds/redate
This commit is contained in:
14
.github/config.yml
vendored
14
.github/config.yml
vendored
@ -1,14 +0,0 @@
|
||||
# Configuration for request-info - https://github.com/behaviorbot/request-info
|
||||
|
||||
# *Required* Comment to reply with
|
||||
requestInfoReplyComment: >
|
||||
We would appreciate it if you could provide us with some more information about this issue/PR!
|
||||
|
||||
# *OPTIONAL* default titles to check against for lack of descriptiveness
|
||||
# MUST BE ALL LOWERCASE
|
||||
requestInfoDefaultTitles:
|
||||
- update readme.md
|
||||
- updates
|
||||
|
||||
# *OPTIONAL* Label to be added to Issues and Pull Requests with insufficient information given
|
||||
requestInfoLabelToAdd: needs-more-info
|
||||
35
.github/lock.yml
vendored
35
.github/lock.yml
vendored
@ -1,35 +0,0 @@
|
||||
# Configuration for Lock Threads - https://github.com/dessant/lock-threads
|
||||
|
||||
# Number of days of inactivity before a closed issue or pull request is locked
|
||||
daysUntilLock: 60
|
||||
|
||||
# Skip issues and pull requests created before a given timestamp. Timestamp must
|
||||
# follow ISO 8601 (`YYYY-MM-DD`). Set to `false` to disable
|
||||
skipCreatedBefore: false
|
||||
|
||||
# Issues and pull requests with these labels will be ignored. Set to `[]` to disable
|
||||
exemptLabels: []
|
||||
|
||||
# Label to add before locking, such as `outdated`. Set to `false` to disable
|
||||
lockLabel: false
|
||||
|
||||
# Comment to post before locking. Set to `false` to disable
|
||||
lockComment: false
|
||||
|
||||
# Assign `resolved` as the reason for locking. Set to `false` to disable
|
||||
setLockReason: true
|
||||
|
||||
# Limit to only `issues` or `pulls`
|
||||
# only: issues
|
||||
|
||||
# Optionally, specify configuration settings just for `issues` or `pulls`
|
||||
# issues:
|
||||
# exemptLabels:
|
||||
# - help-wanted
|
||||
# lockLabel: outdated
|
||||
|
||||
# pulls:
|
||||
# daysUntilLock: 30
|
||||
|
||||
# Repository to extend settings from
|
||||
# _extends: repo
|
||||
18
.github/stale.yml
vendored
18
.github/stale.yml
vendored
@ -1,18 +0,0 @@
|
||||
# Number of days of inactivity before an issue becomes stale
|
||||
daysUntilStale: 21
|
||||
# Number of days of inactivity before a stale issue is closed
|
||||
daysUntilClose: 7
|
||||
# Issues with these labels will never be considered stale
|
||||
exemptLabels:
|
||||
- not-stale
|
||||
- pinned
|
||||
- security
|
||||
# Label to use when marking an issue as stale
|
||||
staleLabel: false
|
||||
# Comment to post when marking an issue as stale. Set to `false` to disable
|
||||
markComment: >
|
||||
This issue has been automatically marked as stale because it has not had
|
||||
recent activity. It will be closed if no further activity occurs. Thank you
|
||||
for your contributions.
|
||||
# Comment to post when closing a stale issue. Set to `false` to disable
|
||||
closeComment: false
|
||||
@ -2,20 +2,10 @@
|
||||
|
||||
**30 seconds of code** is powered by the community, so feel free to contribute in any way you can to help us!
|
||||
|
||||
## Tools
|
||||
|
||||
Before you begin contributing, you should install the integration-tools globally on your machine:
|
||||
|
||||
```sh
|
||||
npm install -g @30-seconds/integration-tools
|
||||
```
|
||||
|
||||
This will allow you to use our customized tools for all of our content repositories.
|
||||
|
||||
## How you can help
|
||||
|
||||
- Submit pull requests with new snippets (see guidelines below) or snippet updates (tags, descriptions, explanations, typos, examples, code improvements).
|
||||
- Open issues for things you want to see added, modified, discuss ideas or help out with existing issues.
|
||||
- Open issues for things you want to see added, modified, discuss ideas or help out with existing issues.
|
||||
|
||||
## Ground rules
|
||||
|
||||
@ -23,25 +13,24 @@ Breaking any of these rules will result in your pull request being closed. Pleas
|
||||
|
||||
- **Always be polite and respectful to others** and try to follow the advice of the moderators/collaborators/owners.
|
||||
- **Only modify snippet files**, never modify the generated files in the `snippet_data` directory.
|
||||
- **Use the integration tools commands** to generate new snippets, ensuring they have the correct name and are in the correct location.
|
||||
- **Use the snippet template** to create new snippets, ensure they have the correct name and are in the correct location.
|
||||
- **Follow snippet format exactly**, otherwise your snippets will not be recognized correctly by the tools responsible for publishing them on the website. This includes such things as spacing and empty lines - if you accidentally make a mistake, consult the repository's [snippet template](snippet-template.md).
|
||||
- **Snippets should solve real-world problems**, no matter how simple and should be abstract enough to be applied to different scenarios.
|
||||
|
||||
## Snippet creation
|
||||
|
||||
After installing the integration tools, you can run the following command:
|
||||
In order to create a new snippet, you should follow the steps below:
|
||||
|
||||
```sh
|
||||
create-new-snippet <my-snippet-name>
|
||||
```
|
||||
|
||||
Replace `<my-snippet-name>` with the name of the snippet you are adding.
|
||||
- Crate a copy of the [snippet template](snippet-template.md) in the `snippets` directory.
|
||||
- Change the name of the newly created file to the name of your snippet.
|
||||
- Edit the file, adding your snippet based on the guidelines.
|
||||
|
||||
## Snippet guidelines
|
||||
|
||||
- Snippets must have all their frontmatter sections (title, tags etc.) filled.
|
||||
- Snippet titles must correspond to the filename and follow the language and repository's naming conventions.
|
||||
- Snippet tags must be comma-separated, contain a primary tag as seen on the website as their first tag and an expertise tag (`beginner`, `intermediate` or `advanced`) as their last tag.
|
||||
- Snippets must have their `firstSeen` dates formatted using [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601).
|
||||
- Snippet descriptions must be short and to the point. Explain *what* the snippet does and detail *how* the snippet works and the language features used in it.
|
||||
- Snippet code and examples must be enclosed in appropriate, language-tagged blocks as shown in the snippet template, be short and use modern techniques and features. Also make sure to test your code before submitting.
|
||||
- If your snippet contains arguments with default parameters, explain what happens if they are omitted when calling the function and what the default case is. Specify default parameters for arguments only if necessary.
|
||||
|
||||
14
README.md
14
README.md
@ -1,14 +1,14 @@
|
||||
[](https://30secondsofcode.org/python/p/1)
|
||||
|
||||
## 30 seconds of Python
|
||||
# 30 seconds of code
|
||||
|
||||
> Short Python code snippets for all your development needs
|
||||
|
||||
* Visit [our website](https://30secondsofcode.org) to view our snippet collection.
|
||||
* Use the [Search page](https://30secondsofcode.org/search) to find snippets that suit your needs. You can search by name, tag, language or using a snippet's description. Just start typing a term and see what comes up.
|
||||
* Browse the [Python Snippet List](https://30secondsofcode.org/python/p/1) to see all the snippets in this project or click individual tags at the top of the same page to narrow down your search to a specific tag.
|
||||
* Browse the [Python Snippet collection](https://30secondsofcode.org/python/p/1) to see all the snippets in this project or click individual tags at the top of the same page to narrow down your search to a specific tag.
|
||||
* Click on each snippet card to view the whole snippet, including code, explanation and examples.
|
||||
* You can use the button on the right side of a snippet card to copy the code to clipboard.
|
||||
* You can use the button at the bottom of a snippet card to copy the code to clipboard.
|
||||
* If you like the project, give it a star. It means a lot to the people maintaining it.
|
||||
|
||||
## Want to contribute?
|
||||
@ -18,9 +18,9 @@
|
||||
* If you find a problem with a specific snippet, please [open an issue](https://github.com/30-seconds/30-seconds-of-python/issues/new).
|
||||
* If you find a problem with the website, please [report it in the web repository](https://github.com/30-seconds/30-seconds-web/issues/new).
|
||||
|
||||
## Credits & Sponsors
|
||||
## Credits
|
||||
|
||||
* This repository is maintained by the [30-seconds organization on GitHub](https://github.com/30-seconds).
|
||||
* This repository is maintained by the [30 seconds of code organization on GitHub](https://github.com/30-seconds).
|
||||
* All snippets are licensed under the CC0-1.0 License, unless explicitly stated otherwise.
|
||||
* Logos, names and trademarks are not to be used without the explicit consent of the maintainers or owners of the 30 seconds GitHub organization.
|
||||
* Our website is powered by [Netlify](https://www.netlify.com/), [Gatsby](https://www.gatsbyjs.org/), [Travis CI](https://travis-ci.com/) & [GitHub](https://github.com/)
|
||||
* Logos, names and trademarks are not to be used without the explicit consent of the owners of the 30 seconds of code GitHub organization.
|
||||
* Our website is powered by [Netlify](https://www.netlify.com/), [Next.js](https://nextjs.org/) & [GitHub](https://github.com/).
|
||||
|
||||
BIN
logo.png
BIN
logo.png
Binary file not shown.
|
Before Width: | Height: | Size: 31 KiB After Width: | Height: | Size: 169 KiB |
@ -1,6 +1,7 @@
|
||||
---
|
||||
title: function_name
|
||||
tags: utility,intermediate
|
||||
firstSeen: 2021-06-13T05:00:00-04:00
|
||||
---
|
||||
|
||||
Explain briefly what the snippet does.
|
||||
|
||||
@ -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.
|
||||
|
||||
@ -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.
|
||||
|
||||
@ -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.
|
||||
|
||||
@ -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.
|
||||
|
||||
@ -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.
|
||||
|
||||
@ -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.
|
||||
|
||||
@ -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.
|
||||
|
||||
@ -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.
|
||||
|
||||
@ -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.
|
||||
|
||||
@ -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.
|
||||
|
||||
@ -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.
|
||||
|
||||
@ -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.
|
||||
|
||||
@ -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.
|
||||
|
||||
@ -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.
|
||||
|
||||
@ -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
|
||||
```
|
||||
|
||||
@ -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
|
||||
```
|
||||
|
||||
@ -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.
|
||||
|
||||
@ -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.
|
||||
|
||||
@ -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.
|
||||
|
||||
@ -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.
|
||||
|
||||
@ -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]}
|
||||
```
|
||||
|
||||
@ -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.
|
||||
|
||||
@ -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
|
||||
```
|
||||
|
||||
@ -1,23 +1,25 @@
|
||||
---
|
||||
title: compose_right
|
||||
tags: function,advanced
|
||||
---
|
||||
|
||||
Performs left-to-right function composition.
|
||||
|
||||
- Use `functools.reduce()` to perform left-to-right function composition.
|
||||
- The first (leftmost) function can accept one or more arguments; the remaining functions must be unary.
|
||||
|
||||
```py
|
||||
from functools import reduce
|
||||
|
||||
def compose_right(*fns):
|
||||
return reduce(lambda f, g: lambda *args: g(f(*args)), fns)
|
||||
```
|
||||
|
||||
```py
|
||||
add = lambda x, y: x + y
|
||||
square = lambda x: x * x
|
||||
add_and_square = compose_right(add, square)
|
||||
add_and_square(1, 2) # 9
|
||||
```
|
||||
---
|
||||
title: compose_right
|
||||
tags: function,advanced
|
||||
firstSeen: 2020-01-02T15:51:26+02:00
|
||||
lastUpdated: 2020-11-02T19:27:07+02:00
|
||||
---
|
||||
|
||||
Performs left-to-right function composition.
|
||||
|
||||
- Use `functools.reduce()` to perform left-to-right function composition.
|
||||
- The first (leftmost) function can accept one or more arguments; the remaining functions must be unary.
|
||||
|
||||
```py
|
||||
from functools import reduce
|
||||
|
||||
def compose_right(*fns):
|
||||
return reduce(lambda f, g: lambda *args: g(f(*args)), fns)
|
||||
```
|
||||
|
||||
```py
|
||||
add = lambda x, y: x + y
|
||||
square = lambda x: x * x
|
||||
add_and_square = compose_right(add, square)
|
||||
add_and_square(1, 2) # 9
|
||||
```
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
---
|
||||
title: count_by
|
||||
tags: list,intermediate
|
||||
firstSeen: 2018-02-07T10:33:47+02:00
|
||||
lastUpdated: 2020-11-02T19:27:07+02:00
|
||||
---
|
||||
|
||||
Groups the elements of a list based on the given function and returns the count of elements in each group.
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
---
|
||||
title: count_occurrences
|
||||
tags: list,beginner
|
||||
firstSeen: 2021-01-10T00:00:36+02:00
|
||||
lastUpdated: 2021-01-10T00:00:36+02:00
|
||||
---
|
||||
|
||||
Counts the occurrences of a value in a list.
|
||||
|
||||
@ -1,20 +1,22 @@
|
||||
---
|
||||
title: cumsum
|
||||
tags: list,intermediate
|
||||
---
|
||||
|
||||
Creates a list of partial sums.
|
||||
|
||||
- Use `itertools.accumulate()` to create the accumulated sum for each element.
|
||||
- Use `list()` to convert the result into a list.
|
||||
|
||||
```py
|
||||
from itertools import accumulate
|
||||
|
||||
def cumsum(lst):
|
||||
return list(accumulate(lst))
|
||||
```
|
||||
|
||||
```py
|
||||
cumsum(range(0, 15, 3)) # [0, 3, 9, 18, 30]
|
||||
```
|
||||
---
|
||||
title: cumsum
|
||||
tags: list,intermediate
|
||||
firstSeen: 2021-01-13T23:30:41+02:00
|
||||
lastUpdated: 2021-01-13T23:30:41+02:00
|
||||
---
|
||||
|
||||
Creates a list of partial sums.
|
||||
|
||||
- Use `itertools.accumulate()` to create the accumulated sum for each element.
|
||||
- Use `list()` to convert the result into a list.
|
||||
|
||||
```py
|
||||
from itertools import accumulate
|
||||
|
||||
def cumsum(lst):
|
||||
return list(accumulate(lst))
|
||||
```
|
||||
|
||||
```py
|
||||
cumsum(range(0, 15, 3)) # [0, 3, 9, 18, 30]
|
||||
```
|
||||
|
||||
@ -1,21 +1,23 @@
|
||||
---
|
||||
title: curry
|
||||
tags: function,intermediate
|
||||
---
|
||||
|
||||
Curries a function.
|
||||
|
||||
- Use `functools.partial()` to return a new partial object which behaves like `fn` with the given arguments, `args`, partially applied.
|
||||
|
||||
```py
|
||||
from functools import partial
|
||||
|
||||
def curry(fn, *args):
|
||||
return partial(fn, *args)
|
||||
```
|
||||
|
||||
```py
|
||||
add = lambda x, y: x + y
|
||||
add10 = curry(add, 10)
|
||||
add10(20) # 30
|
||||
```
|
||||
---
|
||||
title: curry
|
||||
tags: function,intermediate
|
||||
firstSeen: 2020-01-02T16:14:50+02:00
|
||||
lastUpdated: 2020-11-02T19:27:07+02:00
|
||||
---
|
||||
|
||||
Curries a function.
|
||||
|
||||
- Use `functools.partial()` to return a new partial object which behaves like `fn` with the given arguments, `args`, partially applied.
|
||||
|
||||
```py
|
||||
from functools import partial
|
||||
|
||||
def curry(fn, *args):
|
||||
return partial(fn, *args)
|
||||
```
|
||||
|
||||
```py
|
||||
add = lambda x, y: x + y
|
||||
add10 = curry(add, 10)
|
||||
add10(20) # 30
|
||||
```
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
---
|
||||
title: daterange
|
||||
tags: date,intermediate
|
||||
firstSeen: 2020-10-28T16:19:14+02:00
|
||||
lastUpdated: 2021-01-07T23:30:28+02:00
|
||||
---
|
||||
|
||||
Creates a list of dates between `start` (inclusive) and `end` (not inclusive).
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
---
|
||||
title: days_ago
|
||||
tags: date,intermediate
|
||||
firstSeen: 2020-10-28T16:19:30+02:00
|
||||
lastUpdated: 2020-10-28T16:19:30+02:00
|
||||
---
|
||||
|
||||
Calculates the date of `n` days ago from today.
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
---
|
||||
title: days_diff
|
||||
tags: date,beginner
|
||||
firstSeen: 2020-10-28T16:19:39+02:00
|
||||
lastUpdated: 2020-10-28T16:19:39+02:00
|
||||
---
|
||||
|
||||
Calculates the day difference between two dates.
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
---
|
||||
title: days_from_now
|
||||
tags: date,intermediate
|
||||
firstSeen: 2020-10-28T16:19:51+02:00
|
||||
lastUpdated: 2020-10-28T16:19:51+02:00
|
||||
---
|
||||
|
||||
Calculates the date of `n` days from today.
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
---
|
||||
title: decapitalize
|
||||
tags: string,intermediate
|
||||
firstSeen: 2018-02-01T10:19:59+02:00
|
||||
lastUpdated: 2020-11-02T19:27:53+02:00
|
||||
---
|
||||
|
||||
Decapitalizes the first letter of a string.
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
---
|
||||
title: deep_flatten
|
||||
tags: list,recursion,intermediate
|
||||
firstSeen: 2018-01-16T16:54:14+02:00
|
||||
lastUpdated: 2020-12-29T19:53:45+02:00
|
||||
---
|
||||
|
||||
Deep flattens a list.
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
---
|
||||
title: degrees_to_rads
|
||||
tags: math,beginner
|
||||
firstSeen: 2019-10-15T14:31:11+03:00
|
||||
lastUpdated: 2020-11-02T19:27:53+02:00
|
||||
---
|
||||
|
||||
Converts an angle from degrees to radians.
|
||||
|
||||
@ -1,20 +1,22 @@
|
||||
---
|
||||
title: delay
|
||||
tags: function,intermediate
|
||||
---
|
||||
|
||||
Invokes the provided function after `ms` milliseconds.
|
||||
|
||||
- Use `time.sleep()` to delay the execution of `fn` by `ms / 1000` seconds.
|
||||
|
||||
```py
|
||||
from time import sleep
|
||||
|
||||
def delay(fn, ms, *args):
|
||||
sleep(ms / 1000)
|
||||
return fn(*args)
|
||||
```
|
||||
|
||||
```py
|
||||
delay(lambda x: print(x), 1000, 'later') # prints 'later' after one second
|
||||
```
|
||||
---
|
||||
title: delay
|
||||
tags: function,intermediate
|
||||
firstSeen: 2020-01-02T16:24:51+02:00
|
||||
lastUpdated: 2020-11-02T19:27:53+02:00
|
||||
---
|
||||
|
||||
Invokes the provided function after `ms` milliseconds.
|
||||
|
||||
- Use `time.sleep()` to delay the execution of `fn` by `ms / 1000` seconds.
|
||||
|
||||
```py
|
||||
from time import sleep
|
||||
|
||||
def delay(fn, ms, *args):
|
||||
sleep(ms / 1000)
|
||||
return fn(*args)
|
||||
```
|
||||
|
||||
```py
|
||||
delay(lambda x: print(x), 1000, 'later') # prints 'later' after one second
|
||||
```
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
---
|
||||
title: dict_to_list
|
||||
tags: dictionary,list,intermediate
|
||||
firstSeen: 2020-10-16T21:24:14+03:00
|
||||
lastUpdated: 2020-11-02T19:27:53+02:00
|
||||
---
|
||||
|
||||
Converts a dictionary to a list of tuples.
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
---
|
||||
title: difference
|
||||
tags: list,beginner
|
||||
firstSeen: 2018-01-20T16:16:44+02:00
|
||||
lastUpdated: 2020-11-02T19:27:53+02:00
|
||||
---
|
||||
|
||||
Calculates the difference between two iterables, without filtering duplicate values.
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
---
|
||||
title: difference_by
|
||||
tags: list,function,intermediate
|
||||
firstSeen: 2018-02-08T15:59:27+02:00
|
||||
lastUpdated: 2020-11-02T19:27:53+02:00
|
||||
---
|
||||
|
||||
Returns the difference between two lists, after applying the provided function to each list element of both.
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
---
|
||||
title: digitize
|
||||
tags: math,list,beginner
|
||||
firstSeen: 2019-08-20T13:00:27+03:00
|
||||
lastUpdated: 2020-09-15T16:13:06+03:00
|
||||
---
|
||||
|
||||
Converts a number to a list of digits.
|
||||
|
||||
@ -1,20 +1,22 @@
|
||||
---
|
||||
title: drop
|
||||
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):
|
||||
return a[n:]
|
||||
```
|
||||
|
||||
```py
|
||||
drop([1, 2, 3]) # [2, 3]
|
||||
drop([1, 2, 3], 2) # [3]
|
||||
drop([1, 2, 3], 42) # []
|
||||
```
|
||||
---
|
||||
title: drop
|
||||
tags: list,beginner
|
||||
firstSeen: 2020-03-10T21:59:41+02:00
|
||||
lastUpdated: 2020-11-02T19:27:53+02:00
|
||||
---
|
||||
|
||||
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):
|
||||
return a[n:]
|
||||
```
|
||||
|
||||
```py
|
||||
drop([1, 2, 3]) # [2, 3]
|
||||
drop([1, 2, 3], 2) # [3]
|
||||
drop([1, 2, 3], 42) # []
|
||||
```
|
||||
|
||||
@ -1,20 +1,22 @@
|
||||
---
|
||||
title: drop_right
|
||||
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):
|
||||
return a[:-n]
|
||||
```
|
||||
|
||||
```py
|
||||
drop_right([1, 2, 3]) # [1, 2]
|
||||
drop_right([1, 2, 3], 2) # [1]
|
||||
drop_right([1, 2, 3], 42) # []
|
||||
```
|
||||
---
|
||||
title: drop_right
|
||||
tags: list,beginner
|
||||
firstSeen: 2020-03-10T21:59:41+02:00
|
||||
lastUpdated: 2020-11-02T19:27:53+02:00
|
||||
---
|
||||
|
||||
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):
|
||||
return a[:-n]
|
||||
```
|
||||
|
||||
```py
|
||||
drop_right([1, 2, 3]) # [1, 2]
|
||||
drop_right([1, 2, 3], 2) # [1]
|
||||
drop_right([1, 2, 3], 42) # []
|
||||
```
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
---
|
||||
title: every
|
||||
tags: list,intermediate
|
||||
firstSeen: 2019-08-20T11:34:24+03:00
|
||||
lastUpdated: 2020-11-02T19:27:53+02:00
|
||||
---
|
||||
|
||||
Checks if the provided function returns `True` for every element in the list.
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
---
|
||||
title: every_nth
|
||||
tags: list,beginner
|
||||
firstSeen: 2019-08-20T13:10:12+03:00
|
||||
lastUpdated: 2020-11-02T19:27:53+02:00
|
||||
---
|
||||
|
||||
Returns every `nth` element in a list.
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
---
|
||||
title: factorial
|
||||
tags: math,recursion,beginner
|
||||
firstSeen: 2018-01-27T07:29:56+02:00
|
||||
lastUpdated: 2020-09-15T16:13:06+03:00
|
||||
---
|
||||
|
||||
Calculates the factorial of a number.
|
||||
|
||||
@ -1,18 +1,20 @@
|
||||
---
|
||||
title: fahrenheit_to_celsius
|
||||
tags: math,beginner
|
||||
unlisted: true
|
||||
---
|
||||
|
||||
Converts Fahrenheit to Celsius.
|
||||
|
||||
- Follow the conversion formula `C = (F - 32) * 5/9`.
|
||||
|
||||
```py
|
||||
def fahrenheit_to_celsius(degrees):
|
||||
return ((degrees - 32) * 5/9)
|
||||
```
|
||||
|
||||
```py
|
||||
fahrenheit_to_celsius(77) # 25.0
|
||||
```
|
||||
---
|
||||
title: fahrenheit_to_celsius
|
||||
tags: math,beginner
|
||||
unlisted: true
|
||||
firstSeen: 2020-04-05T12:29:03+03:00
|
||||
lastUpdated: 2021-01-04T12:47:04+02:00
|
||||
---
|
||||
|
||||
Converts Fahrenheit to Celsius.
|
||||
|
||||
- Follow the conversion formula `C = (F - 32) * 5/9`.
|
||||
|
||||
```py
|
||||
def fahrenheit_to_celsius(degrees):
|
||||
return ((degrees - 32) * 5/9)
|
||||
```
|
||||
|
||||
```py
|
||||
fahrenheit_to_celsius(77) # 25.0
|
||||
```
|
||||
|
||||
@ -1,24 +1,26 @@
|
||||
---
|
||||
title: fibonacci
|
||||
tags: math,list,intermediate
|
||||
---
|
||||
|
||||
Generates a list, containing the Fibonacci sequence, up until the nth term.
|
||||
|
||||
- Starting with `0` and `1`, use `list.append()` to add the sum of the last two numbers of the list to the end of the list, until the length of the list reaches `n`.
|
||||
- If `n` is less or equal to `0`, return a list containing `0`.
|
||||
|
||||
```py
|
||||
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
|
||||
```
|
||||
|
||||
```py
|
||||
fibonacci(7) # [0, 1, 1, 2, 3, 5, 8, 13]
|
||||
```
|
||||
---
|
||||
title: fibonacci
|
||||
tags: math,list,intermediate
|
||||
firstSeen: 2018-10-06T06:06:33+03:00
|
||||
lastUpdated: 2020-11-02T19:27:53+02:00
|
||||
---
|
||||
|
||||
Generates a list, containing the Fibonacci sequence, up until the nth term.
|
||||
|
||||
- Starting with `0` and `1`, use `list.append()` to add the sum of the last two numbers of the list to the end of the list, until the length of the list reaches `n`.
|
||||
- If `n` is less or equal to `0`, return a list containing `0`.
|
||||
|
||||
```py
|
||||
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
|
||||
```
|
||||
|
||||
```py
|
||||
fibonacci(7) # [0, 1, 1, 2, 3, 5, 8, 13]
|
||||
```
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
---
|
||||
title: filter_non_unique
|
||||
tags: list,beginner
|
||||
firstSeen: 2019-08-20T13:13:51+03:00
|
||||
lastUpdated: 2020-11-02T19:27:53+02:00
|
||||
---
|
||||
|
||||
Creates a list with the non-unique values filtered out.
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
---
|
||||
title: filter_unique
|
||||
tags: list,beginner
|
||||
firstSeen: 2019-10-02T20:06:24+03:00
|
||||
lastUpdated: 2020-11-02T19:27:53+02:00
|
||||
---
|
||||
|
||||
Creates a list with the unique values filtered out.
|
||||
|
||||
@ -1,17 +1,19 @@
|
||||
---
|
||||
title: find
|
||||
tags: list,beginner
|
||||
---
|
||||
|
||||
Finds the value of the first element in the given list that satisfies the provided testing function.
|
||||
|
||||
- Use a list comprehension and `next()` to return the first element in `lst` for which `fn` returns `True`.
|
||||
|
||||
```py
|
||||
def find(lst, fn):
|
||||
return next(x for x in lst if fn(x))
|
||||
```
|
||||
|
||||
```py
|
||||
find([1, 2, 3, 4], lambda n: n % 2 == 1) # 1
|
||||
```
|
||||
---
|
||||
title: find
|
||||
tags: list,beginner
|
||||
firstSeen: 2020-03-10T22:38:48+02:00
|
||||
lastUpdated: 2020-11-02T19:27:53+02:00
|
||||
---
|
||||
|
||||
Finds the value of the first element in the given list that satisfies the provided testing function.
|
||||
|
||||
- Use a list comprehension and `next()` to return the first element in `lst` for which `fn` returns `True`.
|
||||
|
||||
```py
|
||||
def find(lst, fn):
|
||||
return next(x for x in lst if fn(x))
|
||||
```
|
||||
|
||||
```py
|
||||
find([1, 2, 3, 4], lambda n: n % 2 == 1) # 1
|
||||
```
|
||||
|
||||
@ -1,17 +1,19 @@
|
||||
---
|
||||
title: find_index
|
||||
tags: list,intermediate
|
||||
---
|
||||
|
||||
Finds the index of the first element in the given list that satisfies the provided testing function.
|
||||
|
||||
- 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):
|
||||
return next(i for i, x in enumerate(lst) if fn(x))
|
||||
```
|
||||
|
||||
```py
|
||||
find_index([1, 2, 3, 4], lambda n: n % 2 == 1) # 0
|
||||
```
|
||||
---
|
||||
title: find_index
|
||||
tags: list,intermediate
|
||||
firstSeen: 2020-03-10T22:38:48+02:00
|
||||
lastUpdated: 2020-11-02T19:27:53+02:00
|
||||
---
|
||||
|
||||
Finds the index of the first element in the given list that satisfies the provided testing function.
|
||||
|
||||
- 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):
|
||||
return next(i for i, x in enumerate(lst) if fn(x))
|
||||
```
|
||||
|
||||
```py
|
||||
find_index([1, 2, 3, 4], lambda n: n % 2 == 1) # 0
|
||||
```
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
---
|
||||
title: find_index_of_all
|
||||
tags: list,intermediate
|
||||
firstSeen: 2020-10-11T13:45:19+03:00
|
||||
lastUpdated: 2020-11-02T19:27:53+02:00
|
||||
---
|
||||
|
||||
Finds the indexes of all elements in the given list that satisfy the provided testing function.
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
---
|
||||
title: find_key
|
||||
tags: dictionary,intermediate
|
||||
firstSeen: 2020-04-16T19:13:20+03:00
|
||||
lastUpdated: 2020-11-02T19:27:53+02:00
|
||||
---
|
||||
|
||||
Finds the first key in the provided dictionary that has the given value.
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
---
|
||||
title: find_keys
|
||||
tags: dictionary,intermediate
|
||||
firstSeen: 2020-04-16T19:17:13+03:00
|
||||
lastUpdated: 2020-11-02T19:27:53+02:00
|
||||
---
|
||||
|
||||
Finds all keys in the provided dictionary that have the given value.
|
||||
|
||||
@ -1,17 +1,19 @@
|
||||
---
|
||||
title: find_last
|
||||
tags: list,beginner
|
||||
---
|
||||
|
||||
Finds the value of the last element in the given list that satisfies the provided testing function.
|
||||
|
||||
- Use a list comprehension and `next()` to return the last element in `lst` for which `fn` returns `True`.
|
||||
|
||||
```py
|
||||
def find_last(lst, fn):
|
||||
return next(x for x in lst[::-1] if fn(x))
|
||||
```
|
||||
|
||||
```py
|
||||
find_last([1, 2, 3, 4], lambda n: n % 2 == 1) # 3
|
||||
```
|
||||
---
|
||||
title: find_last
|
||||
tags: list,beginner
|
||||
firstSeen: 2020-03-10T22:38:48+02:00
|
||||
lastUpdated: 2020-11-02T19:27:53+02:00
|
||||
---
|
||||
|
||||
Finds the value of the last element in the given list that satisfies the provided testing function.
|
||||
|
||||
- Use a list comprehension and `next()` to return the last element in `lst` for which `fn` returns `True`.
|
||||
|
||||
```py
|
||||
def find_last(lst, fn):
|
||||
return next(x for x in lst[::-1] if fn(x))
|
||||
```
|
||||
|
||||
```py
|
||||
find_last([1, 2, 3, 4], lambda n: n % 2 == 1) # 3
|
||||
```
|
||||
|
||||
@ -1,17 +1,19 @@
|
||||
---
|
||||
title: find_last_index
|
||||
tags: list,beginner
|
||||
---
|
||||
|
||||
Finds the index of the last element in the given list that satisfies the provided testing function.
|
||||
|
||||
- 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):
|
||||
return len(lst) - 1 - next(i for i, x in enumerate(lst[::-1]) if fn(x))
|
||||
```
|
||||
|
||||
```py
|
||||
find_last_index([1, 2, 3, 4], lambda n: n % 2 == 1) # 2
|
||||
```
|
||||
---
|
||||
title: find_last_index
|
||||
tags: list,beginner
|
||||
firstSeen: 2020-03-10T22:38:48+02:00
|
||||
lastUpdated: 2020-11-02T19:27:53+02:00
|
||||
---
|
||||
|
||||
Finds the index of the last element in the given list that satisfies the provided testing function.
|
||||
|
||||
- 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):
|
||||
return len(lst) - 1 - next(i for i, x in enumerate(lst[::-1]) if fn(x))
|
||||
```
|
||||
|
||||
```py
|
||||
find_last_index([1, 2, 3, 4], lambda n: n % 2 == 1) # 2
|
||||
```
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
---
|
||||
title: find_parity_outliers
|
||||
tags: list,math,intermediate
|
||||
firstSeen: 2020-01-08T18:54:35+02:00
|
||||
lastUpdated: 2020-11-02T19:27:53+02:00
|
||||
---
|
||||
|
||||
Finds the items that are parity outliers in a given list.
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
---
|
||||
title: flatten
|
||||
tags: list,intermediate
|
||||
firstSeen: 2019-09-19T15:46:10+03:00
|
||||
lastUpdated: 2020-11-02T19:27:53+02:00
|
||||
---
|
||||
|
||||
Flattens a list of lists once.
|
||||
|
||||
@ -1,18 +1,20 @@
|
||||
---
|
||||
title: for_each
|
||||
tags: list,beginner
|
||||
---
|
||||
|
||||
Executes the provided function once for each list element.
|
||||
|
||||
- Use a `for` loop to execute `fn` for each element in `itr`.
|
||||
|
||||
```py
|
||||
def for_each(itr, fn):
|
||||
for el in itr:
|
||||
fn(el)
|
||||
```
|
||||
|
||||
```py
|
||||
for_each([1, 2, 3], print) # 1 2 3
|
||||
```
|
||||
---
|
||||
title: for_each
|
||||
tags: list,beginner
|
||||
firstSeen: 2020-03-15T12:54:08+02:00
|
||||
lastUpdated: 2020-09-15T16:13:06+03:00
|
||||
---
|
||||
|
||||
Executes the provided function once for each list element.
|
||||
|
||||
- Use a `for` loop to execute `fn` for each element in `itr`.
|
||||
|
||||
```py
|
||||
def for_each(itr, fn):
|
||||
for el in itr:
|
||||
fn(el)
|
||||
```
|
||||
|
||||
```py
|
||||
for_each([1, 2, 3], print) # 1 2 3
|
||||
```
|
||||
|
||||
@ -1,18 +1,20 @@
|
||||
---
|
||||
title: for_each_right
|
||||
tags: list,beginner
|
||||
---
|
||||
|
||||
Executes the provided function once for each list element, starting from the list's last element.
|
||||
|
||||
- Use a `for` loop in combination with slice notation to execute `fn` for each element in `itr`, starting from the last one.
|
||||
|
||||
```py
|
||||
def for_each_right(itr, fn):
|
||||
for el in itr[::-1]:
|
||||
fn(el)
|
||||
```
|
||||
|
||||
```py
|
||||
for_each_right([1, 2, 3], print) # 3 2 1
|
||||
```
|
||||
---
|
||||
title: for_each_right
|
||||
tags: list,beginner
|
||||
firstSeen: 2020-03-15T12:54:08+02:00
|
||||
lastUpdated: 2020-09-15T16:13:06+03:00
|
||||
---
|
||||
|
||||
Executes the provided function once for each list element, starting from the list's last element.
|
||||
|
||||
- Use a `for` loop in combination with slice notation to execute `fn` for each element in `itr`, starting from the last one.
|
||||
|
||||
```py
|
||||
def for_each_right(itr, fn):
|
||||
for el in itr[::-1]:
|
||||
fn(el)
|
||||
```
|
||||
|
||||
```py
|
||||
for_each_right([1, 2, 3], print) # 3 2 1
|
||||
```
|
||||
|
||||
@ -1,23 +1,25 @@
|
||||
---
|
||||
title: frequencies
|
||||
tags: list,intermediate
|
||||
---
|
||||
|
||||
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.
|
||||
|
||||
```py
|
||||
from collections import defaultdict
|
||||
|
||||
def frequencies(lst):
|
||||
freq = defaultdict(int)
|
||||
for val in lst:
|
||||
freq[val] += 1
|
||||
return dict(freq)
|
||||
```
|
||||
|
||||
```py
|
||||
frequencies(['a', 'b', 'a', 'c', 'a', 'a', 'b']) # { 'a': 4, 'b': 2, 'c': 1 }
|
||||
```
|
||||
---
|
||||
title: frequencies
|
||||
tags: list,intermediate
|
||||
firstSeen: 2020-03-15T12:54:08+02:00
|
||||
lastUpdated: 2020-11-02T19:27:53+02:00
|
||||
---
|
||||
|
||||
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.
|
||||
|
||||
```py
|
||||
from collections import defaultdict
|
||||
|
||||
def frequencies(lst):
|
||||
freq = defaultdict(int)
|
||||
for val in lst:
|
||||
freq[val] += 1
|
||||
return dict(freq)
|
||||
```
|
||||
|
||||
```py
|
||||
frequencies(['a', 'b', 'a', 'c', 'a', 'a', 'b']) # { 'a': 4, 'b': 2, 'c': 1 }
|
||||
```
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
---
|
||||
title: from_iso_date
|
||||
tags: date,intermediate
|
||||
firstSeen: 2020-10-28T16:20:04+02:00
|
||||
lastUpdated: 2021-01-07T23:30:28+02:00
|
||||
---
|
||||
|
||||
Converts a date from its ISO-8601 representation.
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
---
|
||||
title: gcd
|
||||
tags: math,beginner
|
||||
firstSeen: 2018-01-08T16:26:35+02:00
|
||||
lastUpdated: 2020-09-15T16:13:06+03:00
|
||||
---
|
||||
|
||||
Calculates the greatest common divisor of a list of numbers.
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
---
|
||||
title: geometric_progression
|
||||
tags: math,list,intermediate
|
||||
firstSeen: 2020-10-04T12:19:28+03:00
|
||||
lastUpdated: 2020-11-02T19:28:05+02:00
|
||||
---
|
||||
|
||||
Initializes a list containing the numbers in the specified range where `start` and `end` are inclusive and the ratio between two terms is `step`.
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
---
|
||||
title: get
|
||||
tags: dictionary,list,intermediate
|
||||
firstSeen: 2020-10-28T12:21:39+02:00
|
||||
lastUpdated: 2020-10-28T12:21:39+02:00
|
||||
---
|
||||
|
||||
Retrieves the value of the nested key indicated by the given selector list from a dictionary or list.
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
---
|
||||
title: group_by
|
||||
tags: list,dictionary,intermediate
|
||||
firstSeen: 2019-08-20T13:29:00+03:00
|
||||
lastUpdated: 2020-11-02T19:28:05+02:00
|
||||
---
|
||||
|
||||
Groups the elements of a list based on the given function.
|
||||
|
||||
@ -1,19 +1,21 @@
|
||||
---
|
||||
title: hamming_distance
|
||||
tags: math,intermediate
|
||||
---
|
||||
|
||||
Calculates the Hamming distance between two values.
|
||||
|
||||
- Use the XOR operator (`^`) to find the bit difference between the two numbers.
|
||||
- Use `bin()` to convert the result to a binary string.
|
||||
- Convert the string to a list and use `count()` of `str` class to count and return the number of `1`s in it.
|
||||
|
||||
```py
|
||||
def hamming_distance(a, b):
|
||||
return bin(a ^ b).count('1')
|
||||
```
|
||||
|
||||
```py
|
||||
hamming_distance(2, 3) # 1
|
||||
```
|
||||
---
|
||||
title: hamming_distance
|
||||
tags: math,intermediate
|
||||
firstSeen: 2021-01-17T13:04:06+02:00
|
||||
lastUpdated: 2021-02-18T14:22:25+02:00
|
||||
---
|
||||
|
||||
Calculates the Hamming distance between two values.
|
||||
|
||||
- Use the XOR operator (`^`) to find the bit difference between the two numbers.
|
||||
- Use `bin()` to convert the result to a binary string.
|
||||
- Convert the string to a list and use `count()` of `str` class to count and return the number of `1`s in it.
|
||||
|
||||
```py
|
||||
def hamming_distance(a, b):
|
||||
return bin(a ^ b).count('1')
|
||||
```
|
||||
|
||||
```py
|
||||
hamming_distance(2, 3) # 1
|
||||
```
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
---
|
||||
title: has_duplicates
|
||||
tags: list,beginner
|
||||
firstSeen: 2018-04-01T11:03:09+03:00
|
||||
lastUpdated: 2020-11-02T19:28:05+02:00
|
||||
---
|
||||
|
||||
Checks if there are duplicate values in a flat list.
|
||||
|
||||
@ -1,22 +1,24 @@
|
||||
---
|
||||
title: have_same_contents
|
||||
tags: list,intermediate
|
||||
---
|
||||
|
||||
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.
|
||||
- Return `False` if the counts do not match for any element, `True` otherwise.
|
||||
|
||||
```py
|
||||
def have_same_contents(a, b):
|
||||
for v in set(a + b):
|
||||
if a.count(v) != b.count(v):
|
||||
return False
|
||||
return True
|
||||
```
|
||||
|
||||
```py
|
||||
have_same_contents([1, 2, 4], [2, 4, 1]) # True
|
||||
```
|
||||
---
|
||||
title: have_same_contents
|
||||
tags: list,intermediate
|
||||
firstSeen: 2020-03-15T12:54:08+02:00
|
||||
lastUpdated: 2020-11-02T19:28:05+02:00
|
||||
---
|
||||
|
||||
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.
|
||||
- Return `False` if the counts do not match for any element, `True` otherwise.
|
||||
|
||||
```py
|
||||
def have_same_contents(a, b):
|
||||
for v in set(a + b):
|
||||
if a.count(v) != b.count(v):
|
||||
return False
|
||||
return True
|
||||
```
|
||||
|
||||
```py
|
||||
have_same_contents([1, 2, 4], [2, 4, 1]) # True
|
||||
```
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
---
|
||||
title: head
|
||||
tags: list,beginner
|
||||
firstSeen: 2019-08-20T14:08:52+03:00
|
||||
lastUpdated: 2020-09-15T16:13:06+03:00
|
||||
---
|
||||
|
||||
Returns the head of a list.
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
---
|
||||
title: hex_to_rgb
|
||||
tags: string,math,intermediate
|
||||
firstSeen: 2020-09-13T01:08:21+03:00
|
||||
lastUpdated: 2020-09-15T16:13:06+03:00
|
||||
---
|
||||
|
||||
Converts a hexadecimal color code to a tuple of integers corresponding to its RGB components.
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
---
|
||||
title: in_range
|
||||
tags: math,beginner
|
||||
firstSeen: 2019-08-20T13:41:40+03:00
|
||||
lastUpdated: 2020-09-15T16:13:06+03:00
|
||||
---
|
||||
|
||||
Checks if the given number falls within the given range.
|
||||
|
||||
@ -1,22 +1,24 @@
|
||||
---
|
||||
title: includes_all
|
||||
tags: list,intermediate
|
||||
---
|
||||
|
||||
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.
|
||||
- Return `False` if any one value is not found, `True` otherwise.
|
||||
|
||||
```py
|
||||
def includes_all(lst, values):
|
||||
for v in values:
|
||||
if v not in lst:
|
||||
return False
|
||||
return True
|
||||
```
|
||||
|
||||
```py
|
||||
includes_all([1, 2, 3, 4], [1, 4]) # True
|
||||
includes_all([1, 2, 3, 4], [1, 5]) # False
|
||||
```
|
||||
---
|
||||
title: includes_all
|
||||
tags: list,intermediate
|
||||
firstSeen: 2020-03-15T12:54:08+02:00
|
||||
lastUpdated: 2020-11-02T19:28:05+02:00
|
||||
---
|
||||
|
||||
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.
|
||||
- Return `False` if any one value is not found, `True` otherwise.
|
||||
|
||||
```py
|
||||
def includes_all(lst, values):
|
||||
for v in values:
|
||||
if v not in lst:
|
||||
return False
|
||||
return True
|
||||
```
|
||||
|
||||
```py
|
||||
includes_all([1, 2, 3, 4], [1, 4]) # True
|
||||
includes_all([1, 2, 3, 4], [1, 5]) # False
|
||||
```
|
||||
|
||||
@ -1,22 +1,24 @@
|
||||
---
|
||||
title: includes_any
|
||||
tags: list,intermediate
|
||||
---
|
||||
|
||||
Checks if any element in `values` is included in `lst`.
|
||||
|
||||
- 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):
|
||||
for v in values:
|
||||
if v in lst:
|
||||
return True
|
||||
return False
|
||||
```
|
||||
|
||||
```py
|
||||
includes_any([1, 2, 3, 4], [2, 9]) # True
|
||||
includes_any([1, 2, 3, 4], [8, 9]) # False
|
||||
```
|
||||
---
|
||||
title: includes_any
|
||||
tags: list,intermediate
|
||||
firstSeen: 2020-03-15T12:54:08+02:00
|
||||
lastUpdated: 2020-11-02T19:28:05+02:00
|
||||
---
|
||||
|
||||
Checks if any element in `values` is included in `lst`.
|
||||
|
||||
- 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):
|
||||
for v in values:
|
||||
if v in lst:
|
||||
return True
|
||||
return False
|
||||
```
|
||||
|
||||
```py
|
||||
includes_any([1, 2, 3, 4], [2, 9]) # True
|
||||
includes_any([1, 2, 3, 4], [8, 9]) # False
|
||||
```
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
---
|
||||
title: index_of_all
|
||||
tags: list,intermediate
|
||||
firstSeen: 2020-10-11T13:40:42+03:00
|
||||
lastUpdated: 2020-10-11T13:45:19+03:00
|
||||
---
|
||||
|
||||
Returns a list of indexes of all the occurrences of an element in a list.
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
---
|
||||
title: initial
|
||||
tags: list,beginner
|
||||
firstSeen: 2019-08-20T14:08:52+03:00
|
||||
lastUpdated: 2020-11-02T19:28:05+02:00
|
||||
---
|
||||
|
||||
Returns all the elements of a list except the last one.
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
---
|
||||
title: initialize_2d_list
|
||||
tags: list,intermediate
|
||||
firstSeen: 2019-10-25T10:11:51+03:00
|
||||
lastUpdated: 2020-11-02T19:28:05+02:00
|
||||
---
|
||||
|
||||
Initializes a 2D list of given width and height and value.
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
---
|
||||
title: initialize_list_with_range
|
||||
tags: list,beginner
|
||||
firstSeen: 2019-08-20T15:21:41+03:00
|
||||
lastUpdated: 2020-11-02T19:28:05+02:00
|
||||
---
|
||||
|
||||
Initializes a list containing the numbers in the specified range where `start` and `end` are inclusive with their common difference `step`.
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
---
|
||||
title: initialize_list_with_values
|
||||
tags: list,beginner
|
||||
firstSeen: 2019-08-20T14:12:06+03:00
|
||||
lastUpdated: 2020-11-02T19:28:05+02:00
|
||||
---
|
||||
|
||||
Initializes and fills a list with the specified value.
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
---
|
||||
title: intersection
|
||||
tags: list,beginner
|
||||
firstSeen: 2019-08-20T15:14:26+03:00
|
||||
lastUpdated: 2020-11-02T19:28:05+02:00
|
||||
---
|
||||
|
||||
Returns a list of elements that exist in both lists.
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
---
|
||||
title: intersection_by
|
||||
tags: list,function,intermediate
|
||||
firstSeen: 2019-08-20T15:16:27+03:00
|
||||
lastUpdated: 2020-11-02T19:28:05+02:00
|
||||
---
|
||||
|
||||
Returns a list of elements that exist in both lists, after applying the provided function to each list element of both.
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
---
|
||||
title: invert_dictionary
|
||||
tags: dictionary,intermediate
|
||||
firstSeen: 2020-04-07T21:13:32+03:00
|
||||
lastUpdated: 2020-11-02T19:28:05+02:00
|
||||
---
|
||||
|
||||
Inverts a dictionary with unique hashable values.
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
---
|
||||
title: is_anagram
|
||||
tags: string,intermediate
|
||||
firstSeen: 2018-10-01T13:17:29+03:00
|
||||
lastUpdated: 2020-11-02T19:28:05+02:00
|
||||
---
|
||||
|
||||
Checks if a string is an anagram of another string (case-insensitive, ignores spaces, punctuation and special characters).
|
||||
|
||||
@ -1,21 +1,23 @@
|
||||
---
|
||||
title: is_contained_in
|
||||
tags: list,intermediate
|
||||
---
|
||||
|
||||
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 occurrences than it has in `b`.
|
||||
- Return `False` if any such value is found, `True` otherwise.
|
||||
|
||||
```py
|
||||
def is_contained_in(a, b):
|
||||
for v in set(a):
|
||||
if a.count(v) > b.count(v):
|
||||
return False
|
||||
return True
|
||||
```
|
||||
|
||||
```py
|
||||
is_contained_in([1, 4], [2, 4, 1]) # True
|
||||
```
|
||||
---
|
||||
title: is_contained_in
|
||||
tags: list,intermediate
|
||||
firstSeen: 2020-03-16T19:48:15+02:00
|
||||
lastUpdated: 2021-01-07T23:30:28+02:00
|
||||
---
|
||||
|
||||
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 occurrences than it has in `b`.
|
||||
- Return `False` if any such value is found, `True` otherwise.
|
||||
|
||||
```py
|
||||
def is_contained_in(a, b):
|
||||
for v in set(a):
|
||||
if a.count(v) > b.count(v):
|
||||
return False
|
||||
return True
|
||||
```
|
||||
|
||||
```py
|
||||
is_contained_in([1, 4], [2, 4, 1]) # True
|
||||
```
|
||||
|
||||
@ -2,6 +2,8 @@
|
||||
title: is_divisible
|
||||
tags: math,beginner
|
||||
unlisted: true
|
||||
firstSeen: 2019-08-20T14:19:55+03:00
|
||||
lastUpdated: 2021-01-04T12:47:04+02:00
|
||||
---
|
||||
|
||||
Checks if the first numeric argument is divisible by the second one.
|
||||
|
||||
@ -2,6 +2,8 @@
|
||||
title: is_even
|
||||
tags: math,beginner
|
||||
unlisted: true
|
||||
firstSeen: 2019-08-20T14:21:44+03:00
|
||||
lastUpdated: 2021-01-04T12:47:04+02:00
|
||||
---
|
||||
|
||||
Checks if the given number is even.
|
||||
|
||||
@ -2,6 +2,8 @@
|
||||
title: is_odd
|
||||
tags: math,beginner
|
||||
unlisted: true
|
||||
firstSeen: 2019-08-20T14:21:44+03:00
|
||||
lastUpdated: 2021-01-04T12:47:04+02:00
|
||||
---
|
||||
|
||||
Checks if the given number is odd.
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
---
|
||||
title: is_prime
|
||||
tags: math,intermediate
|
||||
firstSeen: 2020-10-03T18:03:32+03:00
|
||||
lastUpdated: 2020-11-02T19:28:05+02:00
|
||||
---
|
||||
|
||||
Checks if the provided integer is a prime number.
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
---
|
||||
title: is_weekday
|
||||
tags: date,beginner
|
||||
firstSeen: 2020-10-28T16:20:18+02:00
|
||||
lastUpdated: 2020-11-02T19:28:05+02:00
|
||||
---
|
||||
|
||||
Checks if the given date is a weekday.
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
---
|
||||
title: is_weekend
|
||||
tags: date,beginner
|
||||
firstSeen: 2020-10-28T16:20:27+02:00
|
||||
lastUpdated: 2020-11-02T19:28:05+02:00
|
||||
---
|
||||
|
||||
Checks if the given date is a weekend.
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
---
|
||||
title: kebab
|
||||
tags: string,regexp,intermediate
|
||||
firstSeen: 2019-08-21T08:59:54+03:00
|
||||
lastUpdated: 2020-11-02T19:28:05+02:00
|
||||
---
|
||||
|
||||
Converts a string to kebab case.
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
---
|
||||
title: key_in_dict
|
||||
tags: dictionary,beginner
|
||||
firstSeen: 2020-10-16T21:30:49+03:00
|
||||
lastUpdated: 2020-10-16T21:30:49+03:00
|
||||
---
|
||||
|
||||
Checks if the given key exists in a dictionary.
|
||||
|
||||
@ -1,17 +1,19 @@
|
||||
---
|
||||
title: key_of_max
|
||||
tags: dictionary,beginner
|
||||
---
|
||||
|
||||
Finds the key of the maximum value in a dictionary.
|
||||
|
||||
- Use `max()` with the `key` parameter set to `dict.get()` to find and return the key of the maximum value in the given dictionary.
|
||||
|
||||
```py
|
||||
def key_of_max(d):
|
||||
return max(d, key = d.get)
|
||||
```
|
||||
|
||||
```py
|
||||
key_of_max({'a':4, 'b':0, 'c':13}) # c
|
||||
```
|
||||
---
|
||||
title: key_of_max
|
||||
tags: dictionary,beginner
|
||||
firstSeen: 2021-01-07T23:15:48+02:00
|
||||
lastUpdated: 2021-01-07T23:15:48+02:00
|
||||
---
|
||||
|
||||
Finds the key of the maximum value in a dictionary.
|
||||
|
||||
- Use `max()` with the `key` parameter set to `dict.get()` to find and return the key of the maximum value in the given dictionary.
|
||||
|
||||
```py
|
||||
def key_of_max(d):
|
||||
return max(d, key = d.get)
|
||||
```
|
||||
|
||||
```py
|
||||
key_of_max({'a':4, 'b':0, 'c':13}) # c
|
||||
```
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user