给定一个字符串,计算它对应的列数


17

在Excel中,列的范围为A-Z, AA,AB,AZ,BA,..,BZ依此类推。它们实际上每个代表数字,而是编码为字母字符串。

在此挑战中,将为您提供一串字母,并且您必须计算其对应的列。

一些测试:

“ A”返回1(表示它是第一列)

'B'返回2

'Z'返回26

'AA'返回27

'AB'返回28

'AZ'返回52

'ZZ'返回702

'AAA'返回703

您可以假设只给出大写字母。

最短的字节获胜。

祝好运!


所以...以26为底的字母?
Jo King

1
它不是以26为底,因为没有零。
J.Doe

@ J.Doe Ah,我想你是对的。我没有注意到,因为我的解决方案Z无论如何都会自动视为10
Jo King


Answers:



7

Google表格,21个字节

(公式对结果求值,取自单元格A1的输入)

=column(indirect(A1&2

即将发布的高尔夫版本略少一些。
ATaco

1
我在Google表格中也有一个不依赖内置COLUMN的解决方案,请查看。(此外,我为我投入更多精力而无法解决的问题感到很沮丧……无论如何,这都是一个典型的问题,尤其是当挑战在HNQ上时。)
user202729 '18

6

[R 48 43字节

@Giuseppe使用-5字节,使用相同的逻辑,但是作为消除nchar调用的程序。

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

在线尝试!








2

APL(NARS),11个字符,22个字节

{+/26⊥⎕A⍳⍵}

测试

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

2

C(GCC) 46,43个字节

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

在线尝试!

德戈尔夫

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.


1

Google表格,100字节

(公式对结果求值,取自单元格A1的输入)

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

所有空格都是为了清楚起见而添加的。

注意

  • 我不知道是否可以删除的重复项row(indirect("1:"&len(A1))
  • 尽管Google表格具有decimal功能,但音译会占用大量字节。

1

APL + WIN,12个字节

索引原点1。

26⊥¯65+⎕av⍳⎕

在线尝试!由Dyalog Classic提供

说明:

⎕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

1

Java(JDK),92字节

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

在线尝试!

输出量

A = 1

B = 2

Z = 26

AA = 27

AB = 28

AZ = 52

ZZ = 702

AAA = 703


我不是打Java的专家,但是您可以通过返回而不是打印,简化for循环,删除空格并摆脱pand n变量来使它大打折扣。92个字节!
Jo King

太好了.......
Syed Hamza Hassan

1
您可以删除static以获取7个字节。您也可以将此函数设为lambda以节省更多字节。我也认为递归版本可以节省字节。无论如何,这是我的39个字节的解决方案
奥利维尔·格雷戈尔

那好极了。
Syed Hamza Hassan




1

J,11个字节

26#.64|3&u:

在线尝试!

怎么运行的

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

1

Japt -h,10字节

åÈ*26+InYc

尝试一下

还是没有标志。如果我们可以将输入作为字符数组,则可以删除第一个字节。

¨c aI̓26

尝试一下


说明

åÈ             :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



0

J,20个字节

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

在线尝试!

说明:

 [:(#.~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)

0

木炭,10字节

I↨²⁶ES⊕⌕αι

在线尝试!链接是详细版本的代码。说明:

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


0

MBASIC,84字节

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

输出:

? AZ
 52

? ZZ
 702

? AAA
 703

0

x86机器码,19个字节

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

部件:

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

在线尝试!


0

科特林,29个字节

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

在线尝试!

讲解

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
    }
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.