องค์ประกอบของสตริงที่ดัชนีที่ระบุ


13

กำหนดสตริงsและจำนวนที่ไม่ใช่เชิงลบnน้อยกว่าความยาวของsการส่งออกของตัวละครที่ตำแหน่งของn -ths

0-indexing และ 1-indexing ได้รับอนุญาต สำหรับ 1 การจัดทำดัชนีจะเป็นบวกและน้อยกว่าหรือเท่ากับความยาวของns

s จะประกอบด้วยอักขระที่พิมพ์ได้เท่านั้น

อนุญาตอินพุต / เอาท์พุตที่สมเหตุสมผล ช่องโหว่มาตรฐานใช้

ทดสอบ (ดัชนี 0):

n s        output
0 "abcde"  a
1 "abcde"  b
2 "a != b" !
3 "+-*/"   /
4 "314159" 5

ทดสอบ (ดัชนี 1):

n s        output
1 "abcde"  a
2 "abcde"  b
3 "a != b" !
4 "+-*/"   /
5 "314159" 5

นี่คือดังนั้นคำตอบที่สั้นที่สุดในหน่วยไบต์ชนะ


13
ฉันลงคะแนนเพราะสิ่งนี้ไม่ใช่ความท้าทายในการเขียนโปรแกรมหรือเล่นกอล์ฟ สิ่งที่ถูกถามเป็นหลักที่นี่คือภาษาใดมีงานที่สั้นที่สุดสำหรับงาน
Shaggy

15
@Shaggy Same สำหรับความท้าทายที่เรียบง่ายอื่น ๆ อีกมากมายเช่นการเพิ่มตัวเลขสองตัวทดสอบว่าตัวเลขนั้นดีหรือพิมพ์ "Hello, World!" ในขณะที่สิ่งเหล่านี้น่าเบื่อในหลายภาษาที่สามารถทำให้พวกเขาออกนอกกรอบพวกเขาสามารถท้าทายที่น่าสนใจสำหรับภาษาดั้งเดิมมากขึ้นที่ต้องม้วนการใช้งานของตัวเอง นอกจากนี้สิ่งที่ซับซ้อนมากขึ้นมักจะถามภาษาเหล่านั้นมากเกินไปดังนั้นจึงเป็นเรื่องดีที่จะมีทางออกให้ หากความท้าทายเล็กน้อยทำให้คุณเบื่อลองทำมันด้วยภาษาที่ไม่สำคัญ
Martin Ender

คำตอบ:



7

อลิซ 5 ไบต์

I&IO@

ลองออนไลน์!

ตามปกติจะสั้นกว่ามากหากเราหลีกเลี่ยงโหมด Ordinal และใช้รูปแบบอินพุตสุดซึ้ง นี่เป็นครั้งแรกที่จุดรหัสของตัวละครจะถูกใช้เป็น input ส่วนที่เหลือของอินพุตเป็นสตริง ดัชนีเป็นแบบ 1

คำอธิบาย

I   Read a character and push its code point.
&I  Read that many more characters and push them.
O   Output the last one we read.
@   Terminate the program.

abysmal- ฉันคิดว่าคุณทำให้มัน xD
Stephen

@StephenS คุณหมายถึงฉันเสนอรูปแบบ I / O นี้ใน meta? ใช่ฉันทำ แต่ส่วนใหญ่สำหรับภาษาที่จะต้องใช้ตัวแยกวิเคราะห์ / จำนวนเต็มทศนิยมของตัวเองทุกครั้งที่พวกเขาแข่งขันในความท้าทายด้วย I / O ตัวเลขดังนั้นพวกเขาน่าจะข้ามพวกเขาทั้งหมด แต่มันมีผลข้างเคียงที่โชคร้ายในบางภาษาที่สามารถอ่านและเขียนทศนิยมได้ค่อนข้างง่ายมันยังสั้นกว่าที่จะใช้รหัสจุดแทน
Martin Ender

6

Python ขนาด 15 ไบต์

str.__getitem__

หรือ

lambda s,i:s[i]

ทั้งคู่รับอาร์กิวเมนต์ 2 ตัว: สตริงและดัชนี 0 การจัดทำดัชนี


ฉันประหลาดใจที่ทั้งสองวิธีมีความยาวเท่ากัน
Leun Nun

6

Haskell, 4 ไบต์

(!!)

การทำดัชนีแบบ 0 ตัวอย่างการใช้งาน: ->(!!) "Hello" 1'e'


5

ระดับเสียงคู่ 10 ไบต์

@(s,n)s(n)

ใช้เวลาสตริงsและจำนวนnเป็น input และส่งกลับnตัวอักษร TH sของ


5

Retina , 28 20 19 ไบต์

