diff options
| author | ntovar <tovar.nelo@gmail.com> | 2024-01-04 15:30:57 -0500 |
|---|---|---|
| committer | ntovar <tovar.nelo@gmail.com> | 2024-01-04 15:30:57 -0500 |
| commit | b17d828cb7e32ec35ef0d5a77c4b129285c2bb85 (patch) | |
| tree | 71f63ffec1d58617923819d7de99ab3abb08fe9d | |
| parent | b82d014ea517fb191361d5cbcd01a5fdb5de4d80 (diff) | |
| download | perlweeklychallenge-club-b17d828cb7e32ec35ef0d5a77c4b129285c2bb85.tar.gz perlweeklychallenge-club-b17d828cb7e32ec35ef0d5a77c4b129285c2bb85.tar.bz2 perlweeklychallenge-club-b17d828cb7e32ec35ef0d5a77c4b129285c2bb85.zip | |
Adding Perl and Bash solutions. By Nelo Tovar
| -rwxr-xr-x | challenge-250/nelo-tovar/bash/ch-1.sh | 34 | ||||
| -rwxr-xr-x | challenge-250/nelo-tovar/bash/ch-2.sh | 37 | ||||
| -rw-r--r-- | challenge-250/nelo-tovar/perl/ch-1.pl | 38 | ||||
| -rw-r--r-- | challenge-250/nelo-tovar/perl/ch-2.pl | 39 |
4 files changed, 148 insertions, 0 deletions
diff --git a/challenge-250/nelo-tovar/bash/ch-1.sh b/challenge-250/nelo-tovar/bash/ch-1.sh new file mode 100755 index 0000000000..4ebc478a47 --- /dev/null +++ b/challenge-250/nelo-tovar/bash/ch-1.sh @@ -0,0 +1,34 @@ +#!/usr/bin/env bash +# +# The Weekly Challenge 250 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-250/ +# +# Task 1 : Smallest Index + +function smallest_index() { + local ints=("$@") + local length=${#ints[@]} + + for (( i = 0; i < $length; i++ )); do + if [[ $(($i % 10)) -eq ${ints[$i]} ]]; then + echo $i + return 0 + fi + done + + echo -1 +} + +example1='0 1 2' +example2='4 3 2 1' +example3='1 2 3 4 5 6 7 8 9 0' + +for e in "$example1" "$example2" "$example3"; do + array=($e) + si=$(smallest_index "${array[@]}") + echo "Input : @ints = (${array[@]})" + echo "Output : $si" + echo "" +done + diff --git a/challenge-250/nelo-tovar/bash/ch-2.sh b/challenge-250/nelo-tovar/bash/ch-2.sh new file mode 100755 index 0000000000..ccd1ec2f4c --- /dev/null +++ b/challenge-250/nelo-tovar/bash/ch-2.sh @@ -0,0 +1,37 @@ +#!/usr/bin/env bash +# +# The Weekly Challenge 250 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-250/ +# +# Task 2 : Alphanumeric String Value + +function alphanumeric_string_value() { + local alphanumstr=("$@") + local max=0 + + for x in ${alphanumstr[@]}; do + if [[ $x =~ ^[0-9]+$ ]]; then + value=$((x + 0 )) + else + value=${#x} + fi + + if [[ $value -gt $max ]]; then + max=$value + fi + done + + echo $max +} + +example1='perl 2 000 python r4ku' +example2='001 1 000 0001' + +for e in "$example1" "$example2"; do + array=($e) + asv=$(alphanumeric_string_value "${array[@]}") + echo "Input : alphanumstr = (${array[@]})" + echo -e "Output : $asv\n" +done + diff --git a/challenge-250/nelo-tovar/perl/ch-1.pl b/challenge-250/nelo-tovar/perl/ch-1.pl new file mode 100644 index 0000000000..44c454ac9f --- /dev/null +++ b/challenge-250/nelo-tovar/perl/ch-1.pl @@ -0,0 +1,38 @@ +#!/usr/bin/env perl + +# The Weekly Challenge 250 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-250/ +# +# Task 1 - Smallest Index +# + +use strict; +use warnings; +use v5.28; +use Data::Dump qw(dump); + +my @examples = ( + [ 0, 1, 2 ], + [ 4, 3, 2, 1 ], + [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 ], +); + +sub smallest_index { + my $ints = shift; + my $length = scalar @$ints; + + for (my $i = 0; $i < $length; $i++) { + return $i if (($i % 10) == $ints->[$i] ); + } + + return -1; +} + +for my $elements (@examples) { + my $si = smallest_index $elements; + + say 'Input : @ints = ', dump(@$elements); + say 'Output : ', $si; + say ' '; +} diff --git a/challenge-250/nelo-tovar/perl/ch-2.pl b/challenge-250/nelo-tovar/perl/ch-2.pl new file mode 100644 index 0000000000..f587afc974 --- /dev/null +++ b/challenge-250/nelo-tovar/perl/ch-2.pl @@ -0,0 +1,39 @@ +#!/usr/bin/env perl + +# The Weekly Challenge 250 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-250/ +# +# Task 2 - Alphanumeric String Value +# + +use strict; +use warnings; +use v5.28; +use Data::Dump qw(dump); + +my @examples = ( + [ "perl", "2", "000", "python", "r4ku" ], + [ "001", "1", "000", "0001" ], +); + +sub alphanumeric_string_value { + my $alphanumstr = shift; + my $max = 0; + + foreach my $x (@$alphanumstr) { + my $value = ($x =~ /^\d+$/ ? $x + 0 : length $x); + + $max = $value if ( $value > $max) + } + + return $max; +} + +for my $elements (@examples) { + my $asv = alphanumeric_string_value $elements; + + say 'Input : @alphanumstr = ', dump(@$elements); + say 'Output : ', $asv; + say ' '; +} |
