aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-12-22 16:39:23 +0000
committerGitHub <noreply@github.com>2023-12-22 16:39:23 +0000
commitf6f187b247d1c25d42ad3fd3f10ce8c6c8211318 (patch)
tree3f8bbfbd6ce1fa8cff610aa7c6447397d9eee651
parenta0beb4deffb473d184a6f56804ccfbc8e31b1ec5 (diff)
parenta3f5ca2bc4fa1b216a9d1b1097aaf9af0a269aa4 (diff)
downloadperlweeklychallenge-club-f6f187b247d1c25d42ad3fd3f10ce8c6c8211318.tar.gz
perlweeklychallenge-club-f6f187b247d1c25d42ad3fd3f10ce8c6c8211318.tar.bz2
perlweeklychallenge-club-f6f187b247d1c25d42ad3fd3f10ce8c6c8211318.zip
Merge pull request #9275 from deadmarshal/TWC248
TWC248
-rw-r--r--challenge-248/deadmarshal/blog.txt1
-rw-r--r--challenge-248/deadmarshal/perl/ch-1.pl14
-rw-r--r--challenge-248/deadmarshal/perl/ch-2.pl21
-rw-r--r--challenge-248/deadmarshal/raku/ch-1.raku11
-rw-r--r--challenge-248/deadmarshal/raku/ch-2.raku17
5 files changed, 64 insertions, 0 deletions
diff --git a/challenge-248/deadmarshal/blog.txt b/challenge-248/deadmarshal/blog.txt
new file mode 100644
index 0000000000..a553152768
--- /dev/null
+++ b/challenge-248/deadmarshal/blog.txt
@@ -0,0 +1 @@
+https://deadmarshal.blogspot.com/2023/12/twc248.html
diff --git a/challenge-248/deadmarshal/perl/ch-1.pl b/challenge-248/deadmarshal/perl/ch-1.pl
new file mode 100644
index 0000000000..2844d9fcd6
--- /dev/null
+++ b/challenge-248/deadmarshal/perl/ch-1.pl
@@ -0,0 +1,14 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use List::MoreUtils qw(indexes);
+use List::Util qw(min);
+
+sub shortest_distance{
+ my @arr = split'',$_[0];
+ map{my $i = $_; min map{abs $i - $_}indexes{$_ eq $_[1]}@arr}0..$#arr;
+}
+
+printf "(%s)\n",join ',',shortest_distance('loveleetcode','e');
+printf "(%s)\n",join ',',shortest_distance('aaab','b');
+
diff --git a/challenge-248/deadmarshal/perl/ch-2.pl b/challenge-248/deadmarshal/perl/ch-2.pl
new file mode 100644
index 0000000000..c6af446383
--- /dev/null
+++ b/challenge-248/deadmarshal/perl/ch-2.pl
@@ -0,0 +1,21 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use Data::Show;
+
+sub submatrix_sum{
+ my ($m) = @_;
+ my @ret;
+ foreach my $i(0..$#$m-1){
+ push @ret,[];
+ foreach my $j(0..$#{$m->[0]}-1){
+ $ret[$i][$j] = $m->[$i][$j] + $m->[$i][$j+1] +
+ $m->[$i+1][$j] + $m->[$i+1][$j+1]
+ }
+ }
+ @ret
+}
+
+print show submatrix_sum([[1,2,3,4],[5,6,7,8],[9,10,11,12]]);
+print show submatrix_sum([[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]]);
+
diff --git a/challenge-248/deadmarshal/raku/ch-1.raku b/challenge-248/deadmarshal/raku/ch-1.raku
new file mode 100644
index 0000000000..86eec1e961
--- /dev/null
+++ b/challenge-248/deadmarshal/raku/ch-1.raku
@@ -0,0 +1,11 @@
+#!/usr/bin/env raku
+
+sub shortest-distance($str,$c)
+{
+ my @arr = $str.comb;
+ (0..@arr.end).map: {my $i = $_; ($str.indices($c).map: {abs $i - $_}).min}
+}
+
+say shortest-distance('loveleetcode','e');
+say shortest-distance('aaab','b');
+
diff --git a/challenge-248/deadmarshal/raku/ch-2.raku b/challenge-248/deadmarshal/raku/ch-2.raku
new file mode 100644
index 0000000000..faced1fe47
--- /dev/null
+++ b/challenge-248/deadmarshal/raku/ch-2.raku
@@ -0,0 +1,17 @@
+#!/usr/bin/env raku
+
+sub submatrix-sum(@m)
+{
+ my @ret;
+ for 0..@m.elems-2 -> $i {
+ for 0..@m[0].elems-2 -> $j {
+ @ret[$i;$j] = @m[$i;$j] + @m[$i;$j+1] +
+ @m[$i+1;$j] + @m[$i+1;$j+1];
+ }
+ }
+ @ret
+}
+
+say submatrix-sum([[1,2,3,4],[5,6,7,8],[9,10,11,12]]);
+say submatrix-sum([[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]]);
+