บันทึก 8 ไบต์ด้วย @MartinEnder โดยไม่ได้ใช้กลุ่มการปรับสมดุล

บันทึกแล้ว 1 ไบต์ขอบคุณ @ mbomb007 โดยใช้^.+แทน^\d+

^.+
$*
+`1¶.
¶
!1`.

ลองออนไลน์!

โปรแกรมถูกทำดัชนี 0


เปลี่ยน\dเป็น.บันทึกไบต์
mbomb007

@ mbomb007 ขอบคุณสำหรับเคล็ดลับ :)
user41805


3

อลิซ 10 ไบต์

/@!O?]&
 I

ลองออนไลน์!

คาดว่าสตริงในบรรทัดแรกและดัชนีที่ใช้ 0 ในบรรทัดที่สอง

คำอธิบาย

แม้จะมีความมั่งคั่งในตัว แต่การจัดทำดัชนีสตริงยังไม่มีอยู่ในอลิซ เหตุผลก็คือมันต้องมีทั้งจำนวนเต็มและพารามิเตอร์สตริงและคำสั่งทั้งหมดในอลิซเป็นจำนวนเต็มจำนวนเต็มหรือสตริงไปยังสตริงอย่างเคร่งครัด

โดยทั่วไปวิธีหลักในการดำเนินการใด ๆ กับสตริงที่ต้องใช้หรือส่งผลให้จำนวนเต็มคือการจัดเก็บสตริงบนเทปในโหมด Ordinal ซึ่งคุณสามารถจัดการกับจำนวนเต็มในโหมด Cardinal

/    Reflect to SE. Switch to Ordinal. While in Ordinal mode, the IP bounces
     diagonally up and down through the program.
I    Read one line from STDIN and push it.
!    Store the string on the tape. This writes the character codes into consecutive
     tape cells starting at the tape head. (It also writes a -1 at the end as a
     terminator, but the tape is initialised to -1s anyway).

     The next few commands are junk that luckily doesn't affect the program:

?      Load the string from the tape and push it to the stack again.
&      Fold the next command over this string. That is, for each character
       in the string, push that character, then execute the command.
?      So we're folding "load" over the string itself. So if the string is "abc"
       we'll end up with "a" "abc" "b" "abc" "c" "abc" on the stack.
!      Store the top copy of "abc" on the tape (does nothing, because it's
       already there).

     Now the relevant part of the program continues.

I    Read another line from STDIN, i.e. the string representation of the index.
/    Reflect to W. Switch to Cardinal. The IP wraps to the last column.
&    Implicitly convert the string to the integer value it represents and
     repeat the next command that many times.
]    Move the tape head right that many cells. Note that Ordinal and Cardinal
     mode have separate tape heads, but they are initialised to the same cell.
?    Load the value from that cell, which is the code point of the character
     at the given index.
O    Print the corresponding character.
!    This discards or converts some of the strings on the stack and writes some
     value back to the tape, but it's irrelevant.
@    Terminate the program.

1
"แม้จะมีความมั่งคั่งในตัว แต่การจัดทำดัชนีสตริงไม่มีอยู่ในอลิซ" > _>
Leun Nun

1
@LeakyNun หากคุณคิดว่ามันไม่ดีก็ไม่มีการสร้างความยาวของสตริง ;)
Martin Ender



3

Brachylog 2 ไบต์

∋₎

ลองออนไลน์!

คำอธิบาย

รวมเอาท์พุทรวมกับองค์ประกอบของอินพุต ด้วยเป็นตัวห้อยมันจะรวมเอาท์พุทของมันกับIองค์ประกอบที่ของของSกับ[S,I]เป็นอินพุท


3

Cubix , 8 ไบต์

t@poIA//

วิธีการแก้ปัญหานี้ได้รับการจัดทำดัชนี 1 อินพุตควรประกอบด้วยตัวเลขก่อนจากนั้นจึงเป็นตัวคั่น (นั่นไม่ใช่ตัวเลขหรือ a .) จากนั้นสตริง

ลองออนไลน์!




2

SILOSขนาด 43 ไบต์

loadLine
readIO
a=256+i
a=get a
printChar a

ลองออนไลน์!

เรียบง่ายพอสมควร


SILOS กลับมา \ o /
แม่ชี

1
ใช่ฉันพยายามตอบรับสายมากที่สุดเท่าที่เป็นไปได้รวมถึงหน้าจอการโหลดหน้าต่างหนึ่ง ฉันชอบประเด็นที่อยู่ในรูปของเอาต์พุตกราฟิกและไลบรารี แต่ฉันยังต้องการพัฒนารูปแบบการบีบอัดเพื่อลองและแข่งขันได้ เป็นหลัก int [] มันรวบรวมเพื่อสามารถสร้างผ่านการอ่านสตรีมไบต์ @LeakyNun
Rohan Jhunjhunwala

