diff --git a/exercises/practice/leap/.approaches/boolean-chain/content.md b/exercises/practice/leap/.approaches/boolean-chain/content.md index e924104..648129a 100644 --- a/exercises/practice/leap/.approaches/boolean-chain/content.md +++ b/exercises/practice/leap/.approaches/boolean-chain/content.md @@ -10,7 +10,7 @@ fun leap(year): end ``` -This approach uses the Boolean operators `and` and `or` to chain together values from two Boolean expressions, creating a Boolean value. +This approach uses a helper function `year-is-divisible-by` and the Boolean operators `and` and `or` to run each check (divisible by 4, not divisible by 100, and divisible by 400) in order, producing a single Boolean value at the end. The `and` operator returns `true` when both sides are true but `false` otherwise. The `or` operator returns `false` when both sides are false but `true` otherwise. diff --git a/exercises/practice/leap/.approaches/boolean-chain/snippet.txt b/exercises/practice/leap/.approaches/boolean-chain/snippet.txt index 311c8dd..d2c45c8 100644 --- a/exercises/practice/leap/.approaches/boolean-chain/snippet.txt +++ b/exercises/practice/leap/.approaches/boolean-chain/snippet.txt @@ -1 +1,7 @@ -PLACEHOLDER \ No newline at end of file +fun leap(year): + fun year-is-divisible-by(divisor): + num-equal(num-modulo(year, divisor), 0) + end + + year-is-divisible-by(4) and (not(year-is-divisible-by(100)) or year-is-divisible-by(400)) +end \ No newline at end of file diff --git a/exercises/practice/leap/.approaches/config.json b/exercises/practice/leap/.approaches/config.json index 683145d..d1e7b01 100644 --- a/exercises/practice/leap/.approaches/config.json +++ b/exercises/practice/leap/.approaches/config.json @@ -9,7 +9,16 @@ "uuid": "2f53896f-b3e7-4c7d-be3a-a3342087826e", "slug": "boolean-chain", "title": "Boolean Chaining", - "blurb": "Use operators to check boolean values in a chain", + "blurb": "Use operators to check Boolean values in a chain", + "authors": [ + "BNAndras" + ] + }, + { + "uuid": "19b192ea-7dbe-4c22-b29c-93df1127f7e0", + "slug": "if-expressions", + "title": "If Expressions", + "blurb": "Use if expressions to conditionally check a leap year", "authors": [ "BNAndras" ] diff --git a/exercises/practice/leap/.approaches/if-expressions/content.md b/exercises/practice/leap/.approaches/if-expressions/content.md new file mode 100644 index 0000000..590f0d6 --- /dev/null +++ b/exercises/practice/leap/.approaches/if-expressions/content.md @@ -0,0 +1,32 @@ +# If expressions + +```pyret +fun leap(year): + if num-equal(num-modulo(year, 400), 0): + true + else if num-equal(num-modulo(year, 100), 0): + false + else if num-equal(num-modulo(year, 4), 0): + true + else: + false +end +``` + +An [if expression][if-expression] contains one or more branches with conditions that are checked sequentially and associated blocks of code. +Once a condition produces `true`, the associated block is evaluated and its contents returned. +No subsequent conditions will be tested. +One of the conditions in the if expression must produce `true` or an error is thrown. +Therefore, `else` is used to provide a branch that is evaluated when the other branches' conditions aren't satisified. + +If a year is evenly divisible by 400, the first condition `num-equal(num-modulo(year, 400), 0)` produces `true`, and the associated block is evaluated, returning `true`. + +If a year is divisible by 100, the second condition `num-equal(num-modulo(year, 100), 0)` produces `true`, and the associated block is evaluated, returning `false`. If this year had also been divisible by 400, the first condition would have been satisfied and the code would not have reached this point. + +If a year is divisible by 4, the third condition `num-equal(num-modulo(year, 4), 0)` produces `true`, and the associated block is evaluated, returning `true`. +Because of the previous conditions, this year isn't also divisible by 400 or 100. + +Finally, if no other branches' conditions were true, the block associated with `else` is evaluated, representing when a year isn't divisible by 4, 100, or 400. +Here, `false` is returned. + +[if-expression]: https://pyret.org/docs/latest/Expressions.html#%28part._s~3aif-expr%29 diff --git a/exercises/practice/leap/.approaches/if-expressions/snippet.txt b/exercises/practice/leap/.approaches/if-expressions/snippet.txt new file mode 100644 index 0000000..70612c5 --- /dev/null +++ b/exercises/practice/leap/.approaches/if-expressions/snippet.txt @@ -0,0 +1,8 @@ +fun leap(year): + if num-equal(num-modulo(year, 400), 0): + true + else if num-equal(num-modulo(year, 100), 0): + false + else if num-equal(num-modulo(year, 4), 0): + true +# clipped for brevity \ No newline at end of file diff --git a/exercises/practice/leap/.approaches/introduction.md b/exercises/practice/leap/.approaches/introduction.md index 01d2f24..94e978a 100644 --- a/exercises/practice/leap/.approaches/introduction.md +++ b/exercises/practice/leap/.approaches/introduction.md @@ -21,5 +21,23 @@ end For more information, check the [Boolean chain approach][approach-boolean-chain]. +## Approach: If expressions + +```pyret +fun leap(year): + if num-equal(num-modulo(year, 400), 0): + true + else if num-equal(num-modulo(year, 100), 0): + false + else if num-equal(num-modulo(year, 4), 0): + true + else: + false +end +``` + +For more information, check the [if expressions approach][approach-if-expressions]. + [num-modulo]: https://pyret.org/docs/latest/numbers.html#%28part._numbers_num-modulo%29 -[approach-boolean-chain]: https://exercism.org/tracks/pyret/exercises/leap/approaches/boolean-chain \ No newline at end of file +[approach-boolean-chain]: https://exercism.org/tracks/pyret/exercises/leap/approaches/boolean-chain +[approach-if-expressions]: https://exercism.org/tracks/pyret/exercises/leap/approaches/if-expressions \ No newline at end of file