# Given a string, calculate the number of the column it corresponds to

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!

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

# 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.

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

=column(indirect(A1&2

• Just about to post a slightly less golfed version of this. – ATaco Oct 20 '18 at 6:38
• 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.) – user202729 Oct 22 '18 at 0:13

# 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!

# Java (JDK), 39 bytes

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


Try it online!

• Which java platform supports this ? – Syed Hamza Hassan Oct 21 '18 at 18:26
• @SyedHamzaHassan Java 8 or more. – Olivier Grégoire Oct 21 '18 at 18:34

# Python 2, 52 45 bytes

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


Try it online!

# 05AB1E, 6 bytes

Çžx-₂β


Try it online!

• Out of curiosity, why use žx instead of just 64? – Kevin Cruijssen Oct 21 '18 at 17:13
• I don't know, it looked nicer I guess? – Okx Oct 21 '18 at 21:44

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


Try it online!

# 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.

# Jelly, 7 bytes

ØAiⱮḅ26


Try it online!

# 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


# 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.


# JavaScript (Node.js), 48 bytes

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


Try it online!

(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.

# 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


# 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

• 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!. – Jo King Oct 20 '18 at 14:22
• Wonderful....... – Syed Hamza Hassan Oct 20 '18 at 14:26
• 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. – Olivier Grégoire Oct 20 '18 at 21:36
• That's Wonderful. – Syed Hamza Hassan Oct 21 '18 at 14:03

# MATL, 11 bytes

0w"26*@+64-


Try it online!

# Kotlin, 36 bytes

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


Try it online!

# Ruby, 18 bytes

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


Try it online!

# 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


# 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


p$_.chars.reduce(0){|x,y|26*x+y.ord-64}  Try it online! # APL (Dyalog Classic), 11 bytes ⎕A∘⍳⊥⍨26⍴⍨≢  Try it online! # 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)


# Charcoal, 10 bytes

Ｉ↨²⁶ＥＳ⊕⌕αι


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

     Ｓ      Input string
Ｅ       Map over characters
ι  Current character
α   Uppercase alphabet
⌕    Find index
⊕     Increment
²⁶        Literal 26
↨          Base conversion
Ｉ           Cast to string
Implicitly print


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


Try it online!

## 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


# 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))

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


Try it online!

# 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
}
}