diff --git a/exercises/practice/leap/.approaches/and-or/content.md b/exercises/practice/leap/.approaches/and-or/content.md deleted file mode 100644 index 8040f2044..000000000 --- a/exercises/practice/leap/.approaches/and-or/content.md +++ /dev/null @@ -1 +0,0 @@ -# and or \ No newline at end of file diff --git a/exercises/practice/leap/.approaches/and-or/snippet.txt b/exercises/practice/leap/.approaches/and-or/snippet.txt deleted file mode 100644 index e69de29bb..000000000 diff --git a/exercises/practice/leap/.approaches/boolean/content.md b/exercises/practice/leap/.approaches/boolean/content.md new file mode 100644 index 000000000..56ab51297 --- /dev/null +++ b/exercises/practice/leap/.approaches/boolean/content.md @@ -0,0 +1 @@ +# Boolean Operations Approach \ No newline at end of file diff --git a/exercises/practice/leap/.approaches/boolean/snippet.txt b/exercises/practice/leap/.approaches/boolean/snippet.txt new file mode 100644 index 000000000..12a319ebf --- /dev/null +++ b/exercises/practice/leap/.approaches/boolean/snippet.txt @@ -0,0 +1,4 @@ +(defn leap-year? [year] + (or (divides? year 400) + (and (not (divides? year 100)) + (divides? year 4)))) \ No newline at end of file diff --git a/exercises/practice/leap/.approaches/cond/content.md b/exercises/practice/leap/.approaches/cond/content.md deleted file mode 100644 index af6819e37..000000000 --- a/exercises/practice/leap/.approaches/cond/content.md +++ /dev/null @@ -1 +0,0 @@ -# cond \ No newline at end of file diff --git a/exercises/practice/leap/.approaches/cond/snippet.txt b/exercises/practice/leap/.approaches/cond/snippet.txt deleted file mode 100644 index e69de29bb..000000000 diff --git a/exercises/practice/leap/.approaches/conditional/content.md b/exercises/practice/leap/.approaches/conditional/content.md new file mode 100644 index 000000000..2e53474cf --- /dev/null +++ b/exercises/practice/leap/.approaches/conditional/content.md @@ -0,0 +1 @@ +# Conditional Branching Approach \ No newline at end of file diff --git a/exercises/practice/leap/.approaches/conditional/snippet.txt b/exercises/practice/leap/.approaches/conditional/snippet.txt new file mode 100644 index 000000000..450f496ab --- /dev/null +++ b/exercises/practice/leap/.approaches/conditional/snippet.txt @@ -0,0 +1,4 @@ +(defn leap-year? [year] + (if (divides? year 100) + (divides? year 400) + (divides? year 4))) \ No newline at end of file diff --git a/exercises/practice/leap/.approaches/config.json b/exercises/practice/leap/.approaches/config.json index 9348ade8b..9c6c19e92 100644 --- a/exercises/practice/leap/.approaches/config.json +++ b/exercises/practice/leap/.approaches/config.json @@ -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" ] diff --git a/exercises/practice/leap/.approaches/if/content.md b/exercises/practice/leap/.approaches/if/content.md deleted file mode 100644 index 628c54b08..000000000 --- a/exercises/practice/leap/.approaches/if/content.md +++ /dev/null @@ -1 +0,0 @@ -# if \ No newline at end of file diff --git a/exercises/practice/leap/.approaches/if/snippet.txt b/exercises/practice/leap/.approaches/if/snippet.txt deleted file mode 100644 index e69de29bb..000000000 diff --git a/exercises/practice/leap/.approaches/introduction.md b/exercises/practice/leap/.approaches/introduction.md index 3d07efe55..134c56003 100644 --- a/exercises/practice/leap/.approaches/introduction.md +++ b/exercises/practice/leap/.approaches/introduction.md @@ -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 \ No newline at end of file