-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
leap approaches and performance article
- Loading branch information
Showing
14 changed files
with
209 additions
and
167 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1 @@ | ||
year=$1 | ||
if (( year % 4 == 0 && (year % 100 != 0 || year % 400 == 0) )); then | ||
echo true | ||
else | ||
echo false | ||
fi | ||
|
||
expr {$year % 4 == 0 && ($year % 100 != 0 || $year % 400 == 0)} |
23 changes: 23 additions & 0 deletions
23
exercises/practice/leap/.approaches/clock-command/content.md
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Using the `clock` command | ||
|
||
Using [the `clock` command][tcl-clock] approach may be considered a "cheat" for this exercise. | ||
|
||
```tcl | ||
set timestamp [clock scan "$year-02-28" -format {%Y-%m-%d}] | ||
set next_day [clock add $timestamp 1 day] | ||
set day [clock format $next_day -format {%d}] | ||
expr {$day == 29} | ||
``` | ||
|
||
By adding a day to February 28th for the year, you can see if the new day is the 29th or the 1st. | ||
If it is the 29th, then the year is a leap year. | ||
|
||
Reference: [`clock` manual page](https://tcl.tk/man/tcl8.6/TclCmd/clock.htm) | ||
|
||
~~~~exercism/note | ||
[Under the hood][tcl-src-leap], Tcl does have an internal helper function to test for leap years. | ||
[tcl-src-leap]: https://github.com/tcltk/tcl/blob/37176a333aa886595daaddbdf14ae7cacd1f06b0/generic/tclClock.c#L1561 | ||
~~~~ | ||
|
||
[tcl-clock]: https://tcl.tk/man/tcl8.6/TclCmd/clock.htm |
4 changes: 4 additions & 0 deletions
4
exercises/practice/leap/.approaches/clock-command/snippet.txt
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
set timestamp [clock scan "$year-02-28" -format {%Y-%m-%d}] | ||
set next_day [clock add $timestamp 1 day] | ||
set day [clock format $next_day -format {%d}] | ||
expr {$day == 29} |
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
61 changes: 0 additions & 61 deletions
61
exercises/practice/leap/.approaches/external-tools/content.md
This file was deleted.
Oops, something went wrong.
3 changes: 0 additions & 3 deletions
3
exercises/practice/leap/.approaches/external-tools/snippet.txt
This file was deleted.
Oops, something went wrong.
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
7 changes: 1 addition & 6 deletions
7
exercises/practice/leap/.approaches/ternary-operator/snippet.txt
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,6 +1 @@ | ||
year=$1 | ||
if (( year % 100 == 0 ? year % 400 == 0 : year % 4 == 0 )); then | ||
echo true | ||
else | ||
echo false | ||
fi | ||
expr {$year % 100 == 0 ? $year % 400 == 0 : $year % 4 == 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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"articles": [ | ||
{ | ||
"uuid": "2d4c0b08-4830-4bd9-a985-1c0d3bf71980", | ||
"slug": "performance", | ||
"title": "Performance demonstration", | ||
"blurb": "Compare the performances of the various leap year approaches.", | ||
"authors": [ | ||
"glennj" | ||
] | ||
} | ||
] | ||
} |
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
proc leap_bool {year} { | ||
return [expr {$year % 4 == 0 && ($year % 100 != 0 || $year % 400 == 0)}] | ||
} | ||
|
||
proc leap_ternary {year} { | ||
return [expr {$year % 100 == 0 ? $year % 400 == 0 : $year % 4 == 0}] | ||
} | ||
|
||
proc leap_clock {year} { | ||
set timestamp [clock scan "$year-02-28" -format {%Y-%m-%d}] | ||
set next_day [clock add $timestamp 1 day] | ||
set day [clock format $next_day -format {%d}] | ||
return [expr {$day == 29}] | ||
} | ||
|
||
proc time_it {procname} { | ||
foreach year {2023 2024 1900 2000} { | ||
puts [format {%d - %d - %s} \ | ||
$year \ | ||
[$procname $year] \ | ||
[time {$procname $year} 1000]] | ||
} | ||
} | ||
|
||
foreach procname {leap_bool leap_ternary leap_clock} { | ||
puts $procname | ||
time_it $procname | ||
puts "" | ||
} | ||
|
||
set output { | ||
leap_bool | ||
2023 - 0 - 0.378 microseconds per iteration | ||
2024 - 1 - 0.472 microseconds per iteration | ||
1900 - 0 - 0.525 microseconds per iteration | ||
2000 - 1 - 0.533 microseconds per iteration | ||
|
||
leap_ternary | ||
2023 - 0 - 0.428 microseconds per iteration | ||
2024 - 1 - 0.425 microseconds per iteration | ||
1900 - 0 - 0.425 microseconds per iteration | ||
2000 - 1 - 0.434 microseconds per iteration | ||
|
||
leap_clock | ||
2023 - 0 - 65.716 microseconds per iteration | ||
2024 - 1 - 70.092 microseconds per iteration | ||
1900 - 0 - 59.396 microseconds per iteration | ||
2000 - 1 - 72.496 microseconds per iteration | ||
} |
Oops, something went wrong.