aboutsummaryrefslogtreecommitdiff
path: root/challenge-099/james-smith
diff options
context:
space:
mode:
authordrbaggy <js5@sanger.ac.uk>2021-02-11 23:05:40 +0000
committerdrbaggy <js5@sanger.ac.uk>2021-02-11 23:05:40 +0000
commitdf92c99c85ad0eef27c4dd77a2b135a41369f0c0 (patch)
treed6fc2cf4632d1aa1dafe35410aa02755b00dc1db /challenge-099/james-smith
parent71be28ef5f9145bcc7b0e25016589ff0ca7159c9 (diff)
downloadperlweeklychallenge-club-df92c99c85ad0eef27c4dd77a2b135a41369f0c0.tar.gz
perlweeklychallenge-club-df92c99c85ad0eef27c4dd77a2b135a41369f0c0.tar.bz2
perlweeklychallenge-club-df92c99c85ad0eef27c4dd77a2b135a41369f0c0.zip
added display loop version as well as count
Diffstat (limited to 'challenge-099/james-smith')
-rw-r--r--challenge-099/james-smith/perl/ch-2.pl20
1 files changed, 20 insertions, 0 deletions
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";