媒人媒人让我火柴


21

(尽管我们不会找到Find或捕捉到tryCatch

这是实现一些有趣的R函数的多部分系列的第二部分。第一部分可以在这里找到。

任务:

您将以尽可能少的字节实现R的match功能

输入:

  • x,可能是空的整数列表/数组
  • table,可能是空的整数列表/数组
  • nomatch,一个整数值
  • incomparables,可能是空的整数列表/数组

输出:

  • 一个O长度等于的整数的单个数组/整数列表x,其中每个值O[i]表示以下任一值:
    • 一个值的索引j,其中tabletable[j]==x[i]
    • nomatch,这表明在没有价值table等于x[i] x[i]是在列表中incomparables

测试用例

All in the form x, table, nomatch, incomparables -> output
outputs 

[], [1,2,3], 0, [5] -> []

[1, 2, 3], [], 0, [5] -> [0, 0, 0]

[9, 4, 3, 6, 3], [9, 8, 7, 6, 5, 4, 3, 2, 1], -1, [4] -> [1, -1, 7, 4, 7]

[8, 6, 7, 5, 3, 0, 9], [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3, 2, 3, 8, 4, 6, 2, 6], 1000, [1] -> [12, 8, 14, 5, 1, 1000, 6]

可以根据需要生成更多的测试用例。

附加规则:

  • R具有基于1的索引,但是可以接受一致的基于替代的索引。因此,您可以使用以3或17开头的索引,但必须保持一致,并且必须在答案中指出。
  • 如果您选择的语言具有内置的功能,请同时实施您自己的解决方案。
  • 解释表示赞赏。

这是,因此最短的解决方案以字节为单位!


它必须支持负数吗?我假设它不需要,因为仅示例假设它,而且我很确定有一个标准规则。
wizzwizz4

@ wizzwizz4不,因为4在中incomparables,因此无法匹配。如果您的语言不支持负数,则可以要求使用非负数,但是请在提交时说明该假设。
朱塞佩

1
除了标题注释:我们也不会这样做make
瓦尔说莫妮卡(Monica)

1
@val实际上是对屋顶上的提琴手的糟糕参考;所有这些挑战都以各种娱乐节目为主题,因为这一主题非常完美,以至于我认为这将成为一个很好的主题。
朱塞佩

Answers:


8

果冻 10  8 字节

-2多亏了大公埃里克(Erik the Outgolfer)

,⁷y⁵iⱮ⁶o

包含四个命令行参数的完整程序,incomparables nomatch table x该程序将打印R match函数结果列表的Jelly表示形式* 。

在线尝试!

怎么样?

例如与incomparables nomatch table x= [1,4], 2, [2,4], [4,3,2,1,0]

,⁷y⁵iⱮ⁶o - Main Link: list, incomparables; list, nomatch
 ⁷       - newline character                                '\n'
,        - pair (incompararables) with (right)              [[1,4],'\n']
   ⁵     - 5th argument (3rd input = table)                 [2,4]
  y      - translate (right) with lookup (left)             [2,'\n']             
      ⁶  - 6th argument (4th input = x)                     [4,3,2,1,0]
     Ɱ   - map with:
    i    -   first index of (right) in (left)               [0,0,1,0,0]
       o - logical OR                                       [2,2,1,2,2]

*空列表不表示任何内容,第1个列表仅表示项目,而其他列表则用[]分隔并用,



6

R,55个字节

在这种情况下,代码不会使用match其全部功能,而只是用作index功能。第一个R答案,因此字节效率极低!

注意(感谢Giuseppe提供的信息) %in%并且setdiff都在内部使用来实现match,因此,完全摆脱此令人惊讶的有用功能将导致混乱。因此,有150个重复的赏金,没有截止日期!(请注意setdiff,但是允许)

function(x,t,n,i)ifelse(x%in%setdiff(t,i),match(x,t),n)

在线尝试!

要么...

R,5个字节

match

在线尝试!


尽管我不愿使用%in%and match; 我不确定是否可以做得更好。如果您想找到一个好的高尔夫球答案而又没有这些功能中的任何一个(可能太可怕了),我将为您提供帮助。
朱塞佩

嗯,我刚刚在golfR上评论了一下……
Xcoder先生,

专为过时的match提交而
投票

您可以ifelse使用此提示来缩短时间:codegolf.stackexchange.com/a/97826/59530
JAD

2
此外,您的较长提交实际上使用match:S
JAD




5

R,79字节

function(x,t,n,i)sapply(x,function(y)`if`(any(z<-y==t)&all(y-i),which(z)[1],n))

在线尝试!

R,66字节

function(x,t,n,i)sapply(x,function(y)c(which(y==t&all(y-i)),n)[1])

将我的解决方案与尼克解决方案的一部分结合起来。

在线尝试!



4

Python 3,60个字节

lambda x,t,n,i:[v in{*t}-{*i}and-~t.index(v)or n for v in x]

在线尝试!


3.8的特定功能是什么?在我看来就像这可能为Python 3的任何颠覆工作
西奥

好吧,它不是特定于3.8的。我只是将自动生成的模板复制粘贴到TIO上,所以我没有注意到我使用了3.8。感谢您的单挑,将进行调整。
Xcoder先生,

1
R has 1-based indices, but a consistent alternative-based indices are acceptable.因此,您可以取出,-~并仅将0索引用于-1个字节。
价值墨水

1
@ValueInk对于第三个测试用例(通常在列表的开头是匹配元素时),该方法将失败,因为在Python中0为假。
Xcoder先生,

1
啊,公平的表演。顺便说一下,t.index(v)if v in{*t}-{*i}else n其字节数与您当前的v in{*t}-{*i}and-~t.index(v)or n解决方案完全相同,哈哈
Value Ink,



3

Perl 6、45字节

->\b,\c,\d{*>>.&{$_d&&~b.first($_,:k)||c}}

在线尝试!

匿名代码块,将输入的内容作为咖喱,f(table, nomatch, incomparables)(x)并返回匹配0的索引。

说明:

->\b,\c,\d{                               }    # Anonymous code block taking 3 inputs
           *           # Return an anonymous Whatever lambda
            >>.&{                        }  # Mapping input to
                 $_d                       # If the element is not an uncomparable
                     && b.first($_,:k)      # Return the first index in the table
                       ~                    # Stringified so Nils are false
                                      ||c   # Else the nomatch element
~~

+1我做了短暂的工作,但是这样做$_∉d&&b.antipairs.Map{$_}||c的确不错,但是对于falsey值为0。first($ _:k)是一个很好的解决方案,它避免了冗长的antipairs.Map强制。
user0721090601

2

木炭,14字节

IEθ∨∧¬№ει⊕⌕ηιζ

在线尝试!链接是详细版本的代码。1个索引。说明:

  θ             First input (x)
 E              Map over elements
       ε        Fourth input (incomparables)
      №         Count occurrences of
        ι       Current element
     ¬          Is zero
    ∧           Logical And
           η    Second input (table)
          ⌕     Find 0-based index of
            ι   Current element
         ⊕      Convert to 1-indexed
   ∨            Logical Or
             ζ  Third input (nomatch)
I               Cast to string
                Implicitly print on separate lines

2

C(gcc),125个字节

1个索引。

鉴于我不能在传递的数组中使用哨兵值,因此需要为每个数组指定数组边界。

f(x,c,t,d,n,i,e,j,f)int*x,*t,*i;{for(;f=0,c-->0;x[c]=--f?n:j){for(j=e;!f&j;x[c]-i[--j]||--f);for(;!f&j<d;x[c]-t[j++]||++f);}}

在线尝试!


2

附件,39字节

${{[_,y][nil=_or x@_in z]}=>x&Index@_4}

在线尝试!

相当简单的验证。请注意,参数顺序与match的顺序不同;具体来说,x是最后一个参数而不是第一个参数,并且对应_4于上面的代码片段。

说明

${{[_,y][nil=_or x@_in z]}=>x&Index@_4}
${                                    }   named lambda, taking parameters x, y, z, and _4
                            x&Index@_4    short for Index[x, _4];
                                              calculates where each element in _4 occurs in x
                                              returns `nil` for no match
  {                      }=>              over each index:
   [_,y][               ]                     choose y (`nomatch`) if
         nil=_                                    the index is nil
              or x@_in z                          or the element is in `incomparables`
    _                                         otherwise, choose the index

2

Haskell,57 56字节

(t#n)i=map$maybe n id.($zip i[n,n..]++zip t[1..]).lookup

参数顺序是:tablenomatchincomparablesx

在线尝试!


2

05AB1E,7 个字节

õ:Ik®I:

0索引。输入顺序为:incomparables, table, x, nomatch

在线尝试。

说明:

õ:       # Replace all values of the (implicit) first incomparables-list in
         # the (implicit) second table-list with an empty string
         #  i.e. incomparables=[4] and table=[9,8,7,6,5,4,3,2,1] → [9,8,7,6,5,"",3,2,1]
  Ik     # Get the index of each value in the third x-list in this list (-1 if not found)
         #  i.e. x=[9,4,3,6,3] → [0,-1,6,3,6]
    ®I:  # Replace all -1 with the fourth input-integer
         #  i.e. nomatch=-99 → [0,-99,6,3,6]
         # (and output the mapped list implicitly as result)
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.