18
\$\begingroup\$

In Excel, the columns range from A-Z, AA,AB,AZ,BA,..,BZ and so on. They actually each stand for numbers, but rather are encoded as alphabet strings.

In this challenge, you will be given a string of alphabets, and you must calculate the column it corresponds to.

Some tests:

'A' returns 1 (meaning that it is the first column)

'B' returns 2

'Z' returns 26

'AA' returns 27

'AB' returns 28

'AZ' returns 52

'ZZ' returns 702

'AAA' returns 703

You can assume that capital letters will be given only.

Shortest bytes win.

Good luck!

\$\endgroup\$
8
  • \$\begingroup\$ So... base 26 with the alphabet? \$\endgroup\$
    – Jo King
    Oct 20, 2018 at 1:55
  • 1
    \$\begingroup\$ It isn't quite base 26 because there's no zero. \$\endgroup\$
    – J.Doe
    Oct 20, 2018 at 2:03
  • \$\begingroup\$ @J.Doe Ah, I guess you're right. I didn't notice since my solution automatically treated Z as 10 anyway \$\endgroup\$
    – Jo King
    Oct 20, 2018 at 2:56
  • \$\begingroup\$ Reverse challenge. \$\endgroup\$
    – DELETE_ME
    Oct 20, 2018 at 3:01
  • 6
    \$\begingroup\$ @JoKing Bijective base. \$\endgroup\$
    – DELETE_ME
    Oct 20, 2018 at 3:03

28 Answers 28

9
\$\begingroup\$

Perl 6, 17 bytes

{:26[.ords X-64]}

Try it online!

Anonymous code block that subtracts 64 from each byte value and converts from base 26 with Z overflowing to the next column.

\$\endgroup\$
8
\$\begingroup\$

Google Sheets, 21 bytes

(formula evaluates to the result, takes input from cell A1)

