From ed0eedd8722b3fb4f9e3c9ea28e6dacb047e0a20 Mon Sep 17 00:00:00 2001 From: "E. Choroba" Date: Mon, 22 Apr 2024 10:52:33 +0200 Subject: Add solutions to 266: Uncommon Words & X Matrix by E. Choroba --- challenge-266/e-choroba/perl/ch-1.pl | 30 ++++++++++++++++++++++ challenge-266/e-choroba/perl/ch-2.pl | 50 ++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100755 challenge-266/e-choroba/perl/ch-1.pl create mode 100755 challenge-266/e-choroba/perl/ch-2.pl diff --git a/challenge-266/e-choroba/perl/ch-1.pl b/challenge-266/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..c150f01f1b --- /dev/null +++ b/challenge-266/e-choroba/perl/ch-1.pl @@ -0,0 +1,30 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub uncommon_words($line1, $line2) { + my %seen; + ++$seen{$1}{1} while $line1 =~ /(\w+)/g; + ++$seen{$1}{2} while $line2 =~ /(\w+)/g; + my @uncommon = grep { + ($seen{$_}{1} // 0) + ($seen{$_}{2} // 0) == 1 + } keys %seen; + return [""] unless @uncommon; + return \@uncommon +} + +use Test2::V0; +plan(3); + +is uncommon_words('Mango is sweet', 'Mango is sour'), + bag { item $_ for 'sweet', 'sour'; end() }, + 'Example 1'; + +is uncommon_words('Mango Mango', 'Orange'), + ['Orange'], + 'Example 2'; + +is uncommon_words('Mango is Mango', 'Orange is Orange'), + [""], + 'Example 3'; diff --git a/challenge-266/e-choroba/perl/ch-2.pl b/challenge-266/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..af99cc16cb --- /dev/null +++ b/challenge-266/e-choroba/perl/ch-2.pl @@ -0,0 +1,50 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +use PDL; + +sub x_matrix($matrix) { + my $m = pdl($matrix); + my $diagonal = $m->diagonal(0, 1); + return unless $diagonal->prod; + + my $antidiagonal = rotate_matrix($m)->diagonal(0, 1); + return unless $diagonal->prod; + + $diagonal .= 0; + $antidiagonal .= 0; + return if any($m); + + return 1 +} + +sub rotate_matrix($m) { + $m->slice([], [-1, 0]) +} + +use Test::More tests => 3 + 1; + +ok x_matrix([ [1, 0, 0, 2], + [0, 3, 4, 0], + [0, 5, 6, 0], + [7, 0, 0, 1], +]), 'Example 1'; + + +ok ! x_matrix([ [1, 2, 3], + [4, 5, 6], + [7, 8, 9], +]), 'Example 2'; + +ok x_matrix([ [1, 0, 2], + [0, 3, 0], + [4, 0, 5], + ]), 'Example 3'; + +ok ! x_matrix([ [1, 0, 0, 2], + [0, 3, 4, 0], + [0, 5, 0, 0], + [7, 0, 0, 1], +]), 'Zero on a diagonal'; -- cgit