From 71be28ef5f9145bcc7b0e25016589ff0ca7159c9 Mon Sep 17 00:00:00 2001 From: drbaggy Date: Thu, 11 Feb 2021 22:33:34 +0000 Subject: pushing solutions --- challenge-099/james-smith/perl/ch-1.pl | 24 +++++++++++++++ challenge-099/james-smith/perl/ch-2.pl | 56 ++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 challenge-099/james-smith/perl/ch-1.pl create mode 100644 challenge-099/james-smith/perl/ch-2.pl diff --git a/challenge-099/james-smith/perl/ch-1.pl b/challenge-099/james-smith/perl/ch-1.pl new file mode 100644 index 0000000000..2f3aad3bfe --- /dev/null +++ b/challenge-099/james-smith/perl/ch-1.pl @@ -0,0 +1,24 @@ +#!/usr/local/bin/perl + +use strict; + +use warnings; +use feature qw(say); +use Test::More; + +is( file_name_match('abcde','a*e'), 1 ); +is( file_name_match('abcde','a*d'), 0 ); +is( file_name_match('abcde','?b*d'), 0 ); +is( file_name_match('abcde','a*c?e'), 1 ); + +done_testing(); + +sub file_name_match { + ## Convert unix file match pattern with perl regex + ## Add beginning end of string anchors \A & \Z to the beginning and end + ## Convert '*' to '.*' & '?' to '.' + ## {use r modifier to return string with replacements in} + my $re = '\A'.($_[1]=~s{[*]}{.*}gr=~s{[?]}{.}gr).'\Z'; + return $_[0] =~ m{$re} ? 1 : 0; +} + diff --git a/challenge-099/james-smith/perl/ch-2.pl b/challenge-099/james-smith/perl/ch-2.pl new file mode 100644 index 0000000000..0fb16e03b1 --- /dev/null +++ b/challenge-099/james-smith/perl/ch-2.pl @@ -0,0 +1,56 @@ +#!/usr/local/bin/perl + +use strict; + +use warnings; +use feature qw(say); +use Test::More; + +my $c ={}; + +is( uniq_subseq('littleit','lit'), 5 ); +is( uniq_subseq('london','lon'), 3 ); +is( uniq_subseq('abc','abc'), 1 ); +is( uniq_subseq('abcabc','abc'), 4 ); +is( uniq_subseq('abcabcabc','abc'), 10 ); +is( uniq_subseq('abcabcabcabc','abc'),20 ); +is( uniq_subseq('abcabcabcabcabc','abc'),35 ); +is( uniq_subseq('abcabcabcabcabcabc','abc'),56 ); +is( uniq_subseq('abcabcabcabcabcabcabc','abc'),84 ); +is( uniq_subseq('abcabcabcabcabcabcabcabc','abc'),120 ); +is( uniq_subseq('abcabcabcabcabcabcabcabcabc','abc'),165 ); + +is( uniq_subseq_cache('littleit','lit'), 5 ); +is( uniq_subseq_cache('london','lon'), 3 ); +is( uniq_subseq_cache('abc','abc'), 1 ); +is( uniq_subseq_cache('abcabc','abc'), 4 ); +is( uniq_subseq_cache('abcabcabc','abc'), 10 ); +is( uniq_subseq_cache('abcabcabcabc','abc'),20 ); +is( uniq_subseq_cache('abcabcabcabcabc','abc'),35 ); +is( uniq_subseq_cache('abcabcabcabcabcabc','abc'),56 ); +is( uniq_subseq_cache('abcabcabcabcabcabcabc','abc'),84 ); +is( uniq_subseq_cache('abcabcabcabcabcabcabcabc','abc'),120 ); +is( uniq_subseq_cache('abcabcabcabcabcabcabcabcabc','abc'),165 ); + +done_testing(); + +sub uniq_subseq { + my( $str, $sub ) = @_; + my $f = substr $sub,0,1,''; + return scalar @{[ $str =~ m{$f}g ]} if $sub eq q(); + my $res = 0; + $res += uniq_subseq($str,$sub) while $str=~s{.*?$f}{}; + return $res; +} + +sub uniq_subseq_cache { + my( $str, $sub ) = @_; + my $k = "$str-$sub"; + return $c->{$k} if exists $c->{$k}; + my $f = substr $sub,0,1,''; + return $c->{$k} = scalar @{[ $str =~ m{$f}g ]} if $sub eq q(); + my $res = 0; + $res += uniq_subseq($str,$sub) while $str=~s{.*?$f}{}; + return $c->{$k} = $res; +} + -- cgit 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 From b6564e584726b76c7e107ef42b96ad94a0bd10e3 Mon Sep 17 00:00:00 2001 From: drbaggy Date: Thu, 11 Feb 2021 23:09:10 +0000 Subject: whitespace --- challenge-099/james-smith/perl/ch-2.pl | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/challenge-099/james-smith/perl/ch-2.pl b/challenge-099/james-smith/perl/ch-2.pl index b22cf7e58f..d0a3231645 100644 --- a/challenge-099/james-smith/perl/ch-2.pl +++ b/challenge-099/james-smith/perl/ch-2.pl @@ -36,29 +36,30 @@ done_testing(); sub uniq_subseq { my( $str, $sub ) = @_; - my $f = substr $sub,0,1,''; + 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}{}; + $res += uniq_subseq( $str, $sub ) while $str=~s{.*?$f}{}; 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' ),'',''; +say q(); + +print join "\n", display_uniq_subseq( 'littleit', 'lit' ), q(), q(); +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 ) = (@_,''); + my( $str, $sub, $prev ) = ( @_, q() ); ## adding q() means previous is defined in first loop.... - return ($prev =~s{\]\[}{}gr).$str if $sub eq ''; ## If we have exhausted the substring we return the previous part (by collapse []s) + return ($prev =~s{\]\[}{}gr).$str if $sub 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() ); ## regex collects anything before the matched letter & the matched letter + my( $r, $t, @res ) = ( '\A(.*?)('.(substr $sub, 0, 1, q()).')', 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. + 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 } return @res; } @@ -67,10 +68,10 @@ sub uniq_subseq_cache { my( $str, $sub ) = @_; my $k = "$str-$sub"; return $c->{$k} if exists $c->{$k}; - my $f = substr $sub,0,1,''; + my $f = substr $sub, 0, 1, q(); 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( $str, $sub ) while $str=~s{.*?$f}{}; return $c->{$k} = $res; } -- cgit