2

BF , 9 ไบต์

,[->,<]>.

ดัชนีนี้ใช้รหัสอักขระของอักขระถ่าน (เช่นการส่งของอลิซ) หลังจากนั้นเรามีสตริง

ลองออนไลน์!

ลิงก์ TIO ใช้ Bash wrapper และอินพุตสามารถเปลี่ยนแปลงได้ในไฟล์ส่วนหัว (เหตุผลของ wrapper คือเพื่อให้เราเห็นไบต์)


Legit TIO แฮ็ค: p
Leun Nun


1
ฉันบอกว่ามันถูกต้อง
Leun Nun

มีวิธีในการป้อนข้อมูลภาษา TIO อื่น ๆ เช่นนั้นหรือไม่? ชอบ SMBF ไหม?
mbomb007

@ mbomb007 คุณควรจะทำสิ่งนั้นกับภาษาอื่นโดยใช้เครื่องห่อ นี่คือเสื้อคลุมสำหรับSMBF
user41805

2

JavaScript ขนาด11 10 ไบต์

s=>n=>s[n]

ใช้การจัดทำดัชนีแบบ 0

-1 ไบต์ขอบคุณ @Leaky Nun

f=
s=>n=>s[n]
console.log(f("abcde")(0));
console.log(f("abcde")(1));
console.log(f("a != b")(2));
console.log(f("+-*/")(3));
console.log(f("314159")(4));


1
คุณสามารถใช้การแกงs=>i=>s[i]เพื่อบันทึกไบต์
Leaky Nun

1
ทุกครั้งที่ฉันเห็นคำตอบเช่นนี้มันทำให้ฉันรำคาญเพราะฉันรู้ว่าเวอร์ชั่น C # นั้นจะยาวกว่าหนึ่งไบต์สำหรับปลายลำไส้ใหญ่ในตอนท้าย และนี่เป็นกรณีที่เกิดขึ้นจริง
TheLethalCoder

2

> <> , 13 + 1 = 14 ไบต์

+1 เพื่อ-vตั้งค่าสถานะเพื่อรับอินพุต

:?!\i~1-
io;\

ขอบคุณ@steenbergh ที่แจ้งให้ฉันทราบเกี่ยวกับการ-vตั้งค่าสถานะและช่วยฉัน 3 ไบต์!

อินพุตดัชนีด้วยอาร์กิวเมนต์บรรทัดคำสั่ง-v [index](0-indexed) และป้อนสตริงผ่าน stdin

ลองออนไลน์!

คำอธิบาย

สแต็คเริ่มต้นด้วยดัชนีที่ด้านบน
:ซ้ำซ้อน
?ละเว้นอักขระถัดไปถ้าดัชนีเป็น 0 (การหยุดมันออกจากสแต็ก)
หากเป็นศูนย์\ให้สะท้อนถึงทิศทางที่จะลง \จากนั้นก็จะสะท้อนให้เห็นไปทางขวาด้วยต่อไป มันล้อมรอบและดำเนินการinput ตัวอักษรoutput มันและ;หยุดการดำเนินการ
ถ้าไม่!ข้ามคำแนะนำถัดไปดังนั้นมันจะไม่ลงไป
i~อินพุตอักขระแล้วละทิ้งมันทันที
1กด 1.
-ลบออก1จากดัชนีดังนั้นหนึ่งอักขระในอินพุตถูกใช้และดัชนีจะลดลง โปรแกรม loops :รอบกลับไปที่


1
มี-v <x>พารามิเตอร์บรรทัดคำสั่งซึ่งเริ่มต้นสแต็กเพื่อหยุดการ<x>ทำงานของโปรแกรม ลองออนไลน์
steenbergh

2

แครอท 16 ไบต์

$^//.{#}/S1//.$/

ลองออนไลน์!

รูปแบบอินพุตเป็นเช่น:

string
index

และโปรแกรมได้รับการจัดทำดัชนี 1

คำอธิบาย

แครอทมีตัวแปรทั่วโลกหลายตัวหนึ่งตัวสำหรับแต่ละประเภทคือสตริงลอยและอาร์เรย์ โปรแกรมเริ่มต้นในโหมดสตริงที่ตัวดำเนินการทั้งหมดจะมีผลกับตัวแปรสตริงส่วนกลาง และฉันเรียกตัวแปรเหล่านี้ว่า "สแต็ค"

(ตัวอย่างการป้อนข้อมูล: abcdef\n3)

$                          Get the first line of the input and set the stack-string to this value
^                          Exit caret-mode
                           stack-string = "abcdef"
