aboutsummaryrefslogtreecommitdiff
path: root/challenge-112
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-05-10 16:05:45 +0200
committerAbigail <abigail@abigail.be>2021-05-10 16:05:45 +0200
commit09b7571ec15816cfedda4642d1c3fc8e3e9b0f74 (patch)
tree9bb4c1034c860503c39b48d6f3aed2121e40cdff /challenge-112
parent32c098711bd7b38c7ec4bbaeccac07af560ffbaa (diff)
downloadperlweeklychallenge-club-09b7571ec15816cfedda4642d1c3fc8e3e9b0f74.tar.gz
perlweeklychallenge-club-09b7571ec15816cfedda4642d1c3fc8e3e9b0f74.tar.bz2
perlweeklychallenge-club-09b7571ec15816cfedda4642d1c3fc8e3e9b0f74.zip
Bash solutions for week 112
Diffstat (limited to 'challenge-112')
-rw-r--r--challenge-112/abigail/README.md2
-rw-r--r--challenge-112/abigail/bash/ch-1.sh41
-rw-r--r--challenge-112/abigail/bash/ch-2.sh29
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