=column(indirect(A1&2
\$\endgroup\$
2
  • \$\begingroup\$ Just about to post a slightly less golfed version of this. \$\endgroup\$
    – ATaco
    Oct 20, 2018 at 6:38
  • 1
    \$\begingroup\$ I also have a solution in Google Sheets that doesn't rely on builtin COLUMN, check it out. (besides, I feel bad that the solution I put more effort on gets less attention... it's a typical problem with voting anyway, especially when the challenge is on HNQ.) \$\endgroup\$
    – DELETE_ME
    Oct 22, 2018 at 0:13
6
\$\begingroup\$

R, 48 43 bytes

-5 bytes thanks to @Giuseppe, using the same logic, but as a program that eliminates the nchar call.

for(i in utf8ToInt(scan(,"")))F=F*26+i-64;F

Try it online!

\$\endgroup\$
0
5
\$\begingroup\$

Java (JDK), 39 bytes

s->s.chars().reduce(0,(a,b)->a*26+b%32)

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ Which java platform supports this ? \$\endgroup\$ Oct 21, 2018 at 18:26
  • \$\begingroup\$ @SyedHamzaHassan Java 8 or more. \$\endgroup\$ Oct 21, 2018 at 18:34
3
\$\begingroup\$

Python 2, 52 45 bytes

t=0
for c in input():t=26*t+ord(c)%64
print t

Try it online!

\$\endgroup\$
0
3
\$\begingroup\$

05AB1E, 6 bytes

Çžx-₂β

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ Out of curiosity, why use žx instead of just 64? \$\endgroup\$ Oct 21, 2018 at 17:13
  • \$\begingroup\$ I don't know, it looked nicer I guess? \$\endgroup\$
    – Okx
    Oct 21, 2018 at 21:44
3
\$\begingroup\$

Haskell, 38 34 31 bytes

foldl(\o->(o*26-64+).fromEnum)0

Try it online!

\$\endgroup\$
3
\$\begingroup\$

PHP, 41 38 bytes

-3 thanks to Jo King.

for($c=A;$c!=$argn;$i++)$c++;echo$i+1;

run as pipe with -nr

unary output, 34 bytes:

1<?for($c=A;$c!=$argn;$c++)echo 1;

requires PHP 7.1. save to file, run as pipe with -nF.

\$\endgroup\$
2
2
\$\begingroup\$

Jelly, 7 bytes

ØAiⱮḅ26

Try it online!

\$\endgroup\$
2
\$\begingroup\$

APL(NARS), 11 chars, 22 bytes

{+/26⊥⎕A⍳⍵}

test

  f←{+/26⊥⎕A⍳⍵} 
  f¨'A' 'AA' 'AAA'
1 27 703 
  f¨'AB' 'ZZ' 'Z'
28 702 26 
\$\endgroup\$
2
\$\begingroup\$

J, 11 bytes

26#.64|3&u:

Try it online!

How it works

26#.64|3&u:  Monadic verb. Input: a string.
       3&u:  Convert each character to Unicode codepoint
    64|      Modulo 64; maps A -> 1, ... Z -> 26
26#.         Interpret as base-26 digits and convert to single integer
\$\endgroup\$
1
\$\begingroup\$

JavaScript (Node.js), 48 bytes

f=([h,...t],p=0)=>h?f(t,p*26+parseInt(h,36)-9):p

Try it online!

\$\endgroup\$
2
1
\$\begingroup\$

APL (Dyalog Classic), 11 bytes

⎕A∘⍳⊥⍨26⍴⍨≢

Try it online!

\$\endgroup\$
1
\$\begingroup\$

J, 20 bytes

[:(#.~26$~#)32|a.i.]

Try it online!

Explanation:

 [:(#.~26$~#)32|a.i.] 
                  i.    - indices 
                    ]   - of the characters of the input
                a.      - in the alphabet
             32|        - mod 32
 [:(        )           - apply the following code to the above
         $~             - create a list of (left and right arguments exchanged) 
       26               - the number 26
           #            - repeated the length of the input times
    #.~                 - to base (26)
\$\endgroup\$
1
\$\begingroup\$

Google Sheets, 100 bytes

(formula evaluates to the result, takes input from cell A1)

=sum(arrayformula(
  (
    code(
      mid(A1,row(indirect("1:"&len(A1))),1)
    )-64
  )*26^row(indirect("1:"&len(A1)))/26

All spaces are added for clarity only.

Note.

  • I don't know if it's possible to remove the duplication of row(indirect("1:"&len(A1)).
  • Although Google Sheets has a decimal function, the transliteration would takes a lot of bytes.
\$\endgroup\$
1
\$\begingroup\$

APL+WIN, 12 bytes

Index origin 1.

26⊥¯65+⎕av⍳⎕

Try it online! Courtesy of Dyalog Classic

Explanation:

⎕av⍳⎕ Prompts for input and gets Ascii integer value for each character

¯65+ subtracts 65 to give integers 1-26 for A-Z

26⊥ converts resulting vector from base 26 to single integer
\$\endgroup\$
1
\$\begingroup\$

Charcoal, 10 bytes

I↨²⁶ES⊕⌕αι

Try it online! Link is to verbose version of code. Explanation:

     S      Input string
    E       Map over characters
         ι  Current character
        α   Uppercase alphabet
       ⌕    Find index
      ⊕     Increment
  ²⁶        Literal 26
 ↨          Base conversion
I           Cast to string
            Implicitly print
\$\endgroup\$
1
\$\begingroup\$

Java (JDK), 92 bytes

static int m(String v){int x=0;for(int i=0;i<v.length();i++)x=x*26+v.charAt(i)-64;return x;}

Try it online!

Output

A=1

B=2

Z=26

AA=27

AB=28

AZ=52

ZZ=702

AAA=703

\$\endgroup\$
4
  • \$\begingroup\$ I'm not an expert at golfing Java, but you can golf this down considerably by returning instead of printing, simplifying the for loops, removing whitespace and getting rid of the p and n variables. 92 bytes!. \$\endgroup\$
    – Jo King
    Oct 20, 2018 at 14:22
  • \$\begingroup\$ Wonderful....... \$\endgroup\$ Oct 20, 2018 at 14:26
  • 1
    \$\begingroup\$ You can remove static to gain 7 bytes. You could also make this function a lambda to spare more bytes. I also think that the recursive version might save bytes. In any case, here's my 39 bytes solution. \$\endgroup\$ Oct 20, 2018 at 21:36
  • \$\begingroup\$ That's Wonderful. \$\endgroup\$ Oct 21, 2018 at 14:03
1
\$\begingroup\$

MATL, 11 bytes

0w"26*@+64-

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Kotlin, 36 bytes

{it.fold(0){o,n->o*26+n.toInt()-64}}

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Ruby, 18 bytes

->s{[*?A..s].size}

Try it online!

\$\endgroup\$
1
\$\begingroup\$

C (gcc), 46, 43 bytes

a;f(int*s){for(a=0;*s;)a=*s++%64+a*26;s=a;}

Try it online!

Degolf

a; f(int*s)
{  for(a=0;*s;) // Loop through s, which is a null-terminated string.
       a=*s++%64 + a*26; // Multiply accumulated value by 26, and add current char modulo 64 to it.
   s=a;} // Return the accumulated value.
\$\endgroup\$
0
1
\$\begingroup\$

Ahead, 22 bytes

>jvi'@-\26l
^~>O@ ~+*<

Try it online!

\$\endgroup\$
1
\$\begingroup\$

MBASIC, 84 bytes

1 INPUT S$:L=LEN(S$):FOR I=1 TO L:V=ASC(MID$(S$,I,1))-64:T=T+26^(L-I)*V:NEXT:PRINT T

Output:

? AZ
 52

? ZZ
 702

? AAA
 703
\$\endgroup\$
1
\$\begingroup\$

x86 machine code, 19 bytes

00000000: 31c0 8b19 83e3 3f41 b21a f7e2 01d8 3831  1.....?A......81
00000010: 75f0 c3                                  u..

Assembly:

section .text
	global func
func:				;this function uses fastcall conventions
	xor eax, eax		;reset eax to 0
	loop:
		;ebx=*ecx%64
		mov ebx, [ecx]	;ecx is 1st arg to this func (in fastcall conventions)
		and ebx, 63	;because 64 is a pwr of 2,n%64=n&(64-1)

		;ecx++		get next char in str by incrementing ptr
		inc ecx
		
		;eax=eax*26
		mov dl, 26	;using an 8bit reg is less bytes
		mul edx
		
		;eax+=ebx //(eax=(*ecx%64)+(eax*26))
		add eax, ebx

		;if(*ecx!='\0')goto loop
		cmp byte [ecx], dh ;dh==0
		jne loop
	ret			;return value is in eax

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Japt -h, 10 bytes

åÈ*26+InYc

Try it

Or without a flag. The first byte can be removed if we can take input as a character array.

¨c aI̓26

Try it


Explanation

åÈ             :Cumulatively reduce by passing each character at Y through a function, with an initial total of 0
  *26          :  Multiply current total by 26
     -I        :  Subtract 64
       n       :   Subtracted from
        Yc     :    The codepoint of Y
               :Implicitly output the last element of the resulting array
\$\endgroup\$
1
\$\begingroup\$

Kotlin, 29 bytes

{it.fold(0){a,v->v-'@'+a*26}}

Try it online!

Explained

val column: (String) -> Int = {  // String in, Int out
    it.fold(0) { a, v ->  // acc, value
        v - '@'  // distance of char from @ (A=1 etc.)
                + a * 26
    }
}
\$\endgroup\$
0
\$\begingroup\$

Ruby -nl, 39 bytes

p$_.chars.reduce(0){|x,y|26*x+y.ord-64}

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.