aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-12-23 23:49:14 +0000
committerGitHub <noreply@github.com>2023-12-23 23:49:14 +0000
commit352774fcfc5e2d3cf85f05aff6015cfa9e16a076 (patch)
treebf21c5cd14a9c62b49815a48be61a17d99b0038b
parente81c9d6731913a2b45b3592e329bbb907fb183dc (diff)
parent0cee7cdbce77afb4ea73d5baf408f6f830d202a1 (diff)
downloadperlweeklychallenge-club-352774fcfc5e2d3cf85f05aff6015cfa9e16a076.tar.gz
perlweeklychallenge-club-352774fcfc5e2d3cf85f05aff6015cfa9e16a076.tar.bz2
perlweeklychallenge-club-352774fcfc5e2d3cf85f05aff6015cfa9e16a076.zip
Merge pull request #9278 from steve-g-lynn/branch-for-challenge-248
pwc 248
-rw-r--r--challenge-248/steve-g-lynn/blog.txt1
-rwxr-xr-xchallenge-248/steve-g-lynn/perl/ch-1.pl72
-rwxr-xr-xchallenge-248/steve-g-lynn/perl/ch-2.pl40
3 files changed, 113 insertions, 0 deletions
diff --git a/challenge-248/steve-g-lynn/blog.txt b/challenge-248/steve-g-lynn/blog.txt
new file mode 100644
index 0000000000..f1e84ab7a3
--- /dev/null
+++ b/challenge-248/steve-g-lynn/blog.txt
@@ -0,0 +1 @@
+https://thiujiac.blogspot.com/2023/12/pwc-248.html
diff --git a/challenge-248/steve-g-lynn/perl/ch-1.pl b/challenge-248/steve-g-lynn/perl/ch-1.pl
new file mode 100755
index 0000000000..16652eeb5a
--- /dev/null
+++ b/challenge-248/steve-g-lynn/perl/ch-1.pl
@@ -0,0 +1,72 @@
+#!/usr/bin/env -S perl -wl
+
+use PDL;
+use Inline Pdlpp;
+
+local *shortest_distance=sub {
+ my ($str,$chr)=@_;
+ ($str =~ /$chr/) || return 0;
+
+ (1==length($chr)) || (die "Single character input needed:$!\n");
+
+ my $retval=zeros(length($str)); #-- pdl to hold return value
+
+ #-- get indices where the element equals chr
+ my @grp=(grep {$chr eq substr($str,$_,1)} 0..length($str));
+
+ #-- if the initial matching index is greater than 0 say x, fill 0:x
+ ($grp[0] > 0) && ($retval->slice("0:$grp[0]") .= sequence($grp[0]+1)->slice('-1:0'));
+
+ #-- fill in distances between elements of @grp
+ map {
+ $retval->slice("$grp[$_]:$grp[$_+1]") .=
+ pdl($grp[$_])->between(pdl($grp[$_+1]));
+ } 0 .. ($#grp-1);
+
+
+ #-- if the last element of @grp is less than the length of $str, fill in the distance for the remaining elemtnts.
+ ($grp[-1] < (length($str)-1)) && ($retval->slice("$grp[-1]:") .= sequence(length($str)-$grp[-1]));
+
+ return $retval;
+ };
+
+
+print &shortest_distance('loveleetcode','e');
+#[3 2 1 0 1 0 0 1 2 2 1 0]
+
+print &shortest_distance('aaab','b');
+#[3 2 1 0]
+
+__DATA__
+
+__Pdlpp__
+
+pp_def(
+ 'between',
+ Pars => 'indx a1(); indx a2(); int [o]b(n);',
+ RedoDimsCode => '$SIZE(n)=$a2()-$a1()+1;',
+ Code => q{
+ PDL_Indx i,c_mid, n_size=$SIZE(n);
+ if (0==(n_size % 2)){
+ c_mid=n_size/2;
+ for (i=0; i < c_mid; i++) {
+ $b(n=>i)=i;
+ }
+ for (i=c_mid; i < n_size; i++) {
+ $b(n=>i)=n_size-i-1;
+ }
+ }
+ else {
+ c_mid=(n_size+1)/2;
+ for (i=0; i < c_mid; i++) {
+ $b(n=>i)=i;
+ }
+ for (i=c_mid; i < n_size; i++) {
+ $b(n=>i)=n_size-i-1;
+ }
+ }
+ },
+);
+
+
+
diff --git a/challenge-248/steve-g-lynn/perl/ch-2.pl b/challenge-248/steve-g-lynn/perl/ch-2.pl
new file mode 100755
index 0000000000..0c2a7c4789
--- /dev/null
+++ b/challenge-248/steve-g-lynn/perl/ch-2.pl
@@ -0,0 +1,40 @@
+#!/usr/bin/env -S perl -wl
+
+use PDL;
+use Inline Pdlpp;
+
+my $mat=pdl([[1,2,3,4],[5,6,7,8],[9,10,11,12]]);
+print $mat->submatrix_sum;
+#[
+# [14 18 22]
+# [30 34 38]
+#]
+
+$mat=pdl([[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]]);
+print $mat->submatrix_sum;
+#[
+# [2 1 0]
+# [1 2 1]
+# [0 1 2]
+#]
+
+__DATA__
+
+__Pdlpp__
+
+pp_def('submatrix_sum',
+ Pars => 'a(m,n); [o]b(p,q);',
+ RedoDimsCode => '$SIZE(p)=$SIZE(m)-1; $SIZE(q)=$SIZE(n)-1;',
+ Code => q{
+ PDL_Indx i, j, m_size, n_size;
+ m_size=$SIZE(m)-1;
+ n_size=$SIZE(n)-1;
+ for (i=0; i < m_size; i++) {
+ for (j=0; j < n_size; j++) {
+ $b(p=>i, q=>j)=
+ $a(m=>i, n=>j)+$a(m=>i+1, n=>j+1)+
+ $a(m=>i, n=>j+1)+$a(m=>i+1, n=>j);
+ }
+ }
+ },
+);