ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# evanrr's solution

## to Leap in the Bash Track

Published at Apr 22 2021 · 0 comments
Instructions
Test suite
Solution

Given a year, report if it is a leap year.

The tricky thing here is that a leap year in the Gregorian calendar occurs:

``````on every year that is evenly divisible by 4
except every year that is evenly divisible by 100
unless the year is also evenly divisible by 400
``````

For example, 1997 is not a leap year, but 1996 is. 1900 is not a leap year, but 2000 is.

## Notes

Though our exercise adopts some very simple rules, there is more to learn!

For a delightful, four minute explanation of the whole leap year phenomenon, go watch this youtube video.

Run the tests with:

``````bats leap_test.sh
``````

After the first test(s) pass, continue by commenting out or removing the `[[ \$BATS_RUN_SKIPPED == true ]] || skip` annotations prepending other tests.

To run all tests, including the ones with `skip` annotations, run:

``````BATS_RUN_SKIPPED=true bats leap_test.sh
``````

## Source

JavaRanch Cattle Drive, exercise 3 http://www.javaranch.com/leap.jsp

## External utilities

`Bash` is a language to write "scripts" -- programs that can call external tools, such as `sed`, `awk`, `date` and even programs written in other programming languages, like `Python`. This track does not restrict the usage of these utilities, and as long as your solution is portable between systems and does not require installation of third party applications, feel free to use them to solve the exercise.

For an extra challenge, if you would like to have a better understanding of the language, try to re-implement the solution in pure `Bash`, without using any external tools. Note that there are some types of problems that bash cannot solve, such as performing floating point arithmetic and manipulating dates: for those, you must call out to an external tool.

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### leap_test.sh

``````#!/usr/bin/env bash

# local version: 1.6.0.1
# bash-specific test: Input validation

@test 'year not divisible by 4: common year' {
#[[ \$BATS_RUN_SKIPPED == "true" ]] || skip
run bash leap.sh 2015

(( status == 0 ))
[[ \$output == "false" ]]
}

@test 'year divisible by 2, not divisible by 4 in common year' {
[[ \$BATS_RUN_SKIPPED == "true" ]] || skip
run bash leap.sh 1970

(( status == 0 ))
[[ \$output == "false" ]]
}

@test 'year divisible by 4, not divisible by 100: leap year' {
[[ \$BATS_RUN_SKIPPED == "true" ]] || skip
run bash leap.sh 1996

(( status == 0 ))
[[ \$output == "true" ]]
}

@test 'year divisible by 4 and 5 is still a leap year' {
[[ \$BATS_RUN_SKIPPED == "true" ]] || skip
run bash leap.sh 1960

(( status == 0 ))
[[ \$output == "true" ]]
}

@test 'year divisible by 100, not divisible by 400: common year' {
[[ \$BATS_RUN_SKIPPED == "true" ]] || skip
run bash leap.sh 2100

(( status == 0 ))
[[ \$output == "false" ]]
}

@test 'year divisible by 100 but not by 3 is still not a leap year' {
[[ \$BATS_RUN_SKIPPED == "true" ]] || skip
run bash leap.sh 1900

(( status == 0 ))
[[ \$output == "false" ]]
}

@test 'year divisible by 400: leap year' {
[[ \$BATS_RUN_SKIPPED == "true" ]] || skip
run bash leap.sh 2000

(( status == 0 ))
[[ \$output == "true" ]]
}

@test 'year divisible by 400 but not by 125 is still a leap year' {
[[ \$BATS_RUN_SKIPPED == "true" ]] || skip
run bash leap.sh 2400

(( status == 0 ))
[[ \$output == "true" ]]
}

@test 'year divisible by 200, not divisible by 400 in common year' {
[[ \$BATS_RUN_SKIPPED == "true" ]] || skip
run bash leap.sh 1800

(( status == 0 ))
[[ \$output == "false" ]]
}

@test 'No input should return an error' {
[[ \$BATS_RUN_SKIPPED == "true" ]] || skip
run bash leap.sh

(( status == 1 ))
[[ \$output == "Usage: leap.sh <year>" ]]
}

@test 'Too many arguments should return an error' {
[[ \$BATS_RUN_SKIPPED == "true" ]] || skip
run bash leap.sh 2016 4562 4566

(( status == 1 ))
[[ \$output == "Usage: leap.sh <year>" ]]
}

@test 'Float number input should return an error' {
[[ \$BATS_RUN_SKIPPED == "true" ]] || skip
run bash leap.sh 2016.54

(( status == 1 ))
[[ \$output == "Usage: leap.sh <year>" ]]
}

@test 'Alpha input should return an error' {
[[ \$BATS_RUN_SKIPPED == "true" ]] || skip
run bash leap.sh 'abcd'

(( status == 1 ))
[[ \$output == "Usage: leap.sh <year>" ]]
}``````
``````#!/usr/bin/env bash

set -e

usage() {
echo "Usage: leap.sh <year>"
exit "\$1"
}

checkArgs() {
[[ "\$#" == 1 ]] && [[ "\$1" =~ ^[0-9]+\$ ]]
}

divisibleBy() {
[[ \$(( "\$1" % "\$2" )) == 0 ]]
}

checkArgs "\$@" || usage 1 \
&& divisibleBy "\$1" 4 \
&& ! divisibleBy "\$1" 100 \
|| divisibleBy "\$1" 400 \
&& echo "true" || echo "false"``````