第一最后最后最后


11

挑战

任务很简单。给定一个数组以及第一个和最后一个值:在第一个之后返回最后一个的第一个,在最后一个之前返回第一个的最后一个。


或简单地说:给定数组var1,var2。

示例数组:

[var2,,var1,,var2,,var2,var1,var2,]

返回:

  • 数组中出现的第一个var1右侧的第一个var2的索引。

[var2,,第一个var1第一个var2,第二个var2,var1,第三个var2,]

  • 数组中显示的最后一个var2左侧的第一个var1的索引。

[var2,,second var1,,var2,,var2,first var1last var2,]

输入值

两个不同的正整数

正整数数组

输出量

答案索引,按顺序

规则

数组将包含每个变量中的至少一个(最小大小为2)

假设输入工作

示例:0, 1 [1, 0]或类似的将失败

IO很灵活

例子

Input
First = 2; Last = 4; [0, 2, 4, 2, 3, 1, 4, 0, 1, 2, 4, 9]

Output
2, 9

Input
First = 4; Last = 2; [0, 2, 4, 2, 3, 1, 4, 0, 1, 2, 4, 9]

Output
3, 6

Input
First = 0; Last = 1; [0, 1]

Output
1, 0

3
可以var1等于var2
ngn

1
@ngn不,不一定。如果是这样的话,将导致琐碎的结果,因此没有必要处理这种情况。
WretchedLout

3
欢迎来到PPCG!
乔纳森·艾伦

2
我们可以以相反的顺序返回输出吗?例如,测试用例将导致9, 26, 30, 1分别(或加上一个,如果输出为1索引)。
暴民埃里克(Erik the Outgolfer)'18年

1
其次,@ Jakob,当前措词与示例不匹配。
Nit

Answers:





4

JavaScript(ES6),63个字节

(x,y,a)=>a.map(P=(v,i)=>v-y?v-x?0:a=i:1/(p=a)?P=+P||i:0)&&[P,p]

在线尝试!

已评论

(x, y, a) =>          // given the two integers x, y and the array a[]
  a.map(P =           // initialize P to a non-numeric value
            (v, i) => // for each value v at position i in a[]:
    v - y ?           //   if v is not equal to y:
      v - x ?         //     if v is not equal to x:
        0             //       do nothing
      :               //     else (v = x):
        a = i         //       save the current position in a
    :                 //   else (v = y):
      1 / (p = a) ?   //     update p to a (last position of x); if p is numeric (>= 0):
        P = +P || i   //       unless P is also already numeric, update it to i
                      //       (if P is numeric, it's necessarily greater than 0 because
                      //       we've also seen x before; that's why +P works)
      :               //     else:
        0             //       do nothing
  )                   // end of map()
  && [P, p]           // return [P, p]

替代版本

使用JS内置函数,一个更简单的答案是79个字节:

(x,y,a)=>[a.indexOf(y,a.indexOf(x)),a.slice(0,a.lastIndexOf(y)).lastIndexOf(x)]

可以稍微压缩到75个字节:

(x,y,a)=>[a.indexOf(y,a.indexOf(x)),a.slice(0,a[L='lastIndexOf'](y))[L](x)]

在线尝试!

编辑@Neil设法将其减少到一个非常漂亮的67字节

(x,y,a,f=s=>a[z=y,y=x,x=z,s+=`ndexOf`](x,a[s](y)))=>[f`i`,f`lastI`]

在线尝试!


lastIndexOf采用两个参数,因此将直接答案减少到70个字节,而我能够提出以下67个字节的版本:(x,y,a,f=s=>a[z=y,y=x,x=z,s+=`ndexOf`](x,a[s](y)))=>[f`i`,f`lastI`]
Neil

3

Python 3中97 93个字节

-4字节归功于ovs

def h(f,l,a,I=list.index):j=I(a,f);i=len(a)+~I(a[::-1],l);print(I(a[j:],l)+j,i-I(a[i::-1],f))

在线尝试!


a-1-b == a + (-b-1) == a + ~b可以用于-1字节,将index函数分配给名称可以将其获得93字节
ovs

2

Japt27 25 24字节

受到@Arnauld的启发

感谢@Shaggy -2字节和@ETHproductions -1字节

我刚开始使用japt,所以它一定是更好的方法。\

