Skip to content

Commit

Permalink
Add more leap approaches (#180)
Browse files Browse the repository at this point in the history
  • Loading branch information
BNAndras authored Jan 17, 2024
1 parent 35d946f commit 7c8c9d2
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
PLACEHOLDER
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
11 changes: 10 additions & 1 deletion exercises/practice/leap/.approaches/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
Expand Down
32 changes: 32 additions & 0 deletions exercises/practice/leap/.approaches/if-expressions/content.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
20 changes: 19 additions & 1 deletion exercises/practice/leap/.approaches/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
[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

0 comments on commit 7c8c9d2

Please sign in to comment.