exercism

Exercism solutions.
Log | Files | Refs

commit bfe7491f4c5326bc6bba9380e0b424b2584284c0
parent 7f063b445c365935735e8851204bdd86616bbc69
Author: dwrz <dwrz@dwrz.net>
Date:   Thu,  4 Jul 2019 23:14:45 +0000

Add bash/raindrops

Diffstat:
Abash/raindrops/README.md | 64++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Abash/raindrops/raindrops.sh | 25+++++++++++++++++++++++++
Abash/raindrops/raindrops_test.sh | 109+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 198 insertions(+), 0 deletions(-)

diff --git a/bash/raindrops/README.md b/bash/raindrops/README.md @@ -0,0 +1,64 @@ +# Raindrops + +Convert a number to a string, the contents of which depend on the number's factors. + +- If the number has 3 as a factor, output 'Pling'. +- If the number has 5 as a factor, output 'Plang'. +- If the number has 7 as a factor, output 'Plong'. +- If the number does not have 3, 5, or 7 as a factor, + just pass the number's digits straight through. + +## Examples + +- 28's factors are 1, 2, 4, **7**, 14, 28. + - In raindrop-speak, this would be a simple "Plong". +- 30's factors are 1, 2, **3**, **5**, 6, 10, 15, 30. + - In raindrop-speak, this would be a "PlingPlang". +- 34 has four factors: 1, 2, 17, and 34. + - In raindrop-speak, this 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 a famous interview question intended to weed out potential candidates. [http://jumpstartlab.com](http://jumpstartlab.com) + + +## 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/bash/raindrops/raindrops.sh b/bash/raindrops/raindrops.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env bash + +main() { + local name="" + + if [ $(($1 % 3)) -eq 0 ]; then + name="Pling" + fi + + if [ $(($1 % 5)) -eq 0 ]; then + name="${name}Plang" + fi + + if [ $(($1 % 7)) -eq 0 ]; then + name="${name}Plong" + fi + + if [ "$name" = "" ]; then + name="$1" + fi + + echo $name +} + +main "$@" diff --git a/bash/raindrops/raindrops_test.sh b/bash/raindrops/raindrops_test.sh @@ -0,0 +1,109 @@ +#!/usr/bin/env bash + +@test "the sound for 1 is 1" { + run bash raindrops.sh 1 + [ "$status" -eq 0 ] + [ "$output" == "1" ] +} + +@test "the sound for 3 is Pling" { + run bash raindrops.sh 3 + [ "$status" -eq 0 ] + [ "$output" == "Pling" ] +} + +@test "the sound for 5 is Plang" { + run bash raindrops.sh 5 + [ "$status" -eq 0 ] + [ "$output" == "Plang" ] +} + +@test "the sound for 7 is Plong" { + run bash raindrops.sh 7 + [ "$status" -eq 0 ] + [ "$output" == "Plong" ] +} + +@test "the sound for 6 is Pling as it has a factor 3" { + run bash raindrops.sh 6 + [ "$status" -eq 0 ] + [ "$output" == "Pling" ] +} + +@test "2 to the power 3 does not make a raindrop sound as 3 is the exponent not the base" { + run bash raindrops.sh 8 + [ "$status" -eq 0 ] + [ "$output" == "8" ] +} + +@test "the sound for 9 is Pling as it has a factor 3" { + run bash raindrops.sh 9 + [ "$status" -eq 0 ] + [ "$output" == "Pling" ] +} + +@test "the sound for 10 is Plang as it has a factor 5" { + run bash raindrops.sh 10 + [ "$status" -eq 0 ] + [ "$output" == "Plang" ] +} + +@test "the sound for 14 is Plong as it has a factor of 7" { + run bash raindrops.sh 14 + [ "$status" -eq 0 ] + [ "$output" == "Plong" ] +} + +@test "the sound for 15 is PlingPlang as it has factors 3 and 5" { + run bash raindrops.sh 15 + [ "$status" -eq 0 ] + [ "$output" == "PlingPlang" ] +} + +@test "the sound for 21 is PlingPlong as it has factors 3 and 7" { + run bash raindrops.sh 21 + [ "$status" -eq 0 ] + [ "$output" == "PlingPlong" ] +} + +@test "the sound for 25 is Plang as it has a factor 5" { + run bash raindrops.sh 25 + [ "$status" -eq 0 ] + [ "$output" == "Plang" ] +} + +@test "the sound for 27 is Pling as it has a factor 3" { + run bash raindrops.sh 27 + [ "$status" -eq 0 ] + [ "$output" == "Pling" ] +} + +@test "the sound for 35 is PlangPlong as it has factors 5 and 7" { + run bash raindrops.sh 35 + [ "$status" -eq 0 ] + [ "$output" == "PlangPlong" ] +} + +@test "the sound for 49 is Plong as it has a factor 7" { + run bash raindrops.sh 49 + [ "$status" -eq 0 ] + [ "$output" == "Plong" ] +} + +@test "the sound for 52 is 52" { + run bash raindrops.sh 52 + [ "$status" -eq 0 ] + [ "$output" == "52" ] +} + +@test "the sound for 105 is PlingPlangPlong as it has factors 3, 5 and 7" { + run bash raindrops.sh 105 + [ "$status" -eq 0 ] + [ "$output" == "PlingPlangPlong" ] +} + +@test "the sound for 3125 is Plang as it has a factor 5" { + run bash raindrops.sh 3125 + [ "$status" -eq 0 ] + [ "$output" == "Plang" ] +}