From ff53a9f766cbfd9f8026da1122cf475993f1e004 Mon Sep 17 00:00:00 2001 From: drbaggy Date: Fri, 12 Feb 2021 09:37:50 +0000 Subject: tidied up script --- challenge-099/james-smith/perl/ch-2.pl | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/challenge-099/james-smith/perl/ch-2.pl b/challenge-099/james-smith/perl/ch-2.pl index d0a3231645..cd9531a4fd 100644 --- a/challenge-099/james-smith/perl/ch-2.pl +++ b/challenge-099/james-smith/perl/ch-2.pl @@ -35,10 +35,9 @@ is( uniq_subseq_cache('abcabcabcabcabcabcabcabcabc','abc'),165 ); done_testing(); sub uniq_subseq { - my( $str, $sub ) = @_; + my( $res, $str, $sub ) = ( 0, @_ ); my $f = substr $sub, 0, 1, q(); return scalar @{[ $str =~ m{$f}g ]} if $sub eq q(); - my $res = 0; $res += uniq_subseq( $str, $sub ) while $str=~s{.*?$f}{}; return $res; } @@ -65,13 +64,11 @@ sub display_uniq_subseq { } sub uniq_subseq_cache { - my( $str, $sub ) = @_; - my $k = "$str-$sub"; - return $c->{$k} if exists $c->{$k}; + my( $res, $k, $str, $sub ) = ( 0, "$_[0]-$_[1]", @_ ); my $f = substr $sub, 0, 1, q(); + return $c->{$k} if exists $c->{$k}; return $c->{$k} = scalar @{[ $str =~ m{$f}g ]} if $sub eq q(); - my $res = 0; - $res += uniq_subseq( $str, $sub ) while $str=~s{.*?$f}{}; + $res += uniq_subseq_cache( $str, $sub ) while $str=~s{.*?$f}{}; return $c->{$k} = $res; } -- cgit From 81ea1831d28c9b979678a9de3054b3b08a5fb441 Mon Sep 17 00:00:00 2001 From: drbaggy Date: Fri, 12 Feb 2021 10:07:12 +0000 Subject: make variable names more meaningful --- challenge-099/james-smith/perl/ch-2.pl | 43 +++++++++++++++++----------------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/challenge-099/james-smith/perl/ch-2.pl b/challenge-099/james-smith/perl/ch-2.pl index cd9531a4fd..e3df1891a9 100644 --- a/challenge-099/james-smith/perl/ch-2.pl +++ b/challenge-099/james-smith/perl/ch-2.pl @@ -3,11 +3,9 @@ use strict; use warnings; -use feature qw(say); +use feature qw(say state); use Test::More; -my $c ={}; - is( uniq_subseq('littleit','lit'), 5 ); is( uniq_subseq('london','lon'), 3 ); is( uniq_subseq('abc','abc'), 1 ); @@ -35,11 +33,11 @@ is( uniq_subseq_cache('abcabcabcabcabcabcabcabcabc','abc'),165 ); done_testing(); sub uniq_subseq { - my( $res, $str, $sub ) = ( 0, @_ ); - my $f = substr $sub, 0, 1, q(); - return scalar @{[ $str =~ m{$f}g ]} if $sub eq q(); - $res += uniq_subseq( $str, $sub ) while $str=~s{.*?$f}{}; - return $res; + my( $result, $haystack, $needle ) = ( 0, @_ ); + my $first = substr $needle, 0, 1, q(); + return scalar @{[ $haystack =~ m{$first}g ]} if $needle eq q(); + $result += uniq_subseq( $haystack, $needle ) while $haystack=~s{.*?$first}{}; + return $result; } say q(); @@ -49,26 +47,27 @@ print join "\n", display_uniq_subseq( 'london', 'lon' ), q(), q(); print join "\n", display_uniq_subseq( 'abcabcabc', 'abc' ), q(), q(); sub display_uniq_subseq { - my( $str, $sub, $prev ) = ( @_, q() ); ## adding q() means previous is defined in first loop.... + my( $haystack, $needle, $prev ) = ( @_, q() ); ## adding q() means previous is defined in first loop.... - return ($prev =~s{\]\[}{}gr).$str if $sub eq q(); ## If we have exhausted the substring we return the previous part (by collapse []s) + return ($prev =~s{\]\[}{}gr).$haystack if $needle eq q(); ## If we have exhausted the substring we return the previous part (by collapse []s) - my( $r, $t, @res ) = ( '\A(.*?)('.(substr $sub, 0, 1, q()).')', q() ); ## regex collects anything before the matched letter & the matched letter + my( $regexp, @result ) = ( '\A(.*?)('.(substr $needle, 0, 1, q()).')' ); ## regex collects anything before the matched letter & the matched letter - while( $str =~ s{$r}{} ) { - my($a,$b) = ($1,$2); - push @res, display_uniq_subseq( $str, $sub, $prev.$a.'['.$b.']' ); - $prev .= $a.$b; ## put the match onto the previous string, and continue to next match + while( $haystack =~ s{$regexp}{} ) { + my($pre_match,$match) = ($1,$2); + push @result, display_uniq_subseq( $haystack, $needle, $prev.$pre_match.'['.$match.']' ); + $prev .= $pre_match.$match; ## put the match onto the previous string, and continue to next match } - return @res; + return @result; } sub uniq_subseq_cache { - my( $res, $k, $str, $sub ) = ( 0, "$_[0]-$_[1]", @_ ); - my $f = substr $sub, 0, 1, q(); - return $c->{$k} if exists $c->{$k}; - return $c->{$k} = scalar @{[ $str =~ m{$f}g ]} if $sub eq q(); - $res += uniq_subseq_cache( $str, $sub ) while $str=~s{.*?$f}{}; - return $c->{$k} = $res; + state $cache; + my( $result, $cache_key, $haystack, $needle ) = ( 0, "$_[0]-$_[1]", @_ ); + my $first = substr $needle, 0, 1, q(); + return $cache->{$cache_key} if exists $cache->{$cache_key}; + return $cache->{$cache_key} = scalar @{[ $haystack =~ m{$first}g ]} if $needle eq q(); + $result += uniq_subseq_cache( $haystack, $needle ) while $haystack=~s{.*?$first}{}; + return $cache->{$cache_key} = $result; } -- cgit