Skip to content

Commit

Permalink
first draft of the introduction
Browse files Browse the repository at this point in the history
  • Loading branch information
michalporeba committed Jan 7, 2024
1 parent 012d238 commit 3ebe221
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 20 deletions.
1 change: 0 additions & 1 deletion exercises/practice/leap/.approaches/and-or/content.md

This file was deleted.

Empty file.
1 change: 1 addition & 0 deletions exercises/practice/leap/.approaches/boolean/content.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Boolean Operations Approach
4 changes: 4 additions & 0 deletions exercises/practice/leap/.approaches/boolean/snippet.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(defn leap-year? [year]
(or (divides? year 400)
(and (not (divides? year 100))
(divides? year 4))))
1 change: 0 additions & 1 deletion exercises/practice/leap/.approaches/cond/content.md

This file was deleted.

Empty file.
1 change: 1 addition & 0 deletions exercises/practice/leap/.approaches/conditional/content.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Conditional Branching Approach
4 changes: 4 additions & 0 deletions exercises/practice/leap/.approaches/conditional/snippet.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(defn leap-year? [year]
(if (divides? year 100)
(divides? year 400)
(divides? year 4)))
25 changes: 8 additions & 17 deletions exercises/practice/leap/.approaches/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,19 @@
},
"approaches": [
{
"uuid": "",
"slug": "and-or",
"title": "",
"blurb": "",
"uuid": "83290a7b-3e87-46a1-9b82-10b029716ad1",
"slug": "boolean",
"title": "Boolean Operations",
"blurb": "Solving Leap by using boolean operations `and`, `or` and `not`.",
"authors": [
"michalporeba"
]
},
{
"uuid": "",
"slug": "cond",
"title": "",
"blurb": "",
"authors": [
"michalporeba"
]
},
{
"uuid": "",
"slug": "if",
"title": "",
"blurb": "",
"uuid": "a1927e29-b262-4a83-a66d-0e30699e6c21",
"slug": "conditional",
"title": "Conditional Branching",
"blurb": "Solving Leap by using conditional branching functions `if` and `cond`.",
"authors": [
"michalporeba"
]
Expand Down
1 change: 0 additions & 1 deletion exercises/practice/leap/.approaches/if/content.md

This file was deleted.

Empty file.
57 changes: 57 additions & 0 deletions exercises/practice/leap/.approaches/introduction.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,59 @@
# Introduction

Every fourth year is a leap year (with some exceptions), but let's focus initially on the primary condition.

In the Leap problem, we need to determine if a year is evenly divisible by a number,
which means checking if the remainder of an integer division is zero.
This operation in computing is known as [modulo][modulo].


Clojure has two functions we can use: `mod` and `rem`.
The two functions differ in how they work with negative numbers, but since, in this exercise,
all the numbers are non-negative, both will work.

## General Solution

To check if a year is divisible by `n` in Clojure, we can do `(zero? (mod year n))`. To make the intent clearer, let's define a private function.

```clojure
(defn- divides? [number divisor]
(zero? (mod number divisor)))
```

Any approach to the problem will perform this check three times to see if a year is equally divisible by 4, 100 and 400.
What will differ between approaches is which Clojure features we will use to combine the checks.

## Approach: Boolean Operations

The full rules are as follows:
A year is a leap year if
* it is divisible by 4
* but not divisible by 100
* unless it is divisible by 400


We can use `and`, `or` and `not` functions to combine the checks, for example, like so:

```clojure
(or (divides? year 400)
(and (not (divides? year 100))
(divides? year 4))))
```

Explore the details in the [boolean operations approach][boolean-approach].

## Approach: Conditional Branching

Instead of combining the logical expressions, we can use conditional branching with functions like `if` or `cond` to perform the necessary checks and return true or false.

```clojure
(if (divides? year 100)
(divides? year 400)
(divides? year 4)))
```

In the [conditional branching approach][conditional-approach], we discuss the options comparing `if` and `cond` functions.

[modulo]: https://en.wikipedia.org/wiki/Modulo
[boolean-approach]: https://exercism.org/tracks/clojure/exercises/leap/approaches/boolean
[conditional-approach]: https://exercism.org/tracks/clojure/exercises/leap/approaches/conditional

0 comments on commit 3ebe221

Please sign in to comment.