diff options
| author | Abigail <abigail@abigail.freedom.nl> | 2021-12-27 18:20:34 +0100 |
|---|---|---|
| committer | Abigail <abigail@abigail.freedom.nl> | 2021-12-29 20:19:41 +0100 |
| commit | a7cfe19e7988033b8b37d4effedcf01ee203ff0e (patch) | |
| tree | ab78dd124aef81b9f58ec35ca24e9b768e1d3512 /challenge-145/abigail/bash | |
| parent | 767f501835ea07823607293b532b6a0520fafc3b (diff) | |
| download | perlweeklychallenge-club-a7cfe19e7988033b8b37d4effedcf01ee203ff0e.tar.gz perlweeklychallenge-club-a7cfe19e7988033b8b37d4effedcf01ee203ff0e.tar.bz2 perlweeklychallenge-club-a7cfe19e7988033b8b37d4effedcf01ee203ff0e.zip | |
Week 145
Diffstat (limited to 'challenge-145/abigail/bash')
| -rw-r--r-- | challenge-145/abigail/bash/ch-1.sh | 18 | ||||
| -rw-r--r-- | challenge-145/abigail/bash/ch-2.sh | 53 |
2 files changed, 71 insertions, 0 deletions
diff --git a/challenge-145/abigail/bash/ch-1.sh b/challenge-145/abigail/bash/ch-1.sh new file mode 100644 index 0000000000..f9495e1871 --- /dev/null +++ b/challenge-145/abigail/bash/ch-1.sh @@ -0,0 +1,18 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-1.sh < input-file +# + +set -f + +read -a a +read -a b +for ((i = 0; i < ${#a[@]}; i ++)) +do ((sum += a[i] * b[i])) +done +echo $sum diff --git a/challenge-145/abigail/bash/ch-2.sh b/challenge-145/abigail/bash/ch-2.sh new file mode 100644 index 0000000000..97bf3f6bdf --- /dev/null +++ b/challenge-145/abigail/bash/ch-2.sh @@ -0,0 +1,53 @@ +#!/bin/sh + +# +# See ../README.md +# + +# +# Run as: bash ch-2.sh < input-file +# + +set -f + +declare -A palindromes + +function is_palindrome () { + local string=$1 + if ((${#string} < 2)) + then + is_palindrome=1 + return + fi + local f=${string:0:1} + local s=${string: -1:1} + if [ $f != $s ] + then is_palindrome=0 + return + fi + is_palindrome ${string:1:$((${#string} - 2))} +} + + + +while read line +do palindromes=() + for ((i = 0; i < ${#line}; i ++)) + do for ((j = i; j < ${#line}; j ++)) + do sub=${line:$i:$((j-i+1))} + is_palindrome $sub + if (($is_palindrome == 1)) + then palindromes[$sub]=1 + fi + done + done + first=1 + for p in "${!palindromes[@]}" + do if ((first == 1)) + then ((first ++)) + else printf " " + fi + printf %s $p + done + echo +done |
