From 7aa97816c0c54fb050b5c1a5e2c94dc25c544cfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20B=20Nagy?= <20251272+BNAndras@users.noreply.github.com> Date: Mon, 15 Jan 2024 18:19:22 -0800 Subject: [PATCH] Add boolean chain approach --- .../leap/.approaches/boolean-chain/content.md | 36 +++++++++++++++++++ .../.approaches/boolean-chain/snippet.txt | 1 + .../practice/leap/.approaches/config.json | 18 ++++++++++ .../practice/leap/.approaches/introduction.md | 25 +++++++++++++ 4 files changed, 80 insertions(+) create mode 100644 exercises/practice/leap/.approaches/boolean-chain/content.md create mode 100644 exercises/practice/leap/.approaches/boolean-chain/snippet.txt create mode 100644 exercises/practice/leap/.approaches/config.json create mode 100644 exercises/practice/leap/.approaches/introduction.md 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 0000000..e924104 --- /dev/null +++ b/exercises/practice/leap/.approaches/boolean-chain/content.md @@ -0,0 +1,36 @@ +# Boolean chaining + +```pyret +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 +``` + +This approach uses the Boolean operators `and` and `or` to chain together values from two Boolean expressions, creating a Boolean value. +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. + +| n1 | n2 | n1 OR n2 | n1 AND n2 | +| ----- | ----- | ------- | --------- | +| false | false | false | false | +| false | true | true | false | +| true | false | true | false | +| true | true | true | true | + +Both operators can short-circuit which means in some scenarios, the operators don't evaluate the expression to the right. +For `and`, if the left side produces `false`, that value is returned immediately. +For `or`, if the left side produces `true`, that value is returned immediately. + +We can now test if a year is evenly divisible by 4, 100, and 400. +All leap years are divisible by 4 but not by 100 unless they're also divisible by 100. + +| 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 | 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 0000000..311c8dd --- /dev/null +++ b/exercises/practice/leap/.approaches/boolean-chain/snippet.txt @@ -0,0 +1 @@ +PLACEHOLDER \ No newline at end of file diff --git a/exercises/practice/leap/.approaches/config.json b/exercises/practice/leap/.approaches/config.json new file mode 100644 index 0000000..683145d --- /dev/null +++ b/exercises/practice/leap/.approaches/config.json @@ -0,0 +1,18 @@ +{ + "introduction": { + "authors": [ + "BNAndras" + ] + }, + "approaches": [ + { + "uuid": "2f53896f-b3e7-4c7d-be3a-a3342087826e", + "slug": "boolean-chain", + "title": "Boolean Chaining", + "blurb": "Use operators to check boolean values in a chain", + "authors": [ + "BNAndras" + ] + } + ] +} diff --git a/exercises/practice/leap/.approaches/introduction.md b/exercises/practice/leap/.approaches/introduction.md new file mode 100644 index 0000000..01d2f24 --- /dev/null +++ b/exercises/practice/leap/.approaches/introduction.md @@ -0,0 +1,25 @@ +# Introduction + +There are two approaches highlighted here for his exercise. +Both involve checking if a year is evenly divisible by 4, 100, and 400 using [num-modulo][num-modulo]. + +## General guidance + +Regardless of the approach chosen, this exercise requires students to use Boolean logic to decide if a given year is a leap year. + +## Approach: Boolean chaining + +```pyret +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 +``` + +For more information, check the [Boolean chain approach][approach-boolean-chain]. + +[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