aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-04-26 09:09:11 +0100
committerGitHub <noreply@github.com>2024-04-26 09:09:11 +0100
commitabdf407fb93a82f09995b16f3656f94df7985543 (patch)
tree48a2f67bb724d2ca0be772ff39329c8e456fb232
parent015c1ba8fabea2d943bea0e884eab89f5279e99e (diff)
parent360c49abb0bbcfe458543c3fa9d89d265697b804 (diff)
downloadperlweeklychallenge-club-abdf407fb93a82f09995b16f3656f94df7985543.tar.gz
perlweeklychallenge-club-abdf407fb93a82f09995b16f3656f94df7985543.tar.bz2
perlweeklychallenge-club-abdf407fb93a82f09995b16f3656f94df7985543.zip
Merge pull request #9990 from ntovar/branch-266
Challenge 265. Add Perl and Bash (ch-1.sh) solutions. By Nelo Tovar.
-rwxr-xr-xchallenge-266/nelo-tovar/bash/ch-1.sh63
-rw-r--r--challenge-266/nelo-tovar/perl/ch-1.pl46
-rw-r--r--challenge-266/nelo-tovar/perl/ch-2.pl60
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 ' ';
+}