diff options
| author | drbaggy <js5@sanger.ac.uk> | 2021-05-29 13:06:35 +0100 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2021-05-29 13:06:35 +0100 |
| commit | 991442d1831537932ecb88e1c9fe46c78290d128 (patch) | |
| tree | 3f05aacebbe653bd402914613dcac11d58728a6c | |
| parent | eef2c980234adf95b33126a420bdfbadc05ee84c (diff) | |
| parent | cc328f7fe85723fe72a0fa391575cefa4dbe44d8 (diff) | |
| download | perlweeklychallenge-club-991442d1831537932ecb88e1c9fe46c78290d128.tar.gz perlweeklychallenge-club-991442d1831537932ecb88e1c9fe46c78290d128.tar.bz2 perlweeklychallenge-club-991442d1831537932ecb88e1c9fe46c78290d128.zip | |
Merge branch 'master' of https://github.com/drbaggy/perlweeklychallenge-club
| -rw-r--r-- | challenge-114/james-smith/perl/ch-1.pl | 48 |
1 files changed, 29 insertions, 19 deletions
diff --git a/challenge-114/james-smith/perl/ch-1.pl b/challenge-114/james-smith/perl/ch-1.pl index be0c22adc6..8303864dbd 100644 --- a/challenge-114/james-smith/perl/ch-1.pl +++ b/challenge-114/james-smith/perl/ch-1.pl @@ -11,16 +11,28 @@ my @tests = ([1,2],[9,11],[99,101],[989,999],[999,1001],[10,11],[11,22],[12,22], [111222,112211]); #@tests = map { chomp; [split] } <>; -#is( next_palindrome( $_->[0] ), $_->[1] ) foreach @tests; -#is( next_palindrome_naive( $_->[0] ), $_->[1] ) foreach @tests; +is( next_palindrome( $_->[0] ), $_->[1] ) foreach @tests; +is( next_palindrome_naive( $_->[0] ), $_->[1] ) foreach @tests; done_testing(); use Benchmark qw{ cmpthese }; -cmpthese(2000, { - slow => sub { next_palindrome_naive($_) for 99699..100400 }, - fast => sub { next_palindrome($_) for 99699..100400 }, -}); +my @ranges = ( + [ 100_000, 1, 101 ], + [ 50_000, 950, 1050 ], + [ 20_000, 9950, 10050 ], + [ 10_000, 99950, 100050 ], + [ 5_000, 999950, 1000050 ], + [ 1_000, 9999950, 10000050 ], + [ 1_000, 99999950, 100000050 ], +); + +foreach my $r (@ranges) { + cmpthese($r->[0], { + slow => sub { next_palindrome_naive($_) for $r->[1] .. $r->[2] }, + fast => sub { next_palindrome($_) for $r->[1] .. $r->[2] }, + }); +} sub next_palindrome_naive { my ($n) = @_; @@ -29,8 +41,8 @@ sub next_palindrome_naive { } sub next_palindrome { - my $p = 1 + (my $n = shift); - my $x = substr $p, 0, (length $p)>>1; $x||=''; + my $p = 1 + shift; + my $x = substr $p, 0, (length $p)>>1; if( 1 & length $p ) { ## Odd length so we have three options... ## new number by reversing the first half of the number > $n so OK! @@ -41,19 +53,17 @@ sub next_palindrome { ## then 999.9.999 > 999.9.998 and so we don't get this... so we ## are safe with this approach... my $y = substr $p, (length$p)>>1, 1; - return $x.$y.reverse $x if $n < $x.$y.reverse $x; + return $x.$y.reverse $x if $p <= $x.$y.reverse $x; return $x.($y+1).reverse $x if $y<9; - $x++; - return $x.'0'.reverse $x; - } else { - ## Even no of digits.. - ## If $n >= $x.reverse $x we incrememnt $x; - ## The only time that this could lead to a longer number is when - ## $x is 999 and is OK as $x.reverse $x will always be larger than - ## $n.... - $x++ if $n >= $x.reverse $x; - return $x.reverse $x; + return ++$x.'0'.reverse $x; } + ## Even no of digits.. + ## If $n >= $x.reverse $x we incrememnt $x; + ## The only time that this could lead to a longer number is when + ## $x is 999 and is OK as $x.reverse $x will always be larger than + ## $n.... + return $x.reverse $x if $p <= $x.reverse $x; + return ++$x.reverse $x; } |
