commit dd9f32c3bca70a52f354dedde64b28ea6bc86745 Author: Jim Infield Date: Fri Jul 30 15:29:52 2021 -0500 initial commit diff --git a/.exercism/metadata.json b/.exercism/metadata.json new file mode 100644 index 0000000..1a4abd6 --- /dev/null +++ b/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"bash","exercise":"raindrops","id":"7f497d15e3ac43dd82a89e80571d8dd4","url":"https://exercism.io/my/solutions/7f497d15e3ac43dd82a89e80571d8dd4","handle":"jinfield","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..469404e --- /dev/null +++ b/README.md @@ -0,0 +1,62 @@ +# Raindrops + +Your task is to convert a number into a string that contains raindrop sounds corresponding to certain potential factors. A factor is a number that evenly divides into another number, leaving no remainder. The simplest way to test if a one number is a factor of another is to use the [modulo operation](https://en.wikipedia.org/wiki/Modulo_operation). + +The rules of `raindrops` are that if a given number: + +- has 3 as a factor, add 'Pling' to the result. +- has 5 as a factor, add 'Plang' to the result. +- has 7 as a factor, add 'Plong' to the result. +- _does not_ have any of 3, 5, or 7 as a factor, the result should be the digits of the number. + +## Examples + +- 28 has 7 as a factor, but not 3 or 5, so the result would be "Plong". +- 30 has both 3 and 5 as factors, but not 7, so the result would be "PlingPlang". +- 34 is not factored by 3, 5, or 7, so the result would be "34". + + +Run the tests with: + +```bash +bats raindrops_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: + +```bash +BATS_RUN_SKIPPED=true bats raindrops_test.sh +``` + +## Source + +A variation on FizzBuzz, a famous technical interview question that is intended to weed out potential candidates. That question is itself derived from Fizz Buzz, a popular children's game for teaching division. [https://en.wikipedia.org/wiki/Fizz_buzz](https://en.wikipedia.org/wiki/Fizz_buzz) + + +## External utilities +`Bash` is a language to write "scripts" -- programs that can call +external tools, such as +[`sed`](https://www.gnu.org/software/sed/), +[`awk`](https://www.gnu.org/software/gawk/), +[`date`](https://www.gnu.org/software/coreutils/manual/html_node/date-invocation.html) +and even programs written in other programming languages, +like [`Python`](https://www.python.org/). +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. diff --git a/raindrops.sh b/raindrops.sh new file mode 100644 index 0000000..031c878 --- /dev/null +++ b/raindrops.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env bash + +[[ -z $1 || ! $1 =~ ^[0-9]+$ ]] && echo "Usage: $0 " && exit 1 +[[ $(( $1%7 )) = 0 ]] && res+="Pling" +[[ $(( $1%5 )) = 0 ]] && res+="Plang" +[[ $(( $1%3 )) = 0 ]] && res+="Plong" +[[ -z $res ]] && res=$1 +echo $res diff --git a/raindrops_test.sh b/raindrops_test.sh new file mode 100644 index 0000000..182a4f9 --- /dev/null +++ b/raindrops_test.sh @@ -0,0 +1,129 @@ +#!/usr/bin/env bash + +# local version: 1.1.0.0 + +@test "the sound for 1 is 1" { + #[[ $BATS_RUN_SKIPPED == "true" ]] || skip + run bash raindrops.sh 1 + (( status == 0 )) + [[ $output == "1" ]] +} + +@test "the sound for 3 is Pling" { + [[ $BATS_RUN_SKIPPED == "true" ]] || skip + run bash raindrops.sh 3 + (( status == 0 )) + [[ $output == "Pling" ]] +} + +@test "the sound for 5 is Plang" { + [[ $BATS_RUN_SKIPPED == "true" ]] || skip + run bash raindrops.sh 5 + (( status == 0 )) + [[ $output == "Plang" ]] +} + +@test "the sound for 7 is Plong" { + [[ $BATS_RUN_SKIPPED == "true" ]] || skip + run bash raindrops.sh 7 + (( status == 0 )) + [[ $output == "Plong" ]] +} + +@test "the sound for 6 is Pling as it has a factor 3" { + [[ $BATS_RUN_SKIPPED == "true" ]] || skip + run bash raindrops.sh 6 + (( status == 0 )) + [[ $output == "Pling" ]] +} + +@test "2 to the power 3 does not make a raindrop sound as 3 is the exponent not the base" { + [[ $BATS_RUN_SKIPPED == "true" ]] || skip + run bash raindrops.sh 8 + (( status == 0 )) + [[ $output == "8" ]] +} + +@test "the sound for 9 is Pling as it has a factor 3" { + [[ $BATS_RUN_SKIPPED == "true" ]] || skip + run bash raindrops.sh 9 + (( status == 0 )) + [[ $output == "Pling" ]] +} + +@test "the sound for 10 is Plang as it has a factor 5" { + [[ $BATS_RUN_SKIPPED == "true" ]] || skip + run bash raindrops.sh 10 + (( status == 0 )) + [[ $output == "Plang" ]] +} + +@test "the sound for 14 is Plong as it has a factor of 7" { + [[ $BATS_RUN_SKIPPED == "true" ]] || skip + run bash raindrops.sh 14 + (( status == 0 )) + [[ $output == "Plong" ]] +} + +@test "the sound for 15 is PlingPlang as it has factors 3 and 5" { + [[ $BATS_RUN_SKIPPED == "true" ]] || skip + run bash raindrops.sh 15 + (( status == 0 )) + [[ $output == "PlingPlang" ]] +} + +@test "the sound for 21 is PlingPlong as it has factors 3 and 7" { + [[ $BATS_RUN_SKIPPED == "true" ]] || skip + run bash raindrops.sh 21 + (( status == 0 )) + [[ $output == "PlingPlong" ]] +} + +@test "the sound for 25 is Plang as it has a factor 5" { + [[ $BATS_RUN_SKIPPED == "true" ]] || skip + run bash raindrops.sh 25 + (( status == 0 )) + [[ $output == "Plang" ]] +} + +@test "the sound for 27 is Pling as it has a factor 3" { + [[ $BATS_RUN_SKIPPED == "true" ]] || skip + run bash raindrops.sh 27 + (( status == 0 )) + [[ $output == "Pling" ]] +} + +@test "the sound for 35 is PlangPlong as it has factors 5 and 7" { + [[ $BATS_RUN_SKIPPED == "true" ]] || skip + run bash raindrops.sh 35 + (( status == 0 )) + [[ $output == "PlangPlong" ]] +} + +@test "the sound for 49 is Plong as it has a factor 7" { + [[ $BATS_RUN_SKIPPED == "true" ]] || skip + run bash raindrops.sh 49 + (( status == 0 )) + [[ $output == "Plong" ]] +} + +@test "the sound for 52 is 52" { + [[ $BATS_RUN_SKIPPED == "true" ]] || skip + run bash raindrops.sh 52 + (( status == 0 )) + [[ $output == "52" ]] +} + +@test "the sound for 105 is PlingPlangPlong as it has factors 3, 5 and 7" { + [[ $BATS_RUN_SKIPPED == "true" ]] || skip + run bash raindrops.sh 105 + (( status == 0 )) + [[ $output == "PlingPlangPlong" ]] +} + +@test "the sound for 3125 is Plang as it has a factor 5" { + [[ $BATS_RUN_SKIPPED == "true" ]] || skip + run bash raindrops.sh 3125 + (( status == 0 )) + [[ $output == "Plang" ]] +}