aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-12-25 00:11:57 +0000
committerGitHub <noreply@github.com>2023-12-25 00:11:57 +0000
commit7c994fcb226f6b85cbdf409d48c4196fcf6c1a44 (patch)
tree2d327698b5d5bd4b087b59de0f19ed48dbf2eccb
parente478b62437b0a4ac5b745752f2e38bd197a81b6e (diff)
parent25570c1e103251fc366c8d6bfe27c42d9f0fba6c (diff)
downloadperlweeklychallenge-club-7c994fcb226f6b85cbdf409d48c4196fcf6c1a44.tar.gz
perlweeklychallenge-club-7c994fcb226f6b85cbdf409d48c4196fcf6c1a44.tar.bz2
perlweeklychallenge-club-7c994fcb226f6b85cbdf409d48c4196fcf6c1a44.zip
Merge pull request #9281 from steve-g-lynn/branch-for-challenge-248
faster ch-1 in ch-1a.pl
-rwxr-xr-xchallenge-248/steve-g-lynn/perl/ch-1.pl18
-rwxr-xr-xchallenge-248/steve-g-lynn/perl/ch-1a.pl77
2 files changed, 83 insertions, 12 deletions
diff --git a/challenge-248/steve-g-lynn/perl/ch-1.pl b/challenge-248/steve-g-lynn/perl/ch-1.pl
index 16652eeb5a..6ab90e77a1 100755
--- a/challenge-248/steve-g-lynn/perl/ch-1.pl
+++ b/challenge-248/steve-g-lynn/perl/ch-1.pl
@@ -49,22 +49,16 @@ pp_def(
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;
- }
}
+ 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-1a.pl b/challenge-248/steve-g-lynn/perl/ch-1a.pl
new file mode 100755
index 0000000000..7c7d8d69db
--- /dev/null
+++ b/challenge-248/steve-g-lynn/perl/ch-1a.pl
@@ -0,0 +1,77 @@
+#!/usr/bin/env -S perl -wl
+
+#-- faster version that does everything in Inline Pdlpp
+
+use PDL;
+use Inline Pdlpp;
+
+local *shortest_distance_a=sub {
+ # faster version using PP for outer loop
+ my ($str,$chr)=@_;
+ ($str =~ /$chr/) || return 0;
+
+ (1==length($chr)) || (die "Single character input needed:$!\n");
+
+ #-- get indices where the element equals chr
+ my $grp=pdl(grep {$chr eq substr($str,$_,1)} 0..length($str));
+ my $retval=$grp->shortest_distance_pp(length($str));
+
+ return $retval;
+ };
+
+print &shortest_distance_a('loveleetcode','e');
+#[3 2 1 0 1 0 0 1 2 2 1 0]
+
+print &shortest_distance_a('aaab','b');
+#[3 2 1 0]
+
+__DATA__
+
+__Pdlpp__
+
+
+pp_def(
+ 'shortest_distance_pp',
+ Pars => 'indx a(m); indx lenstr(); [o]b(n);',
+ RedoDimsCode => '$SIZE(n)=$lenstr();',
+ GenericTypes => ['N'],
+ Code => q{
+ PDL_Indx i,j,size_m, size_n, ctr_stop;
+ size_m=$SIZE(m);
+ size_n=$SIZE(n);
+ ctr_stop=$a(m=>0);
+ if (0 < $a(m=>0)){
+ for (i=0; i < ctr_stop; i++) {
+ $b(n=>i)=ctr_stop-i;
+ }
+ }
+ PDL_Indx a1,a2,c_mid,gap_size;
+ for (j=0; j<size_m; j++) {
+ a1=$a(m=>j);
+ a2=$a(m=>j+1);
+ gap_size=a2-a1+1;
+
+ if (0==(gap_size % 2)){
+ c_mid=gap_size/2;
+ }
+ else {
+ c_mid=(gap_size+1)/2;
+ }
+
+ for (i=a1; i < a1+c_mid; i++) {
+ $b(n=>i)=i-a1;
+ }
+ for (i=a1+c_mid; i <= a2; i++) {
+ $b(n=>i)=a2-i;
+ }
+
+ }
+ if (size_n > $a(m=>-1)+1){
+ a1=$a(m=>size_m-1);
+ for (i=a1; i < size_n; i++) {
+ $b(n=>i)=i-a1;
+ }
+ }
+ },
+);
+