รับสตริงคำนวณจำนวนคอลัมน์ที่สอดคล้องกับ


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 โดยอัตโนมัติ
โจคิง

ความท้าทายย้อนกลับ
user202729

6
@JoKing ฐาน bijective
user202729

คำตอบ:



7

Google ชีตขนาด 21 ไบต์

(สูตรจะประเมินผลลัพธ์จะรับข้อมูลจากเซลล์ A1)

=column(indirect(A1&2

เพิ่งจะโพสต์เวอร์ชั่นนี้น้อยกว่ากอล์ฟ
ATaco

1
ฉันมีโซลูชันใน Google ชีตที่ไม่พึ่งพา COLUMN ในตัวลองดูสิ (นอกจากนี้ฉันรู้สึกแย่ที่โซลูชันที่ฉันใช้ความพยายามมากขึ้นได้รับความสนใจน้อยลง ... มันเป็นปัญหาทั่วไปของการลงคะแนนอย่างไรก็ตามโดยเฉพาะอย่างยิ่งเมื่อความท้าทายอยู่ที่ HNQ)
user202729






3

PHP, 41 38 ไบต์

-3 ขอบคุณโจคิง

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

ทำงานเป็นท่อด้วย -nr

เอาต์พุต unary, 34 ไบต์:

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

ต้องใช้ PHP 7.1 บันทึกไปยังแฟ้ม, -nFการทำงานเป็นท่อด้วย


@ โจกิ้งใช่มันสามารถทำได้: sandbox.onlinephpfunctions.com/code/…
Ismael Miguel


@Titus Alright แล้ว


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

ลองออนไลน์!

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

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 แต่คุณสามารถตีลงไปได้มากโดยการกลับมาแทนที่จะพิมพ์ทำให้ลูปง่ายขึ้นลดช่องว่างและกำจัดpและnตัวแปร 92 ไบต์! .
Jo King

วิเศษมาก .......
Syed Hamza Hassan

1
คุณสามารถลบออกstaticเพื่อรับ 7 ไบต์ คุณสามารถทำให้ฟังก์ชั่นนี้เป็นแลมบ์ดาเพื่อสำรองไบต์ได้มากขึ้น ฉันยังคิดว่ารุ่นที่เรียกซ้ำอาจบันทึกไบต์ ในกรณีใด ๆ นี่เป็นวิธีการแก้ปัญหา 39 ไบต์ของฉัน
Olivier Grégoire

นั่นช่างวิเศษสุด ๆ.
Syed Hamza Hassan





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

รหัสเครื่อง 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

ลองออนไลน์!


โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.