in reply to Shortest string containing all from 0000 to 9999 challenge.

By the way, you can improve your analyze function quite a bit. This piece in particular stood out to me:

for ( 0 .. 9999 ) { my $k = 0 x ( 4 - length $_ ) . $_;
Even if you had to use a for loop, you could have just written it as for ( '0000' .. '9999' ) instead. Isn't Perl great? You don't even need the for though. I wrote my analyze function like this:
sub analyze { my $number = shift; my $length = length $number; my $duplicates = 0; my %seen; while (length $number >= 4) { $seen{substr($number, -4)}++ and $duplicates++; substr($number, -1, 1, ''); } my $missing = 10000 - keys %seen; return ($length, $missing, $duplicates) }

-sauoq
"My two cents aren't worth a dime.";

Replies are listed 'Best First'.
Re: Re: Shortest string containing all from 0000 to 9999 challenge.
by EvdB (Deacon) on May 22, 2003 at 11:48 UTC
    When I started the problem I used alot of arrays - which carried over into my analysis code.

    Yours is slicker and a good demonstration of the substr function. Just to prove it here are the benchmarks:

    Rate EvdB saouq EvdB 7.89/s -- -67% saouq 23.8/s 202% --

    --tidiness is the memory loss of environmental mnemonics