gpt4 book ai didi

bytearray - 数组搜索代码挑战

转载 作者:行者123 更新时间:2023-12-04 02:30:23 26 4
gpt4 key购买 nike

锁定。这个问题及其答案是locked因为这个问题是题外话,但具有历史意义。它目前不接受新的答案或互动。








这是我的( Code Golf )挑战:
取两个字节数组并确定第二个数组是否是第一个数组的子字符串。如果是,则输出第二个数组的内容出现在第一个数组中的索引。如果在第一个数组中找不到第二个数组,则输出 -1。

示例输入:
{ 63, 101, 245, 215, 0 } { 245, 215 }

预期输出:2

示例输入 2:{ 24, 55, 74, 3, 1 } { 24, 56, 74 }

预期输出 2:-1

编辑:有人指出 bool 是多余的,因此您的函数所要做的就是返回一个表示值索引的 int,如果找不到,则返回 -1。

最佳答案

J

37 个字符用于比请求更多的功能:它返回所有匹配索引的列表。

I.@(([-:#@[{.>@])"_ 0(<@}."0 _~i.@#))

用法:
   NB. Give this function a name   i =: I.@(([-:#@[{.>@])"_ 0(<@}."0 _~i.@#))   NB. Test #1   245 215 i 63 101 245 215 02   NB. Test #2 - no results   24 56 74 i 24 55 74 3 1   NB. Test #3: matches in multiple locations   1 1 i 1 1 1 2 1 1 30 1 4   NB. Test #4: only exact substring matches   1 2 i 0 1 2 3 1 0 2 1 2 01 7

NB. list[0 to end], list[1 to end], list[2 to end], ...
<@}."0 _~i.@#

NB. Does the LHS completely match the RHS (truncated to match LHS)?
[-:#@[{.>@]

NB. boolean list of match/no match
([-:#@[{.>@])"_ 0(<@}."0 _~i.@#)

NB. indices of *true* elements
I.@(([-:#@[{.>@])"_ 0(<@}."0 _~i.@#))

关于bytearray - 数组搜索代码挑战,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1078770/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com