aboutsummaryrefslogtreecommitdiff
path: root/challenge-116
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-06-12 21:41:54 +0100
committerGitHub <noreply@github.com>2021-06-12 21:41:54 +0100
commitc361222da6d2b7cb8d8f12d2ea33b70b9f593400 (patch)
treef2c628072d90e695f51ccd1f8adc2c16285da85a /challenge-116
parentf017365fa0689d27ac78f113369c2681cdb87900 (diff)
parentb7f163ef948758d60d69b29282de77f74f8aa31a (diff)
downloadperlweeklychallenge-club-c361222da6d2b7cb8d8f12d2ea33b70b9f593400.tar.gz
perlweeklychallenge-club-c361222da6d2b7cb8d8f12d2ea33b70b9f593400.tar.bz2
perlweeklychallenge-club-c361222da6d2b7cb8d8f12d2ea33b70b9f593400.zip
Merge pull request #4238 from drbaggy/master
Added check
Diffstat (limited to 'challenge-116')
-rw-r--r--challenge-116/james-smith/README.md3
-rw-r--r--challenge-116/james-smith/perl/ch-1.pl19
2 files changed, 14 insertions, 8 deletions
diff --git a/challenge-116/james-smith/README.md b/challenge-116/james-smith/README.md
index 9d77f3e59f..9900d1f1f9 100644
--- a/challenge-116/james-smith/README.md
+++ b/challenge-116/james-smith/README.md
@@ -32,7 +32,8 @@ sub splitnum {
my( $in, $start ) = ( shift, '' );
for( split //, substr $in, 0, (my $len = length $in) >> 1) {
my @range = ( my $str = my $end = $start .= $_ );
- ($str .= ++$end) && push @range, $end while $len > length $str;
+ ($str .= ++$end) && push @range, $end while ($len > length $str) &&
+ $end eq substr $in,length($str)-length($end),length($end);
return \@range if $string eq $in;
}
return [$in];
diff --git a/challenge-116/james-smith/perl/ch-1.pl b/challenge-116/james-smith/perl/ch-1.pl
index a43fcc2a65..b6c856ed35 100644
--- a/challenge-116/james-smith/perl/ch-1.pl
+++ b/challenge-116/james-smith/perl/ch-1.pl
@@ -5,6 +5,7 @@ use strict;
use warnings;
use feature qw(say);
use Test::More;
+use Benchmark qw(cmpthese);
my $nspace = join '', 1..1000;
my $ncomma = join ',',1..1000;
@@ -26,10 +27,12 @@ my @tests = (
);
is( join(',',@{splitnum($_->[0])}),$_->[1] ) foreach @tests;
-is( join(',',@{splitnum_no_comments($_->[0])}),$_->[1] ) foreach @tests;
-
+is( join(',',@{splitnum_optimized($_->[0])}),$_->[1] ) foreach @tests;
done_testing();
-
+cmpthese( 120, {
+ a => sub { splitnum($_->[0]) foreach @tests; },
+ c => sub { splitnum_optimized($_->[0]) foreach @tests; },
+});
sub splitnum {
my( $in, $start ) = ( shift, '' );
for( split //, substr $in, 0, (my $len = length $in) >> 1) {
@@ -65,14 +68,16 @@ sub splitnum {
## length of $n - but only really valid if you are getting very large
## values of $n...}
-## Below is the code above with the comments removed
-sub splitnum_no_comments {
+## We can add some optimization here... check that we match the string
+## otherwise return;
+
+sub splitnum_optimized {
my( $in, $start ) = ( shift, '' );
for( split //, substr $in, 0, (my $len = length $in) >> 1) {
my @range = ( my $str = my $end = $start .= $_ );
- ($str .= ++$end) && push @range,$end while $len > length $str;
+ ($str .= ++$end) && push @range,$end while ($len > length $str) &&
+ $end eq substr $in,length($str)-length($end),length($end);
return \@range if $str eq $in;
}
return [$in];
}
-