指定索引处的字符串元素


13

给定一个字符串s和一个n小于其长度的非负数s,请在的n第-个位置输出字符s

允许0索引和1索引。对于1分度,n将为正且小于或等于的长度s

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
我对此表示反对,因为这实际上不是编程或打高尔夫球的挑战; 这里要问的是,内置哪种语言的工作最短。
毛茸茸的

15
@Shaggy同样适用于许多其他简单的挑战,例如加两个数字,测试一个数字是否为质数或打印“ Hello,World!”。尽管这些语言在许多无聊的语言中都是可以解决的,但对于那些不得不采用自己的实现的更原始的语言来说,它们可能是一个有趣的挑战。另外,对这些语言来说,任何更复杂的事情通常都太多了,因此很高兴能找到它们。如果您遇到了琐碎的挑战,请尝试用普通的语言来应对。
马丁·恩德

Answers:



7

爱丽丝,5个字节

I&IO@

在线尝试!

通常,如果我们避免使用序数模式并使用糟糕的输入格式,则时间要短得多。在此,第一个字符的代码点用作整数输入。输入的其余部分是字符串。索引从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
斯蒂芬

@StephenS您的意思是我在meta上提出了这种I / O格式?是的,我确实做到了,但是主要是针对每次使用数字I / O进行竞争时都必须实现自己的十进制整数解析器/渲染器的语言,因此他们可能会完全跳过它们。但是它有一个不幸的副作用,在某些可以很容易地读写十进制的语言中,使用代码点仍然要短一些。
马丁·恩德

6

Python,15个字节

str.__getitem__

要么

lambda s,i:s[i]

两者都有两个参数:字符串和索引。0索引。


我很惊讶这两种方法的长度相同。
Leaky Nun

6

Haskell,4个字节

(!!)

基于0的索引。用法示例:(!!) "Hello" 1-> 'e'


5

八度,10字节

@(s,n)s(n)

接受一个字符串s和一个数字n作为输入,并返回的n第th个字符s




3

爱丽丝,10字节

/@!O?]&
 I

在线尝试!

在第一行中包含字符串,在第二行中包含从0开始的索引。

说明

尽管内置索引丰富,但是Alice中不存在字符串索引。原因是它同时需要整数和字符串参数,并且Alice中的所有命令严格来说都是整数到整数或字符串到字符串。

通常,对需要或产生整数的字符串执行任何操作的主要方式是以“序数”模式将字符串存储在磁带上,然后可以在“基数”模式下使用整数进行操作。

/    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
“尽管有大量的内置函数,但是在Alice中不存在字符串索引。” > _>
Leaky Nun

1
@LeakyNun如果您认为这很糟糕,也没有内置函数可以获取字符串的长度。;)
Martin Ender



3

Brachylog,2个字节

∋₎

在线尝试!

说明

将其输出与Input的元素统一。与作为下标,它将统一其与输出I的第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 /
Leaky Nun

1
是的,我正在尝试回答尽可能多的难题,包括Windows加载屏幕之一。我喜欢图形输出和库方面的要点,但是我仍然想开发一种压缩方案以尝试使其具有竞争力。本质上,可以通过读取字节流来生成它编译的int []。@LeakyNun
Rohan Jhunjhunwala

2

BF,9字节

,[->,<]>.

索引通过字符的字符代码获取(例如Alice提交)。接下来,我们有了字符串。

在线尝试!

TIO链接使用Bash包装器,并且可以在头文件中更改输入(使用包装器的原因是可以看到字节)。


合法的TIO骇客:p
Leaky Nun

@LeakyNun我为BF使用了包装器
user41805 '17

1
我确实说过这是合法的。
Leaky Nun

有没有办法像这样输入其他TIO语言?喜欢SMBF吗?
mbomb007 '17

@ mbomb007您应该能够使用其他语言的包装程序来执行此操作。这是SMBF
user41805 '17

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
您可以使用curring s=>i=>s[i]保存一个字节
Leaky Nun

1
每当我看到这样的答案时,都会感到烦恼,因为我知道C#版本最后总是将半冒号加一个字节。确实是这种情况
TheLethalCoder

2

> <>,13 +1 = 14字节

+1以供-v标志输入

:?!\i~1-
io;\

感谢@steenbergh通知我有关-v标志并为我节省了3个字节!

使用命令行参数-v [index](0索引)输入索引,然后通过stdin输入字符串。

在线尝试!

说明

堆栈从顶部的索引开始。
:复制它。
?如果索引为0,则忽略下一个字符。(将其从堆栈中弹出)
如果为零,则\反映向下的方向。然后,它与next一起反映在右边\。它环绕并执行input一个字符,将其o输出并;暂停执行。
如果不是,则!跳过下一条指令,因此它不会下降。
i~输入一个字符,然后立即将其丢弃。
1按下1. 从索引中
-减去1,因此将消耗输入中的一个字符,并减少索引。程序循环返回到:


1
-v <x>命令行参数,该参数初始化堆栈以<x>在程序启动时保持。在线尝试
steenbergh

2

胡萝卜,16字节

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

在线尝试!

输入格式如下:

string
index

并且该程序是1索引的。

说明

Carrot有几个全局变量,每个全局变量一个,分别是:字符串,浮点数和数组(其他将很快实现)。该程序以字符串模式启动,其中所有运算符都会影响全局字符串变量。我将这些变量称为“堆栈”。

(实施例输入: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"]

现在,这将返回一个包含长度为1的字符串的单元素数组,但在网站中将其显示为字符串。

如果我们真的想将结果作为字符串给出,则可以S","在末尾轻松完成,但这没关系,因为在解释器上输出仍然是相同的。


1

批处理,32字节

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

s从STDIN 读取,并n作为命令行参数。



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

sed,31个字节

:
/^1/{s:1 .: :;b}
s: (.).*:\1:

在线尝试!

输入:索引和字符串,以空格分隔。一元索引,但从零开始。


1

魔鬼,4字节

_[_]

创建一个匿名函数,该函数采用String和,int并返回char

用法:

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




1

Mathematica,18个字节

#~StringTake~{#2}&

基本的解决方案,但不幸的是函数名称很长。


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.