diff options
| author | ntovar <tovar.nelo@gmail.com> | 2024-04-25 15:35:28 -0500 |
|---|---|---|
| committer | ntovar <tovar.nelo@gmail.com> | 2024-04-25 15:35:28 -0500 |
| commit | 360c49abb0bbcfe458543c3fa9d89d265697b804 (patch) | |
| tree | a5a328172140e953abf247a63d9e057af47f2f7c | |
| parent | aed41ed119210ab32e32b82af8039f2c1457726b (diff) | |
| download | perlweeklychallenge-club-360c49abb0bbcfe458543c3fa9d89d265697b804.tar.gz perlweeklychallenge-club-360c49abb0bbcfe458543c3fa9d89d265697b804.tar.bz2 perlweeklychallenge-club-360c49abb0bbcfe458543c3fa9d89d265697b804.zip | |
Challenge 265. Add Perl and Bash (ch-1.sh) solutions. By Nelo Tovar.
| -rwxr-xr-x | challenge-266/nelo-tovar/bash/ch-1.sh | 63 | ||||
| -rw-r--r-- | challenge-266/nelo-tovar/perl/ch-1.pl | 46 | ||||
| -rw-r--r-- | challenge-266/nelo-tovar/perl/ch-2.pl | 60 |
3 files changed, 169 insertions, 0 deletions
diff --git a/challenge-266/nelo-tovar/bash/ch-1.sh b/challenge-266/nelo-tovar/bash/ch-1.sh new file mode 100755 index 0000000000..9e5b34b629 --- /dev/null +++ b/challenge-266/nelo-tovar/bash/ch-1.sh @@ -0,0 +1,63 @@ +#!/usr/bin/env bash +# +# The Weekly Challenge 266 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-266/ +# +# Task 1 : Uncommon Words + +function unique() { + local sentence=$1 + local uniq=() + declare -A local words + + for word in $sentence; do + ((words[$word]++)) + done + + for key in ${!words[@]}; do + if [[ ${words[$key]} -eq 1 ]]; then + uniq+=($key) + fi + done + + echo "${uniq[@]}" +} + +function uncommon_words() { + local line1=$1 + local line2=$2 + local uncommons=() + + line1=${line1,,} + line2=${line2,,} + + words1=($(unique "$line1")) + words2=($(unique "$line2")) + + for key in ${words1[@]}; do + if [[ ! $line2 =~ $key ]]; then + uncommons+=($key) + fi + done + + for key in ${words2[@]}; do + if [[ ! $line1 =~ $key ]]; then + uncommons+=($key) + fi + done + + echo ${uncommons[@]} +} + +examples_line1=('Mango is sweet' 'Mango Mango' 'Mango is Mango') +examples_line2=('Mango is sour' 'Orange' 'Orange is Orange' ) + +for (( i = 0; i < ${#examples_line1[@]}; i++ )); do + uw=($(uncommon_words "${examples_line1[$i]}" "${examples_line2[$i]}")) + echo "Input : line1 = ${examples_line1[$i]}" + echo " line2 = ${examples_line2[$i]}" + echo "Output : (${uw[@]})" + echo "" +done + diff --git a/challenge-266/nelo-tovar/perl/ch-1.pl b/challenge-266/nelo-tovar/perl/ch-1.pl new file mode 100644 index 0000000000..32e6830445 --- /dev/null +++ b/challenge-266/nelo-tovar/perl/ch-1.pl @@ -0,0 +1,46 @@ +#!/usr/bin/env perl + +# The Weekly Challenge 266 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-266/ +# +# Task 1 - Uncommon Words +# + +use strict; +use warnings; +use v5.28; +use List::MoreUtils qw(none singleton); +use Data::Dump qw(dump); + +my @examples = ( + {line1 => 'Mango is sweet', line2 => 'Mango is sour' }, + {line1 => 'Mango Mango', line2 => 'Orange'}, + {line1 => 'Mango is Mango', line2 => 'Orange is Orange'}, +); + +sub uncommon_words { + my @words1 = split / /, lc(shift); #line1; + my @words2 = split / /, lc(shift); #line2; + my @uniq1 = singleton @words1; + my @uniq2 = singleton @words2; + my @uncommons; + + foreach my $word (@uniq1) { + push @uncommons, $word if none {$_ eq $word} @words2; + } + foreach my $word (@uniq2) { + push @uncommons, $word if none {$_ eq $word} @words1; + } + + return \@uncommons; +} + +for my $elements (@examples) { + my $uw = uncommon_words $elements->{line1}, $elements->{line2}; + + say 'Input : line1 = ', $elements->{line1}; + say ' line2 = ', $elements->{line2}; + say 'Output : ', dump(@$uw); + say ' '; +} diff --git a/challenge-266/nelo-tovar/perl/ch-2.pl b/challenge-266/nelo-tovar/perl/ch-2.pl new file mode 100644 index 0000000000..37e117abf5 --- /dev/null +++ b/challenge-266/nelo-tovar/perl/ch-2.pl @@ -0,0 +1,60 @@ +#!/usr/bin/env perl + +# The Weekly Challenge 266 - By Nelo Tovar +# +# https://theweeklychallenge.org/blog/perl-weekly-challenge-266/ +# +# Task 2 - X Matrix +# + +use strict; +use warnings; +use v5.28; +use List::Util qw (min max); +use Algorithm::Combinatorics qw(combinations); +use Data::Dump qw(dump); + +my @examples = ( + [ [1, 0, 0, 2], + [0, 3, 4, 0], + [0, 5, 6, 0], + [7, 0, 0, 1], + ], + [ [1, 2, 3], + [4, 5, 6], + [7, 8, 9], + ], + [ [1, 0, 2], + [0, 3, 0], + [4, 0, 5], + ], +); + +sub x_matrix { + my $matrix = shift; + my $length = scalar @$matrix; + + for (my $i = 0; $i < $length; $i++) { + for (my $j = 0; $j < $length; $j++) { + if ($i == $j || $j == ($length - $i - 1)){ + return 'false' if ($matrix->[$i][$j] == 0); + }elsif ($matrix->[$i][$j] != 0){ + return 'false' + } + } + } + + return 'true'; +} + +for my $elements (@examples) { + my $xm = x_matrix $elements; + + say 'Input : matrix = ['; + foreach my $x (@$elements) { + printf "%s%s,\n", ' 'x18,dump($x); + }; + say ' ]'; + say 'Output : ', $xm; + say ' '; +} |
