-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[Leap Approaches]: Typos & Fixes per issue 4197 #4252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
16 changes: 8 additions & 8 deletions
16
exercises/practice/leap/.approaches/calendar-isleap/content.md
This file contains hidden or 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,38 +1,38 @@ | ||
| # The `calendar.isleap()` function | ||
|
|
||
| ```pythoon | ||
| ```python | ||
| from calendar import isleap | ||
|
|
||
| def leap_year(year): | ||
| return isleap(year) | ||
| ``` | ||
|
|
||
| ~~~~exercism/caution | ||
| This approach may be considered a "cheat" for this exercise, which is intended to practice Boolean operators and logic. | ||
| This approach may be considered a "cheat" for this exercise, which is intended to practice Boolean operators and Boolean logic. | ||
| ~~~~ | ||
|
|
||
|
|
||
| The Python standard library includes a [`calendar`][calendar] module for working with many aspects of dates in the [Gregorian calendar][gregorian-calendar]. | ||
|
|
||
| One of the methods provided is [`isleap()`][isleap], which implements exactly the same functionality as this exercise. | ||
|
|
||
| This is not a good way to practice the use of Booleans, as the exercise intends. | ||
| However, it may be convenient (_and better tested_) if you are working with calendar functions more broadly. | ||
| However, it may be convenient (_and better tested_) if you are working with `calendar` functions more broadly. | ||
|
|
||
|
|
||
| ## The library function | ||
|
|
||
| This is the [implementation][implementation]: | ||
| This is the actual [implementation][implementation] in the CPython code: | ||
|
|
||
| ```python | ||
| def isleap(year): | ||
| """Return True for leap years, False for non-leap years.""" | ||
| return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0) | ||
| ``` | ||
|
|
||
| We can see that `calendar.isleap()` is just syntactic sugar for the `boolean-chain` approach. | ||
|
|
||
| We can see that `calendar.isleap()` is just syntactic sugar for the [`boolean-chain`][approach-boolean-chain] approach. | ||
|
|
||
| [approach-boolean-chain]: https://exercism.org/tracks/python/exercises/leap/approaches/boolean-chain | ||
| [calendar]: https://docs.python.org/3/library/calendar.html | ||
| [gregorian-calendar]: https://en.wikipedia.org/wiki/Gregorian_calendar | ||
| [implementation]: https://github.com/python/cpython/blob/main/Lib/calendar.py | ||
| [implementation]: https://github.com/python/cpython/blob/3.13/Lib/calendar.py#L143-L145 | ||
| [isleap]: https://docs.python.org/3/library/calendar.html |
This file contains hidden or 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,37 +1,41 @@ | ||
| { | ||
| "introduction": { | ||
| "authors": ["bobahop"], | ||
| "contributors": ["colinleach"] | ||
| "contributors": ["colinleach", "Yrahcaz7", "Bethanyg"] | ||
| }, | ||
| "approaches": [ | ||
| { | ||
| "uuid": "5d42dc83-2473-425a-90bd-bf03f92b8c8b", | ||
| "slug": "boolean-chain", | ||
| "title": "Boolean chain", | ||
| "blurb": "Use a chain of boolean expressions.", | ||
| "authors": ["bobahop"] | ||
| "authors": ["bobahop"], | ||
| "contributors": ["colinleach", "Yrahcaz7", "Bethanyg"] | ||
| }, | ||
| { | ||
| "uuid": "9952fef5-9f2f-4575-94fc-bc4e96593cd6", | ||
| "uuid": "37193c94-1b5f-4891-a685-11def9204839", | ||
| "slug": "ternary-operator", | ||
| "title": "Ternary operator", | ||
| "blurb": "Use a ternary operator of boolean expressions.", | ||
| "authors": ["bobahop"] | ||
| "authors": ["bobahop"], | ||
| "contributors": ["colinleach", "Yrahcaz7", "Bethanyg"] | ||
| }, | ||
| { | ||
| "uuid": "66302791-0770-4f08-beaa-251c49e280a2", | ||
| "slug": "datetime-addition", | ||
| "title": "datetime addition", | ||
| "blurb": "Use datetime addition.", | ||
| "authors": ["bobahop"] | ||
| "authors": ["bobahop"], | ||
| "contributors": ["colinleach", "Yrahcaz7", "Bethanyg"] | ||
| }, | ||
| { | ||
| "uuid": "d85be356-211a-4d2f-8af0-fa92e390b0b3", | ||
| "slug": "calendar-isleap", | ||
| "title": "calendar.isleap() function", | ||
| "blurb": "Use the calendar module.", | ||
| "authors": ["colinleach", | ||
| "BethanyG"] | ||
| "authors": ["colinleach", "BethanyG"], | ||
| "contributors": ["Yrahcaz7"] | ||
| } | ||
| ] | ||
| } | ||
|
|
29 changes: 15 additions & 14 deletions
29
exercises/practice/leap/.approaches/datetime-addition/content.md
This file contains hidden or 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,29 +1,30 @@ | ||
| # `datetime` addition | ||
|
|
||
| ```python | ||
| import datetime | ||
| from datetime import datetime, timedelta | ||
|
|
||
|
|
||
| def leap_year(year): | ||
| return (datetime.datetime(year, 2, 28) | ||
| + datetime.timedelta(days=1)).day == 29 | ||
|
|
||
| return (datetime(year, 2, 28) | ||
| + timedelta(days=1)).day == 29 | ||
| ``` | ||
|
|
||
| ~~~~exercism/caution | ||
| This approach may be considered a "cheat" for this exercise, which is intended to practice Boolean operators and logic. | ||
| It also adds a tremendous amount of overhead in both performance and memory, as it imports all of the `datetime` module and requires the instantiation of both a `datetime` object and a `datetime.timedelta` object. | ||
| ~~~~exercism/note | ||
| This approach may be considered a "cheat" for this exercise, which is intended to practice Boolean operators and boolean logic. | ||
| It also adds overhead in both performance and memory, as it imports methods from the `datetime` module and requires the instantiation of both a `datetime` object and a `timedelta` object. | ||
|
|
||
| For more information, see this exercises performance article. | ||
| For more information, see the performance article for this exercise. | ||
| ~~~~ | ||
|
|
||
| By adding a day to February 28th for a given year, you can see if the new day falls on the 29th of February, or the 1st of March. | ||
|
|
||
| By adding a day to February 28th for a given year, you can see if the new day falls on the 29th of February or the 1st of March. | ||
| If it is February 29th, then the function returns `True` for the year being a leap year. | ||
| The exact steps are as follows: | ||
|
|
||
| - A new [datetime][datetime] object is created for February 28th of the year. | ||
| - Then the [timedelta][timedelta] of one day is added to that `datetime`, | ||
| and the function returns if the [day][day] property of the resulting `datetime` object is the 29th. | ||
| - A new [`datetime`][datetime] object is created for February 28th of the year. | ||
| - A [`timedelta`][timedelta] of one day is added to that `datetime`. | ||
| - The function returns if the [`day`][day] property of the resulting `datetime` object is the 29th. | ||
|
BethanyG marked this conversation as resolved.
|
||
|
|
||
| [timedelta]: https://docs.python.org/3/library/datetime.html#timedelta-objects | ||
| [day]: https://docs.python.org/3/library/datetime.html#datetime.datetime.day | ||
| [datetime]: https://docs.python.org/3/library/datetime.html#datetime-objects | ||
| [day]: https://docs.python.org/3/library/datetime.html#datetime.datetime.day | ||
| [timedelta]: https://docs.python.org/3/library/datetime.html#timedelta-objects | ||
5 changes: 2 additions & 3 deletions
5
exercises/practice/leap/.approaches/datetime-addition/snippet.txt
This file contains hidden or 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,6 +1,5 @@ | ||
| import datetime | ||
| from datetime import datetime, timedelta | ||
|
|
||
|
|
||
| def leap_year(year): | ||
| return (datetime.datetime(year, 2, 28) | ||
| + datetime.timedelta(days=1)).day == 29 | ||
| return (datetime(year, 2, 28) + timedelta(days=1)).day == 29 |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.