aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-12-18 20:35:23 +0000
committerGitHub <noreply@github.com>2023-12-18 20:35:23 +0000
commit0509d3bbd408119cf57336d66bbaa7aab9dec3ce (patch)
tree0bf89cc4eafcfacde86dce3409c5f809ed4fcb1a
parente6a0d3b4e504202fe35a2de0d63f2c2ce993a4cd (diff)
parent0ddabf87c75af4ada42b3cc99b990c04387b377b (diff)
downloadperlweeklychallenge-club-0509d3bbd408119cf57336d66bbaa7aab9dec3ce.tar.gz
perlweeklychallenge-club-0509d3bbd408119cf57336d66bbaa7aab9dec3ce.tar.bz2
perlweeklychallenge-club-0509d3bbd408119cf57336d66bbaa7aab9dec3ce.zip
Merge pull request #9265 from choroba/ech248
Solve 248: Shortest Distance & Submatrix Sum by E. Choroba
-rwxr-xr-xchallenge-248/e-choroba/perl/ch-1.pl26
-rwxr-xr-xchallenge-248/e-choroba/perl/ch-2.pl38
2 files changed, 64 insertions, 0 deletions
diff --git a/challenge-248/e-choroba/perl/ch-1.pl b/challenge-248/e-choroba/perl/ch-1.pl
new file mode 100755
index 0000000000..6ba2139342
--- /dev/null
+++ b/challenge-248/e-choroba/perl/ch-1.pl
@@ -0,0 +1,26 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use experimental qw( signatures );
+
+sub shortest_distance($str, $char) {
+ my @distances;
+ for my $pos (0 .. length($str) - 1) {
+ my $l = index($str, $char, $pos) - $pos;
+ if (0 <= ( my $r = rindex $str, $char, $pos )) {
+ $r = $pos - $r;
+ $l = $r if $r < $l;
+ }
+ push @distances, $l;
+ }
+ return \@distances
+}
+
+use Test2::V0;
+plan 2;
+
+is shortest_distance('loveleetcode', 'e'),
+ [3, 2, 1, 0, 1, 0, 0, 1, 2, 2, 1, 0],
+ 'Example 1';
+
+is shortest_distance('aaab', 'b'), [3, 2, 1, 0], 'Example 2';
diff --git a/challenge-248/e-choroba/perl/ch-2.pl b/challenge-248/e-choroba/perl/ch-2.pl
new file mode 100755
index 0000000000..32a0511384
--- /dev/null
+++ b/challenge-248/e-choroba/perl/ch-2.pl
@@ -0,0 +1,38 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use experimental qw( signatures );
+
+sub submatrix_sum($m) {
+ my @r;
+ for my $i (0 .. $#$m - 1) {
+ for my $j (0 .. $#{ $m->[0] } - 1) {
+ for my $k (0, 1) {
+ for my $l (0, 1) {
+ $r[$i][$j] += $m->[ $i + $k ][ $j + $l ];
+ }
+ }
+ }
+ }
+ return \@r
+}
+
+use Test2::V0 qw{ is plan };
+plan 2;
+
+is submatrix_sum([[1, 2, 3, 4],
+ [5, 6, 7, 8],
+ [9, 10, 11, 12]]),
+ [[14, 18, 22],
+ [30, 34, 38]],
+ 'Example 1';
+
+
+is submatrix_sum([[1, 0, 0, 0],
+ [0, 1, 0, 0],
+ [0, 0, 1, 0],
+ [0, 0, 0, 1]]),
+ [[2, 1, 0],
+ [1, 2, 1],
+ [0, 1, 2]],
+ 'Example 2';