aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-261/nelo-tovar/bash/ch-1.sh48
-rwxr-xr-xchallenge-261/nelo-tovar/bash/ch-2.sh51
-rw-r--r--challenge-261/nelo-tovar/perl/ch-1.pl42
-rw-r--r--challenge-261/nelo-tovar/perl/ch-2.pl36
4 files changed, 177 insertions, 0 deletions
diff --git a/challenge-261/nelo-tovar/bash/ch-1.sh b/challenge-261/nelo-tovar/bash/ch-1.sh
new file mode 100755
index 0000000000..f4dc4c0dec
--- /dev/null
+++ b/challenge-261/nelo-tovar/bash/ch-1.sh
@@ -0,0 +1,48 @@
+#!/usr/bin/env bash
+#
+# The Weekly Challenge 261 - By Nelo Tovar
+#
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-261/
+#
+# Task 1 : Element Digit Sum
+
+function sum0() {
+ local number=$1
+ local sum=0
+
+ for (( i = 0; i < ${#number}; i++ )); do
+ digit=${number:$i:1}
+ ((sum+=$digit))
+ done
+
+ echo $sum
+}
+
+function element_digit_sum() {
+ local nums=("$@")
+ local length=${#nums[@]}
+ local sum=0
+
+ for (( i = 0; i < $length; i++ )); do
+ if [[ ${nums[$i]} -gt 9 ]]; then
+ d=$(sum0 ${nums[$i]})
+ ((sum+=${nums[$i]} - $d))
+ fi
+ done
+
+ echo ${sum#-}
+}
+
+example1='1 2 3 45'
+example2='1 12 3'
+example3='1 2 3 4'
+example4='236 416 336 350'
+
+for e in "$example1" "$example2" "$example3" "$example4"; do
+ array=($e)
+ eds=($(element_digit_sum "${array[@]}"))
+ echo "Input : ints = (${array[@]})"
+ echo "Output : $eds"
+ echo ""
+done
+
diff --git a/challenge-261/nelo-tovar/bash/ch-2.sh b/challenge-261/nelo-tovar/bash/ch-2.sh
new file mode 100755
index 0000000000..753cfd9c66
--- /dev/null
+++ b/challenge-261/nelo-tovar/bash/ch-2.sh
@@ -0,0 +1,51 @@
+#!/usr/bin/env bash
+#
+# The Weekly Challenge 261 - By Nelo Tovar
+#
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-261/
+#
+# Task 2 : Multiply by Two
+
+function in_array() {
+ local number=$1
+ shift
+ local array=("$@")
+ local length=${#array[@]}
+ local found=0
+
+ for i in ${array[@]}; do
+ if [ $number -eq $i ]; then
+ found=1
+ break
+ fi
+ done
+
+ echo $found
+}
+
+function multiply_by_two() {
+ local start=$1
+ shift
+ local nums=("$@")
+
+ while [[ $(in_array $start ${nums[@]}) -eq 1 ]]; do
+ ((start*=2))
+ done
+
+ echo $start
+}
+
+example0='5 3 6 1 12'
+example1='1 2 4 3'
+example2='5 6 7'
+example_start=(3 1 2)
+
+for e in 0 1 2; do
+ var="example$e"
+ array=(${!var})
+ start=${example_start[$e]}
+ mbt=$(multiply_by_two $start "${array[@]}")
+ echo "Input : ints = (${array[@]}) and start = $start"
+ echo -e "Output : $mbt\n"
+done
+
diff --git a/challenge-261/nelo-tovar/perl/ch-1.pl b/challenge-261/nelo-tovar/perl/ch-1.pl
new file mode 100644
index 0000000000..9b39bb73a0
--- /dev/null
+++ b/challenge-261/nelo-tovar/perl/ch-1.pl
@@ -0,0 +1,42 @@
+#!/usr/bin/env perl
+
+# The Weekly Challenge 261 - By Nelo Tovar
+#
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-261/
+#
+# Task 1 - Element Digit Sum
+#
+
+use strict;
+use warnings;
+use v5.28;
+use List::Util qw/sum0/;
+use Data::Dump qw(dump);
+
+my @examples = (
+ [ 1,2,3,45 ],
+ [ 1,12,3 ],
+ [ 1, 2, 3, 4 ],
+ [ 236, 416, 336, 350 ],
+);
+
+sub element_digit_sum {
+ my $nums = shift;
+ my $sum = 0;
+
+ foreach my $x (@$nums) {
+ next if $x <= 9;
+
+ $sum += $x - sum0(split //, $x);
+ }
+
+ return abs($sum);
+}
+
+for my $elements (@examples) {
+ my $eds = element_digit_sum $elements;
+
+ say 'Input : @ints = ', dump(@$elements);
+ say 'Output : ', $eds;
+ say ' ';
+}
diff --git a/challenge-261/nelo-tovar/perl/ch-2.pl b/challenge-261/nelo-tovar/perl/ch-2.pl
new file mode 100644
index 0000000000..1bc21bc7d1
--- /dev/null
+++ b/challenge-261/nelo-tovar/perl/ch-2.pl
@@ -0,0 +1,36 @@
+#!/usr/bin/env perl
+
+# The Weekly Challenge 261 - By Nelo Tovar
+#
+# https://theweeklychallenge.org/blog/perl-weekly-challenge-261/
+#
+# Task 2 - Multiply by Two
+#
+
+use strict;
+use warnings;
+use v5.28;
+use Data::Dump qw(dump);
+
+my @examples = (
+ {start => 3, ints => [5, 3, 6, 1, 12]},
+ {start => 1, ints => [1, 2, 4, 3]},
+ {start => 2, ints => [5, 6, 7]},
+);
+
+sub multiply_by_two {
+ my $nums = shift;
+ my $start = shift;
+
+ $start *= 2 while (grep {$_ eq $start} @$nums);
+
+ return $start;
+}
+
+for my $elements (@examples) {
+ my $mbt = multiply_by_two($elements->{ints}, $elements->{start});
+
+ say 'Input : @ints = ', dump($elements->{ints}), ' and $start = ', $elements->{start};
+ say 'Output : ', $mbt;
+ say ' ';
+}