diff options
| author | Abigail <abigail@abigail.be> | 2021-03-31 15:06:48 +0200 |
|---|---|---|
| committer | Abigail <abigail@abigail.be> | 2021-03-31 15:07:17 +0200 |
| commit | aa4f9e49e6aaa1ca25bcd2999c98b62f23ff5eaf (patch) | |
| tree | 46b70355172ff420804f9458eeb0f706c37fe9fa /challenge-106 | |
| parent | 39ebc15e335d36f52a0240e1cc9ed5e36f1a4fb2 (diff) | |
| download | perlweeklychallenge-club-aa4f9e49e6aaa1ca25bcd2999c98b62f23ff5eaf.tar.gz perlweeklychallenge-club-aa4f9e49e6aaa1ca25bcd2999c98b62f23ff5eaf.tar.bz2 perlweeklychallenge-club-aa4f9e49e6aaa1ca25bcd2999c98b62f23ff5eaf.zip | |
Bash solution for week 106, part 2
Diffstat (limited to 'challenge-106')
| -rw-r--r-- | challenge-106/abigail/README.md | 1 | ||||
| -rw-r--r-- | challenge-106/abigail/bash/ch-2.sh | 43 |
2 files changed, 44 insertions, 0 deletions
diff --git a/challenge-106/abigail/README.md b/challenge-106/abigail/README.md index 2f9f8e7880..6c1a682ffc 100644 --- a/challenge-106/abigail/README.md +++ b/challenge-106/abigail/README.md @@ -93,6 +93,7 @@ Wikipedia](https://en.wikipedia.org/wiki/Repeating_decimal). ### Solutions * [AWK](perl/ch-2.awk) +* [Bash](bash/ch-2.sh) * [Perl](perl/ch-2.pl) ### Blog diff --git a/challenge-106/abigail/bash/ch-2.sh b/challenge-106/abigail/bash/ch-2.sh new file mode 100644 index 0000000000..77f38b81c3 --- /dev/null +++ b/challenge-106/abigail/bash/ch-2.sh @@ -0,0 +1,43 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-2.sh < input-file +# + +set -f + +declare fraction + +function long_division () { + declare numerator=$1 + declare denominator=$2 + declare BASE=10 + fraction=$((numerator / denominator)). + declare position=${#fraction} + declare -a seen + + ((numerator %= denominator)) + + while ((!seen[numerator])) + do if ((numerator == 0)) + then return + fi + seen[$numerator]=$position + fraction=$fraction$((BASE * numerator / denominator)) + ((numerator = BASE * numerator % denominator)) + ((position ++)) + done + fraction=${fraction::${seen[$numerator]}}\(${fraction:${seen[$numerator]}}\) +} + + +while read numerator denominator +do long_division $numerator $denominator + echo $fraction +done + + |
