diff options
| -rw-r--r-- | challenge-257/nelo-tovar/bash/2 | 1 | ||||
| -rwxr-xr-x | challenge-257/nelo-tovar/bash/ch-1.sh | 38 | ||||
| -rw-r--r-- | challenge-257/nelo-tovar/perl/ch-1.pl | 40 | ||||
| -rw-r--r-- | challenge-257/nelo-tovar/perl/ch-2.pl | 103 |
4 files changed, 181 insertions, 1 deletions
diff --git a/challenge-257/nelo-tovar/bash/2 b/challenge-257/nelo-tovar/bash/2 deleted file mode 100644 index c7c3a5dc0b..0000000000 --- a/challenge-257/nelo-tovar/bash/2 +++ /dev/null @@ -1 +0,0 @@ -word = l diff --git a/challenge-257/nelo-tovar/bash/ch-1.sh b/challenge-257/nelo-tovar/bash/ch-1.sh new file mode 100755 index 0000000000..93d26e3712 --- /dev/null +++ b/challenge-257/nelo-tovar/bash/ch-1.sh @@ -0,0 +1,38 @@ +#!/usr/bin/env bash +# +# The Weekly Challenge 257 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-257/ +# +# Task 1 : Smaller than Current + +function smaller_than_current() { + local ints=("$@") + local length=${#ints[@]} + local smallers=() + + for i in ${ints[@]}; do + count=0 + for j in ${ints[@]}; do + if [ $j -lt $i ]; then + ((count++)) + fi + done + smallers+=($count) + done + echo ${smallers[@]} +} + +example1='5 2 1 6' +example2='1 2 0 3' +example3='0 1' +example4='9 4 9 2' + +for e in "$example1" "$example2" "$example3" "$example4"; do + array=($e) + stc=($(smaller_than_current "${array[@]}")) + echo "Input : nums = (${array[@]})" + echo "Output : (${stc[@]})" + echo "" +done + diff --git a/challenge-257/nelo-tovar/perl/ch-1.pl b/challenge-257/nelo-tovar/perl/ch-1.pl new file mode 100644 index 0000000000..269e80b8b4 --- /dev/null +++ b/challenge-257/nelo-tovar/perl/ch-1.pl @@ -0,0 +1,40 @@ +#!/usr/bin/env perl + +# The Weekly Challenge 257 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-257/ +# +# Task 1 - Smaller Than Current +# + +use strict; +use warnings; +use v5.28; +use Data::Dump qw(dump); + +my @examples = ( + [ 5, 2, 1, 6 ], + [ 1, 2, 0, 3 ], + [ 0, 1 ], + [ 9, 4, 9, 2 ], +); + +sub smaller_than_current { + my $ints = shift; + my @smallers; + + foreach my $x (@$ints) { + my $count = grep {$_ < $x } @$ints; + push(@smallers, $count) + } + + return \@smallers; +} + +for my $elements (@examples) { + my $stc = smaller_than_current $elements; + + say 'Input : @ints = ', dump(@$elements); + say 'Output : ', dump(@$stc); + say ' '; +} diff --git a/challenge-257/nelo-tovar/perl/ch-2.pl b/challenge-257/nelo-tovar/perl/ch-2.pl new file mode 100644 index 0000000000..bbfdfd6817 --- /dev/null +++ b/challenge-257/nelo-tovar/perl/ch-2.pl @@ -0,0 +1,103 @@ +#!/usr/bin/env perl + +# The Weekly Challenge 257 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-257/ +# +# Task 2 - Reduced Row Echelon +# + +use strict; +use warnings; +use v5.28; +use List::MoreUtils qw /before first_value first_index/; +use Data::Dump qw(dump); + +my @examples = ( + [ + [1, 1, 0], + [0, 1, 0], + [0, 0, 0] + ], + [ + [0, 1,-2, 0, 1], + [0, 0, 0, 1, 3], + [0, 0, 0, 0, 0], + [0, 0, 0, 0, 0] + ], + [ + [1, 0, 0, 4], + [0, 1, 0, 7], + [0, 0, 1,-1] + ], + [ + [0, 1,-2, 0, 1], + [0, 0, 0, 0, 0], + [0, 0, 0, 1, 3], + [0, 0, 0, 0, 0] + ], + [ + [0, 1, 0], + [1, 0, 0], + [0, 0, 0] + ], + [ + [4, 0, 0, 0], + [0, 1, 0, 7], + [0, 0, 1,-1] + ] +); + +sub get_pivot { + my $array = shift; + my $length = scalar @$array; + + return first_value {$_ > 0} @$array; + +} + +sub reduce_row_echelon { + my $nums = shift; + my $rows = scalar @$nums; + my @pivots_index = (); + my $zero_row; + + for (my $i = 0; $i < $rows; $i++) { + my $a = @$nums[$i]; + my $pivot = get_pivot($a); + + if (! defined($pivot)){ + $zero_row = $i; + next + } + + return 0 if ( defined($zero_row) ); + return 0 if ( $pivot != 1 ); + + my $pivot_index = first_index {$_ == $pivot} @$a; + push @pivots_index, $pivot_index; + + for (my $j = 0; $j < $rows; $j++) { + next if ( $j eq $i ); + return 0 if (@$nums[$j]->[$pivot_index] != 0 ); + } + } + + for (my $i = 0; $i < @pivots_index - 1; $i++) { + return 0 if ( $pivots_index[$i] >= $pivots_index[$i + 1] ); + } + + return 1; +} + +for my $elements (@examples) { + my $rre = reduce_row_echelon $elements; + + say 'Input : $M = [ '; + foreach my $x (@$elements) { + say ' ' x 15, dump(@$x); + } + say ' ]'; + say 'Output : ', $rre; + say ' '; +} |