[WsX=WbU)bV +XWsTWaV)aU]

在线尝试!


1
欢迎来到Japt :)您可以将那些双空格替换)为起步器,以节省2个字节。
毛茸茸的

@蓬松的坦克!我不知道那是什么
路易斯·费利佩·德·耶稣·穆诺兹

像您一样,我深信有一个更短的方法。但是,现在没有足够的空间尝试解决这个问题!
粗野的

欢迎!您可以使用X=WbU)...+X以下方法保存一个字节:在线尝试!我也在努力寻找一个更短的方法……
ETHproductions '18



1

MATL,27字节

y=Y>/ti=PY>P/t3G=f1)w2G=f0)

在线尝试!

对于相同的字节数,也可以选择:

27个字节

y=Y>yi=*f1)y3G=PY>Pb2G=*f0)

在线尝试!

第二个更容易解释:

y   % implicitly get the first two inputs (the array and var1),
    %  and duplicate the first input
    %  stack: [[0 2 4 2 3 1 4 0 1 2 4 9] 2 [0 2 4 2 3 1 4 0 1 2 4 9]]
=   % compare and return logical (boolean) array
    %  stack: [[0 2 4 2 3 1 4 0 1 2 4 9] [0 1 0 1 0 0 0 0 0 1 0 0]]
Y>  % cumulative maximum - make all values after the first 1 also 1s
    %  stack: [[0 2 4 2 3 1 4 0 1 2 4 9] [0 1 1 1 1 1 1 1 1 1 1 1]]
    %  now we have 1s in positions at and after the first time var1 appears
y   % duplicate 2nd element in stack
    %  stack: [[0 2 4 2 3 1 4 0 1 2 4 9] [0 1 1 1 1 1 1 1 1 1 1 1] [0 2 4 2 3 1 4 0 1 2 4 9]]
i=  % compare with the next input (var2), returning a boolean array
    % stack: [[0 2 4 2 3 1 4 0 1 2 4 9] [0 1 1 1 1 1 1 1 1 1 1 1] [0 0 1 0 0 0 1 0 0 0 1 0]]
*   % multiply the two boolean arrays - so we'll have 1s only where var2 was present after the first occurrence of var1
    % stack: [[0 2 4 2 3 1 4 0 1 2 4 9] [0 0 1 0 0 0 1 0 0 0 1 0]]
f1) % find the index of the first 1 in that (this is our first result value)

除了这些更改之外,代码的第二部分执行相同的操作:

  • 使用2G用于第二输入端(VAR1)和3G第一第三输入(VAR2)代替隐式输入或i,因为这些已经被消耗
  • 使用PY>P(从左到右翻转数组,获取累积最大值,向后翻转)代替Y>在最后一次出现之前而不是第一次出现之后获取1s
  • 用于f0)获得两个条件都为真的最后一个位置,而不是第一个位置(之所以有效,是因为MATL使用模块化索引,所以0表示数组的最后一个索引)

1

MATLAB(80位元组)

输入是xya。由于MATLAB是1索引的,因此应在测试用例中加1。

xi=find(a==x);
yi=find(a==y);
yi(find(yi>xi(1),1))
xi(find(xi<yi(end),1,'last'))

测试用例:

x=4
y=2
a =  [0, 2, 4, 2, 3, 1, 4, 0, 1, 2, 4, 9]

% 
xi=find(a==x);
yi=find(a==y);
yi(find(yi>xi(1),1))
xi(find(xi<yi(end),1,'last'))

ans =

     4


ans =

     7

0

Java 8,114字节

一个lambda,取a java.util.List<Integer>和两个ints(var1,var2)并返回一个逗号分隔的对。

(a,f,l)->a.indexOf(f)+a.subList(a.indexOf(f),a.size()).indexOf(l)+","+a.subList(0,a.lastIndexOf(l)).lastIndexOf(f)

在线试用



0

朱莉娅 71 64字节

多亏了sundar和他find(A.==x)[]findfirst(A,x))

(A,x,y)->findnext(A,y,find(A.==x)[]),findprev(A,x,findlast(A,y))

如果您的语言是基于1的,则可以返回基于1的索引(这是此处的通常共识),因此不需要-1。另外,您可以使用find(A.==x)[]代替保存另一个字节findfirst(A,x)
sundar-恢复莫妮卡
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.