diff options
| -rwxr-xr-x | challenge-248/e-choroba/perl/ch-1.pl | 26 | ||||
| -rwxr-xr-x | challenge-248/e-choroba/perl/ch-2.pl | 38 |
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'; |
