diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2024-08-27 11:36:47 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2024-08-28 11:03:40 +0100 |
| commit | b14a664f85a3cbd2f55cb5b1bd08f251ff3bc82a (patch) | |
| tree | e121fb33ff5e3c035c0f3cbe46a27aa84cd82501 | |
| parent | 6c7dbe6403cd700b6d9ffc7cfff4e03dbe828091 (diff) | |
| download | perlweeklychallenge-club-b14a664f85a3cbd2f55cb5b1bd08f251ff3bc82a.tar.gz perlweeklychallenge-club-b14a664f85a3cbd2f55cb5b1bd08f251ff3bc82a.tar.bz2 perlweeklychallenge-club-b14a664f85a3cbd2f55cb5b1bd08f251ff3bc82a.zip | |
Add Perl solution to challenge 266
| -rw-r--r-- | challenge-266/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-266/paulo-custodio/perl/ch-1.pl | 43 | ||||
| -rw-r--r-- | challenge-266/paulo-custodio/perl/ch-2.pl | 69 | ||||
| -rw-r--r-- | challenge-266/paulo-custodio/t/test-1.yaml | 15 | ||||
| -rw-r--r-- | challenge-266/paulo-custodio/t/test-2.yaml | 15 |
5 files changed, 144 insertions, 0 deletions
diff --git a/challenge-266/paulo-custodio/Makefile b/challenge-266/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-266/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-266/paulo-custodio/perl/ch-1.pl b/challenge-266/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..c31112a73a --- /dev/null +++ b/challenge-266/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,43 @@ +#!/usr/bin/env perl + +# Challenge 266 +# +# Task 1: Uncommon Words +# Submitted by: Mohammad Sajid Anwar +# +# You are given two sentences, $line1 and $line2. +# +# Write a script to find all uncommmon words in any order in the given two +# sentences. Return ('') if none found. +# +# A word is uncommon if it appears exactly once in one of the sentences +# and doesn’t appear in other sentence. +# +# Example 1 +# +# Input: $line1 = 'Mango is sweet' +# $line2 = 'Mango is sour' +# Output: ('sweet', 'sour') +# +# Example 2 +# +# Input: $line1 = 'Mango Mango' +# $line2 = 'Orange' +# Output: ('Orange') +# +# Example 3 +# +# Input: $line1 = 'Mango is Mango' +# $line2 = 'Orange is Orange' +# Output: ('') + +use Modern::Perl; + +my($line1, $line2) = split /,/, "@ARGV"; +my %count; +for my $word (split ' ', "$line1 $line2") { + $count{$word}++; +} +my @uncommon = sort grep {$count{$_}==1} keys %count; + +say @uncommon ? "@uncommon" : "''"; diff --git a/challenge-266/paulo-custodio/perl/ch-2.pl b/challenge-266/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..d5292f221e --- /dev/null +++ b/challenge-266/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,69 @@ +#!/usr/bin/env perl + +# Challenge 266 +# +# Task 2: X Matrix +# Submitted by: Mohammad Sajid Anwar +# +# You are given a square matrix, $matrix. +# +# Write a script to find if the given matrix is X Matrix. +# +# A square matrix is an X Matrix if all the elements on the main diagonal +# and antidiagonal are non-zero and everything else are zero. +# +# Example 1 +# +# Input: $matrix = [ [1, 0, 0, 2], +# [0, 3, 4, 0], +# [0, 5, 6, 0], +# [7, 0, 0, 1], +# ] +# Output: true +# +# Example 2 +# +# Input: $matrix = [ [1, 2, 3], +# [4, 5, 6], +# [7, 8, 9], +# ] +# Output: false +# +# Example 3 +# +# Input: $matrix = [ [1, 0, 2], +# [0, 3, 0], +# [4, 0, 5], +# ] +# Output: true + +use Modern::Perl; + +my @matrix = parse_matrix("@ARGV"); +say is_x(@matrix) ? 'true' : 'false'; + +sub parse_matrix { + my($text) = @_; + my @lines = split /,/, $text; + my @matrix = map {[split ' ', $_]} @lines; + return @matrix; +} + +sub is_x { + my(@matrix) = @_; + + return 0 if scalar(@matrix) != scalar(@{$matrix[0]}); # not square + + for my $i (0 .. $#matrix) { + for my $j (0 .. $#matrix) { + if ($i == $j || $#matrix - $i == $j) { + return 0 if $matrix[$i][$j] == 0; + } + else { + return 0 if $matrix[$i][$j] != 0; + } + } + } + + return 1; +} diff --git a/challenge-266/paulo-custodio/t/test-1.yaml b/challenge-266/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..bc64f94203 --- /dev/null +++ b/challenge-266/paulo-custodio/t/test-1.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: Mango is sweet , Mango is sour + input: + output: sour sweet +- setup: + cleanup: + args: Mango Mango , Orange + input: + output: Orange +- setup: + cleanup: + args: Mango is Mango , Orange is Orange + input: + output: "''" diff --git a/challenge-266/paulo-custodio/t/test-2.yaml b/challenge-266/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..995b0d9f46 --- /dev/null +++ b/challenge-266/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 1 0 0 2 , 0 3 4 0 , 0 5 6 0 , 7 0 0 1 + input: + output: true +- setup: + cleanup: + args: 1 2 3 , 4 5 6 , 7 8 9 + input: + output: false +- setup: + cleanup: + args: 1 0 2 , 0 3 0 , 4 0 5 + input: + output: true |
