diff options
Diffstat (limited to 'challenge-252/nelo-tovar/bash')
| -rwxr-xr-x | challenge-252/nelo-tovar/bash/ch-1.sh | 33 | ||||
| -rwxr-xr-x | challenge-252/nelo-tovar/bash/ch-2.sh | 33 |
2 files changed, 66 insertions, 0 deletions
diff --git a/challenge-252/nelo-tovar/bash/ch-1.sh b/challenge-252/nelo-tovar/bash/ch-1.sh new file mode 100755 index 0000000000..df2ab7a9d1 --- /dev/null +++ b/challenge-252/nelo-tovar/bash/ch-1.sh @@ -0,0 +1,33 @@ +#!/usr/bin/env bash +# +# The Weekly Challenge 252 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-252/ +# +# Task 1 : Special Numbers + +function special_numbers() { + local ints=("$@") + local n=${#ints[@]} + local sum=0 + + for x in ${ints[@]}; do + if [[ $(($n % $x)) -eq 0 ]]; then + ((sum+=$(($x ** 2)))) + fi + done + + echo $sum +} + +example1='1 2 3 4' +example2='2 7 1 19 18 3' + +for e in "$example1" "$example2" ; do + array=($e) + sn=$(special_numbers "${array[@]}") + echo "Input : @ints = (${array[@]})" + echo "Output : $sn" + echo "" +done + diff --git a/challenge-252/nelo-tovar/bash/ch-2.sh b/challenge-252/nelo-tovar/bash/ch-2.sh new file mode 100755 index 0000000000..27749455b8 --- /dev/null +++ b/challenge-252/nelo-tovar/bash/ch-2.sh @@ -0,0 +1,33 @@ +# +# The Weekly Challenge 252 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-252/ +# +# Task 2 : Unique Sum Zero + +function unique_sum_zero() { + local n=$1 + local numbers=() + + if [[ $(($n % 2)) -ne 0 || $n -eq 1 ]]; then + numbers=(0) + fi + + for (( k = 1; k <= $(($n / 2)); k++ )); do + i=$(shuf -i 1-10 -n 1) + numbers+=($i -$i) + done + + echo ${numbers[@]} +} + +example1='5 3 1' + +for e in $example1; do + array=($e) + usz=$(unique_sum_zero $e) + echo "Input : $s = $e" + echo "Output :" $(printf " %s, " ${usz[@]}) + echo " " +done + |
