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

17

1

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!

K Split X

Posted 2018-10-20T01:39:34.900

Reputation: 989

So... base 26 with the alphabet? – Jo King – 2018-10-20T01:55:29.947

1It isn't quite base 26 because there's no zero. – J.Doe – 2018-10-20T02:03:56.010

@J.Doe Ah, I guess you're right. I didn't notice since my solution automatically treated Z as 10 anyway – Jo King – 2018-10-20T02:56:22.710

Reverse challenge. – user202729 – 2018-10-20T03:01:31.730

6

@JoKing Bijective base.

– user202729 – 2018-10-20T03:03:01.650

I wonder if someone will come up with a solution in Excel. – Titus – 2018-10-20T04:46:42.477

Can we take input as an array/list of characters? – Shaggy – 2018-10-20T11:14:04.797

@Titus I don't have Excel or know it... but isn't there a COLUMN function that would do this? – Quintec – 2018-10-20T23:03:51.237

Answers

9

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.

Jo King

Posted 2018-10-20T01:39:34.900

Reputation: 38 234

7

Google Sheets, 21 bytes

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

=column(indirect(A1&2

user202729

Posted 2018-10-20T01:39:34.900

Reputation: 14 620

Just about to post a slightly less golfed version of this. – ATaco – 2018-10-20T06:38:12.210

1I 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 – 2018-10-22T00:13:01.937

6

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!

J.Doe

Posted 2018-10-20T01:39:34.900

Reputation: 2 379

4

Java (JDK), 39 bytes

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

Try it online!

Olivier Grégoire

Posted 2018-10-20T01:39:34.900

Reputation: 10 647

Which java platform supports this ? – Syed Hamza Hassan – 2018-10-21T18:26:33.677

@SyedHamzaHassan Java 8 or more. – Olivier Grégoire – 2018-10-21T18:34:32.130

3

Python 2, 52 45 bytes

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

Try it online!

Chas Brown

Posted 2018-10-20T01:39:34.900

Reputation: 8 959

3

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.

Titus

Posted 2018-10-20T01:39:34.900

Reputation: 13 814

@JoKing Yes, it can be done: http://sandbox.onlinephpfunctions.com/code/5669c9f56e94cf26ff478622abf640cbb24844a1

– Ismael Miguel – 2018-10-21T14:50:10.223

@Titus Alright then – None – 2018-10-23T10:38:02.337

3

05AB1E, 6 bytes

Çžx-₂β

Try it online!

Okx

Posted 2018-10-20T01:39:34.900

Reputation: 15 025

Out of curiosity, why use žx instead of just 64? – Kevin Cruijssen – 2018-10-21T17:13:27.133

I don't know, it looked nicer I guess? – Okx – 2018-10-21T21:44:57.460

3

Haskell, 38 34 31 bytes

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

Try it online!

ovs

Posted 2018-10-20T01:39:34.900

Reputation: 21 408

2

Jelly, 7 bytes

ØAiⱮḅ26

Try it online!

Erik the Outgolfer

Posted 2018-10-20T01:39:34.900

Reputation: 38 134

2

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.

user77406

Posted 2018-10-20T01:39:34.900

Reputation:

2

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 

RosLuP

Posted 2018-10-20T01:39:34.900

Reputation: 3 036

1

JavaScript (Node.js), 48 bytes

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

Try it online!

tsh

Posted 2018-10-20T01:39:34.900

Reputation: 13 072

1.map() is 1 byte shorter. Using Buffer() saves another byte. – Arnauld – 2018-10-20T12:47:51.930

38 bytes, expanding on @Arnauld's suggestions. – Shaggy – 2018-10-20T14:19:42.163

1

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

Syed Hamza Hassan

Posted 2018-10-20T01:39:34.900

Reputation: 129

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 – 2018-10-20T14:22:08.023

Wonderful....... – Syed Hamza Hassan – 2018-10-20T14:26:26.610

1

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 – 2018-10-20T21:36:04.177

That's Wonderful. – Syed Hamza Hassan – 2018-10-21T14:03:28.673

1

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.

user202729

Posted 2018-10-20T01:39:34.900

Reputation: 14 620

1

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

Graham

Posted 2018-10-20T01:39:34.900

Reputation: 3 184

1

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

Shaggy

Posted 2018-10-20T01:39:34.900

Reputation: 24 623

1

MATL, 11 bytes

0w"26*@+64-

Try it online!

Giuseppe

Posted 2018-10-20T01:39:34.900

Reputation: 21 077

1

Kotlin, 36 bytes

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

Try it online!

ovs

Posted 2018-10-20T01:39:34.900

Reputation: 21 408

1

Ruby, 18 bytes

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

Try it online!

G B

Posted 2018-10-20T01:39:34.900

Reputation: 11 099

1

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

Bubbler

Posted 2018-10-20T01:39:34.900

Reputation: 16 616

0

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)

Galen Ivanov

Posted 2018-10-20T01:39:34.900

Reputation: 13 815

0

Ruby -nl, 39 bytes

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

Try it online!

Kirill L.

Posted 2018-10-20T01:39:34.900

Reputation: 6 693

0

APL (Dyalog Classic), 11 bytes

⎕A∘⍳⊥⍨26⍴⍨≢

Try it online!

Galen Ivanov

Posted 2018-10-20T01:39:34.900

Reputation: 13 815

0

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

Neil

Posted 2018-10-20T01:39:34.900

Reputation: 95 035

0

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!

Logern

Posted 2018-10-20T01:39:34.900

Reputation: 845

0

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

snail_

Posted 2018-10-20T01:39:34.900

Reputation: 1 982

0

Ahead, 22 bytes

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

Try it online!

snail_

Posted 2018-10-20T01:39:34.900

Reputation: 1 982

0

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

wooshinyobject

Posted 2018-10-20T01:39:34.900

Reputation: 171