diff options
| -rwxr-xr-x | challenge-251/nelo-tovar/bash/ch-1.sh | 43 | ||||
| -rw-r--r-- | challenge-251/nelo-tovar/perl/ch-1.pl | 41 | ||||
| -rw-r--r-- | challenge-251/nelo-tovar/perl/ch-2.pl | 63 |
3 files changed, 147 insertions, 0 deletions
diff --git a/challenge-251/nelo-tovar/bash/ch-1.sh b/challenge-251/nelo-tovar/bash/ch-1.sh new file mode 100755 index 0000000000..a4ede94594 --- /dev/null +++ b/challenge-251/nelo-tovar/bash/ch-1.sh @@ -0,0 +1,43 @@ +#!/usr/bin/env bash +# +# The Weekly Challenge 251 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-251/ +# +# Task 1 : Concatenation Value + +function concatenation_value() { + local ints=("$@") + local length=${#ints[@]} + local value='' + + while [[ $length > 0 ]]; do + first=${ints[0]} + if [[ $length -gt 1 ]]; then + last=${ints[-1]} + else + last='' + fi + + value=$(( $value + ${first}${last} )) + + unset ints[-1] + ints=("${ints[@]:1}") + length=${#ints[@]} + done + + echo $value +} + +example1='6 12 25 1' +example2='10 7 31 5 2 2' +example3='1 2 10' + +for e in "$example1" "$example2" "$example3"; do + array=($e) + cv=$(concatenation_value "${array[@]}") + echo "Input : ints = (${array[@]})" + echo "Output : $cv" + echo "" +done + diff --git a/challenge-251/nelo-tovar/perl/ch-1.pl b/challenge-251/nelo-tovar/perl/ch-1.pl new file mode 100644 index 0000000000..5c9a1705e5 --- /dev/null +++ b/challenge-251/nelo-tovar/perl/ch-1.pl @@ -0,0 +1,41 @@ +#!/usr/bin/env perl + +# The Weekly Challenge 251 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-251/ +# +# Task 1 - Concatenation Value +# + +use strict; +use warnings; +use v5.28; +use Data::Dump qw(dump); + +my @examples = ( + [ 6, 12, 25, 1 ], + [ 10, 7, 31, 5, 2, 2 ], + [ 1, 2, 10 ], +); + +sub concatenation_value { + my @ints = shift->@*; + my $value = 0; + + while ( scalar @ints > 0 ) { + my $first = shift @ints; + my $last = scalar @ints ? pop @ints : ''; + + $value += "${first}${last}"; + } + + return $value; +} + +for my $elements (@examples) { + my $cv = concatenation_value $elements; + + say 'Input : @ints = ', dump(@$elements); + say 'Output : ', $cv; + say ' '; +} diff --git a/challenge-251/nelo-tovar/perl/ch-2.pl b/challenge-251/nelo-tovar/perl/ch-2.pl new file mode 100644 index 0000000000..7de7720560 --- /dev/null +++ b/challenge-251/nelo-tovar/perl/ch-2.pl @@ -0,0 +1,63 @@ +#!/usr/bin/env perl + +# The Weekly Challenge 251 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-251/ +# +# Task 2 - Lucky Numbers +# + +use strict; +use warnings; +use v5.28; +use List::Util qw (min max); +use Data::Dump qw(dump); + +my @examples = ( + [ + [ 3, 7, 8], + [ 9, 11, 13], + [15, 16, 17] + ], + [ + [ 1, 10, 4, 2], + [ 9, 3, 8, 7], + [15, 16, 17, 12] + ], + [ + [7 ,8], + [1 ,2] + ], +); + +sub lucky_numbers { + my $matrix = shift; + my $len_rows = scalar @$matrix; + my $len_cols = scalar @{$matrix->[0]}; + my $value; + + for (my $i = 0; $i < $len_rows; $i++) { + my $min = min @{$matrix->[$i]}; + + for (my $j = 0; $j < $len_cols; $j++) { + my @cols = map {$_->[$j]} @$matrix; + my $max = max @cols; + + return $matrix->[$i][$j] if ($min eq $max); + } + } + + return -1; +} + +for my $elements (@examples) { + my $ln = lucky_numbers $elements; + + say 'Input : $matrix = ['; + foreach my $x (@$elements) { + printf("%15s%s,\n", ' ', dump($x)) + }; + printf("%14s\n", ']'); + say 'Output : ', $ln; + say ' '; +} |
