aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-266/peter-meszaros/perl/ch-1.pl61
-rwxr-xr-xchallenge-266/peter-meszaros/perl/ch-2.pl83
2 files changed, 144 insertions, 0 deletions
diff --git a/challenge-266/peter-meszaros/perl/ch-1.pl b/challenge-266/peter-meszaros/perl/ch-1.pl
new file mode 100755
index 0000000000..58af846992
--- /dev/null
+++ b/challenge-266/peter-meszaros/perl/ch-1.pl
@@ -0,0 +1,61 @@
+#!/usr/bin/env perl
+#
+=head1 Task 1: Uncommon Words
+
+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.
+
+=head2 Example 1
+
+Input: $line1 = 'Mango is sweet'
+ $line2 = 'Mango is sour'
+Output: ('sweet', 'sour')
+
+=head2 Example 2
+
+Input: $line1 = 'Mango Mango'
+ $line2 = 'Orange'
+Output: ('Orange')
+
+=head2 Example 3
+
+Input: $line1 = 'Mango is Mango'
+ $line2 = 'Orange is Orange'
+Output: ('')
+
+=cut
+
+use strict;
+use warnings;
+use Test2::V0 -no_srand => 1;
+use Data::Dumper;
+
+my $cases = [
+ [['Mango is sweet', 'Mango is sour'], ['sour', 'sweet'], 'Example 1'],
+ [['Mango Mango', 'Orange'], ['Orange'], 'Example 2'],
+ [['Mango is Mango', 'Orange is Orange'], [''], 'Example 3'],
+];
+
+sub uncommon_words
+{
+ my $line1 = $_[0]->[0];
+ my $line2 = $_[0]->[1];
+
+ my %h;
+ $h{$_}++ for split(/\s+/, $line1), split(/\s+/, $line2);
+
+ my @res = sort grep {$h{$_} == 1} keys %h;
+ return @res ? \@res : [''];
+}
+
+for (@$cases) {
+ is(uncommon_words($_->[0]), $_->[1], $_->[2]);
+}
+done_testing();
+
+exit 0;
diff --git a/challenge-266/peter-meszaros/perl/ch-2.pl b/challenge-266/peter-meszaros/perl/ch-2.pl
new file mode 100755
index 0000000000..f4c3bc201e
--- /dev/null
+++ b/challenge-266/peter-meszaros/perl/ch-2.pl
@@ -0,0 +1,83 @@
+#!/usr/bin/env perl
+#
+=head1 Task 2: X Matrix
+
+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.
+
+=head2 Example 1
+
+Input: $matrix = [ [1, 0, 0, 2],
+ [0, 3, 4, 0],
+ [0, 5, 6, 0],
+ [7, 0, 0, 1],
+ ]
+Output: true
+
+=head2 Example 2
+
+Input: $matrix = [ [1, 2, 3],
+ [4, 5, 6],
+ [7, 8, 9],
+ ]
+Output: false
+
+=head2 Example 3
+
+Input: $matrix = [ [1, 0, 2],
+ [0, 3, 0],
+ [4, 0, 5],
+ ]
+Output: true
+
+=cut
+
+use strict;
+use warnings;
+use Test2::V0 -no_srand => 1;
+use Data::Dumper;
+
+my $cases = [
+ [[[1, 0, 0, 2],
+ [0, 3, 4, 0],
+ [0, 5, 6, 0],
+ [7, 0, 0, 1],
+ ], 1, 'Example 1'],
+ [[[1, 2, 3],
+ [4, 5, 6],
+ [7, 8, 9],
+ ], 0, 'Example 2'],
+ [[[1, 0, 2],
+ [0, 3, 0],
+ [4, 0, 5],
+ ], 1, 'Example 3'],
+];
+
+sub x_matrix
+{
+ my $m = shift;
+ my $n = $#$m;
+
+ for my $i (0..$n) {
+ for my $j (0..$n) {
+ if ($i == $j || ($i+$j) == $n) { # diagional or antidiaginal
+ return 0 if $m->[$i]->[$j] == 0;
+ } else { # any other
+ return 0 if $m->[$i]->[$j] != 0;
+ }
+ }
+ }
+ return 1;
+}
+
+for (@$cases) {
+ is(x_matrix($_->[0]), $_->[1], $_->[2]);
+}
+done_testing();
+
+exit 0;
+