Vat*_*ine 12
常见的lisp:
(defun golf-code (master-seq sub-seq) (search sub-seq master-seq))
37个字符的功能比请求的更多:它返回所有匹配索引的列表.
I.@(([-:#@[{.>@])"_ 0(<@}."0 _~i.@#))
Run Code Online (Sandbox Code Playgroud)
用法:
NB. Give this function a name
i =: I.@(([-:#@[{.>@])"_ 0(<@}."0 _~i.@#))
NB. Test #1
245 215 i 63 101 245 215 0
2
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 3
0 1 4
NB. Test #4: only exact substring matches
1 2 i 0 1 2 3 1 0 2 1 2 0
1 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.@#))
Run Code Online (Sandbox Code Playgroud)
PostScript, 149 146 170 166 167 159个字符(在"做工作"部分):
% define data
/A [63 101 245 215 0] def
/S [245 215] def
% do the work
/d{def}def/i{ifelse}d/l S length 1 sub d/p l d[/C{dup[eq{pop -1}{dup S p
get eq{pop p 0 eq{]length}{/p p 1 sub d C}i}{p l eq{pop}if/p l d C}i}i}d
A aload pop C
% The stack now contains -1 or the position
Run Code Online (Sandbox Code Playgroud)
请注意,如果子数组包含多次,则会查找子数组的最后一次出现.
修订记录:
falseby [[ne和trueby [[eq以保存三个字符S出现两次,则删除可能导致误报的错误A.不幸的是,这个bugfix有24个字符.解释版本:
不幸的是,SO语法高亮显示器不知道PostScript,因此可读性仍然有限.
/A [63 101 245 215 0] def
/S [245 215 ] def
/Slast S length 1 sub def % save the index of the last element of S,
% i.e. length-1
/Spos Slast def % our current position in S; this will vary
[ % put a mark on the bottom of the stack, we need this later.
/check % This function recursively removes values from the stack
% and compares them to the values in S
{
dup [
eq
{ % we found the mark on the bottom, i.e. we have no match
pop -1 % remove the mark and push the results
}
{ % we're not at the mark yet
dup % save the top value (part of the bugfix)
S Spos get
eq
{ % the top element of the stack is equal to S[Spos]
pop % remove the saved value, we don't need it
Spos 0
eq
{ % we are at the beginning of S, so the whole thing matched.
] length % Construct an array from the remaining values
% on the stack. This is the part of A before the match,
% so its length is equal to the position of the match.
% Hence we push the result and we're done.
}
{ % we're not at the beginning of S yet, so we have to keep comparing
/Spos Spos 1 sub def % decrease Spos
check % recurse
}
ifelse
}
{ % the top element of the stack is different from S[Spos]
Spos Slast eq {pop} if % leave the saved top value on the stack
% unless we're at the end of S, because in
% this case, we have to compare it to the
% last element of S (rest of the bugfix)
/Spos Slast def % go back to the end of S
check % recurse
}
ifelse
}
ifelse
}
def % end of the definition of check
A aload % put the contents of A onto the stack; this will also push A again,
% so we have to ...
pop % ...remove it again
check % And here we go!
Run Code Online (Sandbox Code Playgroud)
C99
#include <string.h>
void find_stuff(void const * const array1, const size_t array1length, /* Length in bytes, not elements */
void const * const array2, const size_t array2length, /* Length in bytes, not elements */
char * bReturnBool,
int * bReturnIndex)
{
void * found = memmem(array1, array1length, array2, array2length);
*bReturnBool = found != NULL;
*bReturnIndex = *bReturnBool ? found - array1 : -1;
}
Run Code Online (Sandbox Code Playgroud)
简而言之,有点麻烦:
#include <string.h>
#define f(a,b,c,d,e,f) { void * g = memmem(a, b, c, d); f = (e = !!g) ? g - a : -1; }
Run Code Online (Sandbox Code Playgroud)
根据Nikhil Chelliah的回答 kaiser.se的回答:
>>> t=lambda l,s:''.join(map(chr,l)).find(''.join(map(chr,s)))
>>> t([63, 101, 245, 215, 0], [245, 215])
2
>>> t([24, 55, 74, 3, 1], [24, 56, 74])
-1
Run Code Online (Sandbox Code Playgroud)
部分感谢gnibbler:
>>> t=lambda l,s:bytes(l).find(bytes(s))
>>> t([63, 101, 245, 215, 0], [245, 215])
2
>>> t([24, 55, 74, 3, 1], [24, 56, 74])
-1
Run Code Online (Sandbox Code Playgroud)
OP指定的参数顺序:
import List;t l s=maybe(-1)id$findIndex id$map(isPrefixOf s)$tails l
Run Code Online (Sandbox Code Playgroud)
正如ephemient指出的那样,我们可以切换参数并将代码减少四个字符:
import List;t s=maybe(-1)id.findIndex id.map(isPrefixOf s).tails
Run Code Online (Sandbox Code Playgroud)