aboutsummaryrefslogtreecommitdiff
path: root/challenge-116
diff options
context:
space:
mode:
authordrbaggy <js5@sanger.ac.uk>2021-06-07 18:20:16 +0100
committerdrbaggy <js5@sanger.ac.uk>2021-06-07 18:20:16 +0100
commitd172cadef5609efeb80afef11a4df66ce0d24144 (patch)
tree8ff3828a102c0481506dc4d137b6b9180fb4e658 /challenge-116
parentd947cc6a0a440d4e1dae73b3747d9ee02b44d9b1 (diff)
downloadperlweeklychallenge-club-d172cadef5609efeb80afef11a4df66ce0d24144.tar.gz
perlweeklychallenge-club-d172cadef5609efeb80afef11a4df66ce0d24144.tar.bz2
perlweeklychallenge-club-d172cadef5609efeb80afef11a4df66ce0d24144.zip
pushed some code to make this now work for larger numbers {resolved issue with range operator failing} + added some examples to prove this!
Diffstat (limited to 'challenge-116')
-rw-r--r--challenge-116/james-smith/perl/ch-1.pl46
1 files changed, 31 insertions, 15 deletions
diff --git a/challenge-116/james-smith/perl/ch-1.pl b/challenge-116/james-smith/perl/ch-1.pl
index 1bbc1bd3a4..ffc9471780 100644
--- a/challenge-116/james-smith/perl/ch-1.pl
+++ b/challenge-116/james-smith/perl/ch-1.pl
@@ -5,47 +5,63 @@ 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;
+my $lint_x = my $lint = ('9' x 999).'8';
+my $lint_y = ++$lint_x;
+$lint_y++;
my @tests = (
[ 1234, '1,2,3,4' ],
[ 91011, '9,10,11' ],
[ 10203, '10203' ],
- [ $nspace, $ncomma ],
+ ## Numerical comparisons don't work....
+ [ $nspace, $ncomma ], ## 2895 digit no -> 1..1000
+ ## Have to store range as can't use range operator here...
+ [ $lint.$lint_x.$lint_y, $lint.','.$lint_x.','.$lint_y ], ## 3001 digit no -> 2 x 1000 digit + 1001 digit no.
+ [ $lint.$lint_x, $lint.','.$lint_x ], ## 2000 digit no -> 2 x 1000 digit
+ [ $lint_x.$lint_y, $lint_x.','.$lint_y ], ## 2001 digit no -> 1000 digit + 1001 digit no.
+ [ $nspace.'99', $nspace.'99' ], ## 2895 digit no -> 2895 digit no
);
-is( join(',',@{splitnum($_->[0])}),$_->[1] ) foreach @tests;
is( join(',',@{splitnum_no_comments($_->[0])}),$_->[1] ) foreach @tests;
+is( join(',',@{splitnum($_->[0])}),$_->[1] ) foreach @tests;
done_testing();
+cmpthese( 200, {
+ 'f' => sub { splitnum( $_->[0]) foreach @tests },
+ 'h' => sub { splitnum_no_comments($_->[0]) foreach @tests },
+});
+
sub splitnum {
my( $in, $start ) = ( shift, '' );
- foreach( split //, $in ) {
+ #for( split //, substr $in, 0, (length $in) >> 1) {
+ for( split //, $in ) {
## $start contains the first number of sequence
## each time through the loop we will add the
- ## next digit eg 1, 12, 123, 124
+ ## next digit eg 1, 12, 123
##
## $end contains a copy of this, which we will
## then incremement as we generate the sum
- my $string = my $end = $start .= $_;
+ my @range = ( my $string = my $end = $start .= $_ );
## We concatenate the of "end" onto $string until
## it is equal to or larger than the input number
- $string .= ++$end while $in gt $string
- && length $in > length $string;
+ ($string .= ++$end) && push @range, $end
+ while $in gt $string && length $in > length $string;
## Finally we return the list if the input and
## string are the same. Note we will always get
## a true value as in the last loop
## $in == $start == $end == $string
- return [$start..$end] if $string eq $in;
-
+ return \@range if $string eq $in;
}
+ return [$in];
}
@@ -56,15 +72,15 @@ 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 commends removed
sub splitnum_no_comments {
my( $in, $st ) = ( shift, '' );
- for( split //, $in ) {
- my $t = my $en = $st .= $_;
- $t .= ++$en while $in gt $t
- && length $in > length $t;
- return [ $st .. $en ] if $t eq $in;
+ for( split //, substr $in, 0, ( length $in) >> 1) {
+ my @r = ( my $t = my $en = $st .= $_ );
+ ($t .= ++$en) && push @r,$en while $in gt $t
+ && length $in > length $t;
+ return \@r if $t eq $in;
}
+ return [$in];
}