aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDave Jacoby <jacoby.david@gmail.com>2024-04-26 15:56:04 -0400
committerDave Jacoby <jacoby.david@gmail.com>2024-04-26 15:56:04 -0400
commitd65c7f73b41b07cf86f8dc3bfcc527278cdb4349 (patch)
treed652cf4655874c83c0c2fd1c4ad9397a479ec279
parentabdf407fb93a82f09995b16f3656f94df7985543 (diff)
downloadperlweeklychallenge-club-d65c7f73b41b07cf86f8dc3bfcc527278cdb4349.tar.gz
perlweeklychallenge-club-d65c7f73b41b07cf86f8dc3bfcc527278cdb4349.tar.bz2
perlweeklychallenge-club-d65c7f73b41b07cf86f8dc3bfcc527278cdb4349.zip
DAJ 266
-rw-r--r--challenge-266/dave-jacoby/perl/ch-1.pl32
-rw-r--r--challenge-266/dave-jacoby/perl/ch-2.pl60
2 files changed, 92 insertions, 0 deletions
diff --git a/challenge-266/dave-jacoby/perl/ch-1.pl b/challenge-266/dave-jacoby/perl/ch-1.pl
new file mode 100644
index 0000000000..6f07fec7fa
--- /dev/null
+++ b/challenge-266/dave-jacoby/perl/ch-1.pl
@@ -0,0 +1,32 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use experimental qw{ fc postderef say signatures state };
+
+my @examples = (
+
+ [ 'Mango is sweet', 'Mango is sour' ],
+ [ 'Mango Mango', 'Orange' ],
+ [ 'Mango is Mango', 'Orange is Orange' ],
+ [ 'Mango is not Orange', 'Orange isn\'t Mango' ],
+);
+
+for my $example (@examples) {
+ my @output = uncommon_words(@$example);
+ my $output = join ', ', map { qq{'$_'} }
+ map { my $z = $_; $z =~ s/\'/\\\'/gmix; $z } @output;
+ say <<"END";
+ Input: \$line1 = '$example->[0]'
+ \$line2 = '$example->[1]'
+ Output: ($output)
+END
+}
+
+sub uncommon_words (@strings) {
+ my %hash;
+ my @output = map { $hash{$_}++; $_ } map { split /\s+/mix, $_ } @strings;
+ @output = grep { $hash{$_} == 1 } @output;
+ return '' unless scalar @output;
+ return return @output;
+}
diff --git a/challenge-266/dave-jacoby/perl/ch-2.pl b/challenge-266/dave-jacoby/perl/ch-2.pl
new file mode 100644
index 0000000000..226268feef
--- /dev/null
+++ b/challenge-266/dave-jacoby/perl/ch-2.pl
@@ -0,0 +1,60 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use experimental qw{ fc say postderef signatures state };
+
+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 ], ],
+ [ [ 1, 0, 2 ], [ 0, 3, 0 ], [ 4, 3, 5 ], ],
+);
+
+for my $matrix (@examples) {
+ my $output = x_matrix($matrix) ? 'true' : 'false';
+ my $input = display_matrix($matrix);
+
+ say <<"END";
+ Input: \$matrix = [
+ $input,
+ ]
+ Output: $output
+END
+}
+
+sub display_matrix ($matrix) {
+ return join ",\n ",
+ map { join ' ', '[', ( join ', ', $_->@* ), ']' } $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.
+sub x_matrix ($matrix) {
+ my $z = -1 + scalar $matrix->@*;
+ my %diag;
+
+ for my $i ( 0 .. $z ) {
+ my $j = $z - $i;
+ $diag{$i}{$i} = 1;
+ $diag{$i}{$j} = 1;
+ }
+
+ for my $i ( 0 .. $z ) {
+ for my $j ( 0 .. $z ) {
+ my $k = $matrix->[$i][$j];
+ my $d = $diag{$i}{$j} || 0;
+ if ($d) {
+ return 0 if $k == 0;
+ }
+ else {
+ return 0 if $k != 0;
+ }
+ }
+ }
+ return 7;
+}
+