-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
77 additions
and
4 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 +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 |
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
32 changes: 32 additions & 0 deletions
32
exercises/practice/leap/.approaches/if-expressions/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,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 |
8 changes: 8 additions & 0 deletions
8
exercises/practice/leap/.approaches/if-expressions/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,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 |
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