Code Golf: Numeric equivalent of an Excel column name

ForumBot
Messages : 26117
Inscription : mer. avr. 22, 2026 5:33 pm

Code Golf: Numeric equivalent of an Excel column name

Message par ForumBot »

The challenge


The shortest code by character count that will output the numeric equivalent of an Excel column string.


For example, the A column is 1, B is 2, so on and so forth. Once you hit Z, the next column becomes AA, then AB and so on.


Test cases:


A:    1
B: 2
AD: 30
ABC: 731
WTF: 16074
ROFL: 326676


Code count includes input/output (i.e full program).




Source : Stack Overflow

ayi
Site Admin
Messages : 13176
Inscription : mer. avr. 22, 2026 5:21 pm

Re: Code Golf: Numeric equivalent of an Excel column name

Message par ayi »

Perl, 36 34 33 31 30 17 15 11 characters


$_=()=A..$_


Usage:


$ echo -n WTF | perl -ple '$_=()=A..$_'
16074


Reduced to 17 by using echo -n to avoid a chop call.


Reduced to 15 by using say instead of print.


Reduced to 11 by using -p instead of say.


Explanation:

A is evaluated in string context and A..$_ builds a list starting at “A” and string-incrementing up to the input string. Perl interprets the ++ operator (and thus ..) on strings in an alphabetic context, so for example $_="AZ";$_++;print outputs BA.


=()= (aka “goatse” operator) forces an expression to be evaluated in list context, and returns the number of elements returned by that expression i.e., $scalar = () = <expr> corresponds to @list = <expr>; $scalar = @list.

Répondre

Revenir à « Excel & VBA »