Data una stringa, calcola il numero della colonna a cui corrisponde


17

In Excel, le colonne vanno da A-Z, AA,AB,AZ,BA,..,BZe così via. Ognuno corrisponde a numeri, ma piuttosto sono codificati come stringhe dell'alfabeto.

In questa sfida, ti verrà data una stringa di alfabeti e dovrai calcolare la colonna a cui corrisponde.

Alcuni test:

'A' restituisce 1 (significa che è la prima colonna)

'B' restituisce 2

'Z' restituisce 26

'AA' restituisce 27

'AB' restituisce 28

'AZ' restituisce 52

'ZZ' restituisce 702

'AAA' restituisce 703

Puoi presumere che le lettere maiuscole saranno fornite solo.

Vincono i byte più brevi.

In bocca al lupo!


Quindi ... base 26 con l'alfabeto?
Jo King,

1
Non è abbastanza base 26 perché non c'è zero.
J.Doe,

@ J.Doe Ah, immagino tu abbia ragione. Non me ne sono accorto poiché la mia soluzione ha automaticamente considerato Zcomunque 10
Jo King,


Risposte:


9

Perl 6 , 17 byte

{:26[.ords X-64]}

Provalo online!

Blocco di codice anonimo che sottrae 64 da ciascun valore di byte e converte dalla base 26 con Zoverflow alla colonna successiva.


7

Fogli Google, 21 byte

(la formula viene valutata in base al risultato, accetta input dalla cella A1)

=column(indirect(A1&2

Sto per pubblicare una versione leggermente meno giocata di questo.
ATaco,

1
Ho anche una soluzione in Fogli Google che non si basa sulla COLONNA incorporata, dai un'occhiata. (Inoltre, mi sento male che la soluzione su cui mi dedico maggiormente attiri meno attenzione ... è comunque un problema tipico con il voto, specialmente quando la sfida è su HNQ.)
user202729

6

R , 48 43 byte

-5 byte grazie a @Giuseppe, usando la stessa logica, ma come un programma che elimina la ncharchiamata.

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

Provalo online!








2

APL (NARS), 11 caratteri, 22 byte

{+/26⊥⎕A⍳⍵}

test

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

2

C (gcc) , 46 , 43 byte

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

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


1

Fogli Google, 100 byte

(la formula viene valutata in base al risultato, accetta input dalla cella A1)

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

Tutti gli spazi vengono aggiunti solo per chiarezza.

Nota .

  • Non so se è possibile rimuovere la duplicazione di row(indirect("1:"&len(A1)).
  • Sebbene Google Sheets abbia una decimalfunzione, la traslitterazione richiederebbe molti byte.


1

Java (JDK) , 92 byte

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

Provalo online!

Produzione

A = 1

B = 2

Z = 26

AA = 27

AB = 28

AZ = 52

ZZ = 702

AAA = 703


Non sono un esperto del golf di Java, ma puoi giocare a golf considerevolmente ritornando invece di stampare, semplificando i cicli for, rimuovendo gli spazi bianchi e liberandoti delle variabili pe n. 92 byte! .
Jo King,

Meraviglioso .......
Syed Hamza Hassan,

1
È possibile rimuovere staticper ottenere 7 byte. Puoi anche rendere questa funzione una lambda per risparmiare più byte. Penso anche che la versione ricorsiva potrebbe salvare byte. In ogni caso, ecco la mia soluzione da 39 byte .
Olivier Grégoire,

È magnifico.
Syed Hamza Hassan,




1

J , 11 byte

26#.64|3&u:

Provalo online!

Come funziona

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 byte

åÈ*26+InYc

Provalo

O senza una bandiera. Il primo byte può essere rimosso se possiamo prendere l'input come una matrice di caratteri.

¨c aI̓26

Provalo


Spiegazione

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

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

Provalo online!

Spiegazione:

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

Carbone , 10 byte

I↨²⁶ES⊕⌕αι

Provalo online! Il collegamento è alla versione dettagliata del codice. Spiegazione:

     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 byte

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

Produzione:

? AZ
 52

? ZZ
 702

? AAA
 703

0

codice macchina x86, 19 byte

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

Montaggio:

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

Provalo online!


0

Kotlin , 29 byte

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

Provalo online!

spiegato

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
    }
}
Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.