diff --git a/exercises/practice/leap/.approaches/boolean-chain/content.md b/exercises/practice/leap/.approaches/boolean-chain/content.md new file mode 100644 index 00000000..0d44d9bf --- /dev/null +++ b/exercises/practice/leap/.approaches/boolean-chain/content.md @@ -0,0 +1,54 @@ +# Chain of boolean expressions + +```r +leap <- function(year) { + year %% 4 == 0 && (year %% 100 != 0 || year %% 400 == 0) +} +``` + +The first boolean expression uses the [modulo operator][arithmetic-operators] (`%%`) to check if the year is evenly divided by `4`. +- If the year is not evenly divisible by `4`, then the chain will "short circuit" due to the next operator being a [logical AND][logical-operators] `&`), and will return `FALSE`. +- If the year _is_ evenly divisible by `4`, then the year is checked to _not_ be evenly divisible by `100`. +- If the year is not evenly divisible by `100`, then the expression is `TRUE` and the chain will "short-circuit" to return `TRUE`, +since the next operator is a [logical OR][logical-operators] (`|`). +- If the year _is_ evenly divisible by `100`, then the expression is `FALSE`, and the returned value from the chain will be if the year is evenly divisible by `400`. + +| year | year %% 4 == 0 | year %% 100 != 0 | year %% 400 == 0 | is leap year | +| ---- | ------------- | --------------- | --------------- | ------------ | +| 2020 | TRUE | TRUE | not evaluated | TRUE | +| 2019 | FALSE | not evaluated | not evaluated | FALSE | +| 2000 | TRUE | FALSE | TRUE | TRUE | +| 1900 | TRUE | FALSE | FALSE | FALSE | + + +The chain of boolean expressions is efficient, as it proceeds from testing the most likely to least likely conditions. +It is the fastest approach when testing a year that is not evenly divisible by `100` and is not a leap year. + +## Refactoring + +By using the [falsiness][logical-vectors] of `0`, the [not operator][logical-operators] (`!`) can be used instead of comparing equality to `0`. +For example: + +```r +leap <- function(year) { + !year %% 4 && (year %% 100 != 0 || !year %% 400) +} +``` + +It can be thought of as the expression _not_ having a remainder. While it may harm readability (this is of course subjective), based on benchmarking this is slightly faster than checking whether the remainder equals zero. + +## Vectorised implementation + +In R many operations are vectorised (by default), or can be vectorised. `|` and `&` (by contrast to `||` and `&&`) are vectorised versions of [logical operators][logical-operators] which could also be used if one wanted to allow for a vector of years as input. + +```r +leap <- function(year) { + year %% 4 == 0 & (year %% 100 != 0 | year %% 400 == 0) +} +``` + +In general with vectorised operations the trade-off is that it tends to be slower for operations on vectors of length 1 (R doesn't have an atomic scalar data type so scalars are still just vectors of length 1), but in many cases vectorised implementations can be more efficient than naive looping when dealing with multiple input values, and the difference can be quite dramatic for more complex computations. + +[arithmetic-operators]: https://stat.ethz.ch/R-manual/R-patched/library/base/html/Arithmetic.html +[logical-operators]: https://stat.ethz.ch/R-manual/R-devel/library/base/html/Logic.html +[logical-vectors]: https://stat.ethz.ch/R-manual/R-devel/library/base/html/logical.html diff --git a/exercises/practice/leap/.approaches/boolean-chain/snippet.txt b/exercises/practice/leap/.approaches/boolean-chain/snippet.txt new file mode 100644 index 00000000..5f5e93a0 --- /dev/null +++ b/exercises/practice/leap/.approaches/boolean-chain/snippet.txt @@ -0,0 +1,3 @@ +leap <- function(year) { + year %% 4 == 0 && (year %% 100 != 0 || year %% 400 == 0) +} diff --git a/exercises/practice/leap/.approaches/conditional-expression/content.md b/exercises/practice/leap/.approaches/conditional-expression/content.md new file mode 100644 index 00000000..83d4b0c8 --- /dev/null +++ b/exercises/practice/leap/.approaches/conditional-expression/content.md @@ -0,0 +1,45 @@ +# Conditional expression + +```r +leap <- function(year) { + if (year %% 100 == 0) + year %% 400 == 0 + else + year %% 4 == 0 +} +``` + +A [conditional expression][control-flow] uses a maximum of two checks to determine if a year is a leap year. + +It starts by testing the outlier condition of the year being evenly divisible by `100`. +It does this by using the [modulo operator][arithmetic-operators] (`%%`) and checking whether the remainder is `0`. + +- If the year is evenly divisible by `100`, then the expression is `TRUE`, and the conditional expression returns if the year is evenly divisible by `400`. +- If the year is _not_ evenly divisible by `100`, then the expression is `FALSE`, and the conditional expression returns if the year is evenly divisible by `4`. + +| year | year %% 100 == 0 | year %% 400 == 0 | year %% 4 == 0 | is leap year | +| ---- | --------------- | --------------- | -------------- | ------------ | +| 2020 | FALSE | not evaluated | TRUE | TRUE | +| 2019 | FALSE | not evaluated | FALSE | FALSE | +| 2000 | TRUE | TRUE | not evaluated | TRUE | +| 1900 | TRUE | FALSE | not evaluated | FALSE | + +Although it uses a maximum of only two checks, the conditional expression tests an outlier condition first, +making it less efficient than another approach that would first test if the year is evenly divisible by `4`, +which is more likely than the year being evenly divisible by `100`. +The conditional expression was fastest in benchmarking when the year was a leap year or was evenly divisible by `100`, +but those are the least likely conditions. + +In R many operations are vectorised (by default), or can be vectorised. [`ifelse`][ifelse] is a vectorised version of `if ... else ...`, which could also be used if one wanted to allow for a vector of years as input. + +```r +leap <- function(year) { + ifelse(year %% 100 == 0, year %% 400 == 0, year %% 4 == 0) +} +``` + +In general with vectorised operations the trade-off is that it tends to be slower for operations on vectors of length 1 (R doesn't have an atomic scalar data type so scalars are still just vectors of length 1), but in many cases vectorised implementations can be more efficient than naive looping when dealing with multiple input values, and the difference can be quite dramatic for more complex computations. + +[control-flow]: https://stat.ethz.ch/R-manual/R-patched/library/base/html/Control.html +[arithmetic-operators]: https://stat.ethz.ch/R-manual/R-patched/library/base/html/Arithmetic.html +[ifelse]: https://stat.ethz.ch/R-manual/R-patched/library/base/html/ifelse.html diff --git a/exercises/practice/leap/.approaches/conditional-expression/snippet.txt b/exercises/practice/leap/.approaches/conditional-expression/snippet.txt new file mode 100644 index 00000000..d35cca19 --- /dev/null +++ b/exercises/practice/leap/.approaches/conditional-expression/snippet.txt @@ -0,0 +1,6 @@ +leap <- function(year) { + if (year %% 100 == 0) + year %% 400 == 0 + else + year %% 4 == 0 +} diff --git a/exercises/practice/leap/.approaches/config.json b/exercises/practice/leap/.approaches/config.json new file mode 100644 index 00000000..3c0e1250 --- /dev/null +++ b/exercises/practice/leap/.approaches/config.json @@ -0,0 +1,23 @@ +{ + "introduction": { + "authors": [ + "jonmcalder" + ] + }, + "approaches": [ + { + "uuid": "6fb5f80f-706c-4bbb-ba3e-cd53bca110bd", + "slug": "boolean-chain", + "title": "Boolean chain", + "blurb": "Use a chain of boolean expressions.", + "authors": ["jonmcalder"] + }, + { + "uuid": "aa5cd568-60e7-4d9d-9573-752ecdbb92f7", + "slug": "conditional-expression", + "title": "Conditional expression", + "blurb": "Use an if / else statement.", + "authors": ["jonmcalder"] + } + ] +} diff --git a/exercises/practice/leap/.approaches/introduction.md b/exercises/practice/leap/.approaches/introduction.md new file mode 100644 index 00000000..e4a5f577 --- /dev/null +++ b/exercises/practice/leap/.approaches/introduction.md @@ -0,0 +1,48 @@ +# Introduction + +There are various idiomatic approaches to solve Leap. +You can use a chain of boolean expressions to test the conditions. +Or you can use a [conditional expression][control-flow]. + +## General guidance + +The key to solving Leap is to know if the year is evenly divisible by `4`, `100` and `400`. +For determining that, you will use the [modulo operator][arithmetic-operators] (`%%`) to check whether the remainder is `0`. + +## Approach: Chain of Boolean expressions + +```r +leap <- function(year) { + year %% 4 == 0 && (year %% 100 != 0 || year %% 400 == 0) +} +``` + +For more information, check the [Boolean chain approach][approach-boolean-chain]. + +## Approach: Conditional expression + +```r +leap <- function(year) { + if (year %% 100 == 0) + year %% 400 == 0 + else + year %% 4 == 0 +} +``` + +For more information, check the [conditional expression approach][approach-conditional-expression]. + +## Which approach to use? + +- The chain of boolean expressions should be the most efficient, as it proceeds from the most likely to least likely conditions. +It has a maximum of three checks. +It is the fastest approach when testing a year that is not evenly divisible by `100` and is not a leap year. +Since most years fit those conditions, it is the most efficient approach overall. +- The conditional expression approach has a maximum of only two checks, but it starts from a less likely condition. +The conditional expression approach was faster in benchmarking when the year was a leap year or was evenly divisible by `100`, +but those are the least likely conditions. + +[control-flow]: https://stat.ethz.ch/R-manual/R-patched/library/base/html/Control.html +[arithmetic-operators]: https://stat.ethz.ch/R-manual/R-patched/library/base/html/Arithmetic.html +[approach-boolean-chain]: https://exercism.org/tracks/r/exercises/leap/approaches/boolean-chain +[approach-conditional-expression]: https://exercism.org/tracks/r/exercises/leap/approaches/conditional-expression