Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

(tye)Re: Base36 numbers: speed and golf

by tye (Cardinal)
on Feb 27, 2002 at 22:04 UTC ( #148054=note: print w/ replies, xml ) Need Help??


in reply to Base36 numbers: speed and golf

Of course, I can't say whether this is the fastest but it was designed to be fast.

#!/usr/bin/perl -w use strict; my %next; BEGIN { @next{'0'..'9','A'..'Z'}= ('1'..'9','A'..'Z','0'); } sub inc36 { my $add= ""; my $dig; while( not $dig= $next{chop($_[0])} ) { $add .= $dig; if( ! length($_[0]) ) { $dig= '1'; last; } } $_[0] .= $dig . $add; } while( <DATA> ) { for( split " " ) { print "$_ + 1 = "; inc36( $_ ); print $_,$/; } } __END__ 0 1 9 A Y Z 10 19 1Z 9Z AZ ZZ YZZ XYZZY XYZZZ
which outputs:
0 + 1 = 1
1 + 1 = 2
9 + 1 = A
A + 1 = B
Y + 1 = Z
Z + 1 = 10
10 + 1 = 11
19 + 1 = 1A
1Z + 1 = 20
9Z + 1 = A0
AZ + 1 = B0
ZZ + 1 = 100
YZZ + 1 = Z00
XYZZY + 1 = XYZZZ
XYZZZ + 1 = XZ000

        - tye (but my friends call me "Tye")


Comment on (tye)Re: Base36 numbers: speed and golf
Download Code
Re: (tye)Re: Base36 numbers: speed and golf
by jmcnamara (Monsignor) on Feb 28, 2002 at 11:16 UTC

    Well done tye. This is an elegant solution and the fastest by a mile.

    There is a lot of good code produced here at PerlMonks but I wonder how much of it is actually studied or even looked at. At the time of writing two other solutions that contain bugs have higher reps than this node.

    --
    John.

Re: (tye)Re: Base36 numbers: speed and golf
by demerphq (Chancellor) on Feb 28, 2002 at 16:31 UTC
    Well, this was interesting. I wrote a version of your code that used an array and ord instead of the hash. It was 10% slower in my tests which quite suprised me. I really thought the difference in lookup time between an array and a hash would outway the cost of the ord function, but not so.

    Thanks, very educational.

    Yves / DeMerphq
    --
    When to use Prototypes?
    Advanced Sorting - GRT - Guttman Rosler Transform

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://148054]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (14)
As of 2014-09-18 16:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (118 votes), past polls