/                          Operator (behaves differently depending on the argument)
 /.{#}/                     And the argument to this operator is a regex, so this program gets the matches of this regex into the stack-array
  .                          Any character
   {#}                       Pops a line from the input. So now this evaluates to # of any character where # is the second line of the input (in this case, 3)
                           stack-array = ["abc"]
                           And now we just need to get the last character in this string, but first
S1                         Join the array on the number 1 and set this to the stack-string. Because the array only contains one element, the number 1 does not appear in the stack-string.
                           stack-string = "abc"
/                          Operator; because the argument is a regex, this retrieves the matches of the regex:
 /.$/                       Get the last character in the string
                           stack-array = ["c"]

ตอนนี้สิ่งนี้จะคืนอาร์เรย์หนึ่งองค์ประกอบที่มีสตริงที่มีความยาวหนึ่งรายการ แต่จะแสดงเป็นสตริงในเว็บไซต์

หากเราต้องการให้ผลลัพธ์เป็นสตริงเราสามารถทำได้อย่างง่ายดายS","ในตอนท้าย แต่มันไม่สำคัญเพราะผลลัพธ์ยังคงเหมือนเดิมในล่าม


1

รุ่นที่ 32 ไบต์

@set/ps=
@call echo(%%s:~%1,1%%

อ่านsจาก STDIN และรับnเป็นพารามิเตอร์บรรทัดคำสั่ง


1

GolfScript , 7 4 ไบต์

~1/=

ลองออนไลน์!

ฉันดูด GolfScript ดังนั้นฉันให้ความสำคัญกับความช่วยเหลือของMartin Ender


[มีความจำเป็นในการไม่ได้
Martin Ender

แต่โดยทั่วไปถ้าคุณต้องการตัวละครจากสตริงคุณเพียงแค่แยกสตริงก่อนแทนที่จะแปลงจุดโค้ดกลับมา~1/=
Martin Ender

@MartinEnder และนั่นคือวิธีที่คุณเอาชนะอลิซขึ้น ...
Erik the Outgolfer

1

Turtlèd , 9 ไบต์

!?:[*l+].

ลองออนไลน์!

คำอธิบาย:

ก่อนอื่นTurtlèdเป็นแบบกริดกับเต่า มีเซลล์กริดที่เต่าสามารถเลื่อนไปทางซ้ายขึ้นลงและขวาและสามารถเขียนสิ่งต่าง ๆ บนเซลล์กริด

[the first grid cell the turtle starts on is marked with a *]
!         input the string into the string variable

 ?        input the number into the number variable

  :       this command takes the number variable and moves right that many.
          hence this moves right by the amount inputted

   [*  ]  this is an `until` loop. the * there means that `until` the turtle ends the loop
          on a grid cell with * written on it (that is, the first cell), it will execute
          the code inside again and again

     l+   the code inside the while loop. the `l` moves the turtle left, and the +
          increments the string pointer. the string pointer is used with the string var;
          when you want to write something from the string, you use `.`, which writes
          the pointed char. the pointed char is the n-th character of the string, n being
          the value of the string pointer. this code will execute until the l moves
          the turtle back on to the origin cell. since we moved right by the number
          inputted, this will increase the string pointer (which starts at 1)
          by the amount inputted.

       .  write the pointed char, which was dealt with in the previous comment.
          if 0 is inputted, turtle stayed on the origin square, and executed none
          of the loop, and turtle writes the first char of string input.
          if 1 is inputted, turtle moved one right, moved one left and incremented
          string pointer once, which means the second char is written. and so on.


          [the char of input has been written over the origin square]
          [implicitly the grid is outputted, which has spaces and blank lines taken out]
          [this is the requested char outputted, plus an unavoidable trailing newline
          due to how I made the interpreter. sue me]

1

Clojure, 3

nth

: P คุณสามารถทำอะไรเมื่อมีการสร้างขึ้นในเรื่องนี้บ้าง สิ่งนี้ใช้ได้กับรายการเวกเตอร์สตริงและลำดับ มันเป็นทั้ง O (1) หรือ O (n) ขึ้นอยู่กับประเภทข้อมูลที่ใช้



1

Dyvil , 4 ไบต์

_[_]

สร้างฟังก์ชั่นที่ไม่ระบุชื่อที่ใช้Stringและและผลตอบแทนintchar

การใช้งาน:

let f: (String, int) -> char = _[_]
print f("abc", 1) // b




1

Mathematica ขนาด 18 ไบต์

#~StringTake~{#2}&

การแก้ปัญหาพื้นฐาน แต่น่าเสียดายที่ชื่อฟังก์ชั่นค่อนข้างยาว


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