diff options
| -rw-r--r-- | challenge-112/abigail/README.md | 2 | ||||
| -rw-r--r-- | challenge-112/abigail/bash/ch-1.sh | 41 | ||||
| -rw-r--r-- | challenge-112/abigail/bash/ch-2.sh | 29 |
3 files changed, 72 insertions, 0 deletions
diff --git a/challenge-112/abigail/README.md b/challenge-112/abigail/README.md index dbd9121743..574d51411f 100644 --- a/challenge-112/abigail/README.md +++ b/challenge-112/abigail/README.md @@ -34,6 +34,7 @@ Output: "/a" ### Solutions * [AWK](awk/ch-1.awk) +* [Bash](bash/ch-1.sh) * [Perl](perl/ch-1.pl) ### Blog @@ -51,6 +52,7 @@ This is just finding the `$n + 1` Fibonacci number. ### Solutions * [AWK](awk/ch-2.awk) +* [Bash](bash/ch-2.sh) * [Perl](perl/ch-2.pl) ### Blog diff --git a/challenge-112/abigail/bash/ch-1.sh b/challenge-112/abigail/bash/ch-1.sh new file mode 100644 index 0000000000..6f41950bc5 --- /dev/null +++ b/challenge-112/abigail/bash/ch-1.sh @@ -0,0 +1,41 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-1.sh < input-file +# + +set -f + +IFS="/" + +while read -a i_parts +do declare -a o_parts + j=0 + for ((i = 0; i < ${#i_parts[@]}; i ++)) + do if [ "X${i_parts[$i]}" == "X" ] # Skip empty parts + then continue + fi + if [ "X${i_parts[$i]}" == "X." ] # Skip current directory + then continue + fi + if [ "X${i_parts[$i]}" == "X.." ] # Back up to parent directory + then if ((j > 0)) + then ((j --)) + fi + continue + fi + o_parts[$j]=${i_parts[$i]} # Copy part + ((j ++)) + done + if ((j == 0)) + then echo "/" # Root directory + else for ((k = 0; k < j; k ++)) # Canonical path + do printf "/%s" ${o_parts[$k]} + done + echo + fi +done diff --git a/challenge-112/abigail/bash/ch-2.sh b/challenge-112/abigail/bash/ch-2.sh new file mode 100644 index 0000000000..17f2d49b9b --- /dev/null +++ b/challenge-112/abigail/bash/ch-2.sh @@ -0,0 +1,29 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-2.sh < input-file +# + +declare -A cache +cache[0]=1 +cache[1]=1 + +function fib () { + local n=$1 + if [[ -z ${cache[$n]} ]] + then fib $((n - 1)) + cache[$n]=$result + fib $((n - 2)) + cache[$n]=$((cache[$n] + result)) + fi + result=${cache[$n]} +} + +while read n +do fib $n + echo $result +done |
