From df92c99c85ad0eef27c4dd77a2b135a41369f0c0 Mon Sep 17 00:00:00 2001 From: drbaggy Date: Thu, 11 Feb 2021 23:05:40 +0000 Subject: added display loop version as well as count --- challenge-099/james-smith/perl/ch-2.pl | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/challenge-099/james-smith/perl/ch-2.pl b/challenge-099/james-smith/perl/ch-2.pl index 0fb16e03b1..b22cf7e58f 100644 --- a/challenge-099/james-smith/perl/ch-2.pl +++ b/challenge-099/james-smith/perl/ch-2.pl @@ -43,6 +43,26 @@ sub uniq_subseq { return $res; } +say ''; +print join "\n", display_uniq_subseq( 'littleit', 'lit' ),'',''; +print join "\n", display_uniq_subseq( 'london', 'lon' ),'',''; +print join "\n", display_uniq_subseq( 'abcabcabc', 'abc' ),'',''; + +sub display_uniq_subseq { + my( $str, $sub, $prev ) = (@_,''); + + return ($prev =~s{\]\[}{}gr).$str if $sub eq ''; ## If we have exhausted the substring we return the previous part (by collapse []s) + + my( $r, $t, @res ) = ( '\A(.*?)('.(substr $sub,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 look for subsequent match. + } + return @res; +} + sub uniq_subseq_cache { my( $str, $sub ) = @_; my $k = "$str-$sub"; -- cgit