实施模糊查找器


14

受此链接启发,我在Reddit上发现该链接

FuzzyFinder是许多文本编辑器的功能。当您开始输入文件路径时S,FuzzyFinder会弹出并显示当前目录中的所有文件,其中包含您输入的字符串,并按文件中的位置排序S

您的任务是实现模糊查找器。它应该是采用(通过stdin,函数参数或命令行)一个字符串S和一个字符串列表的程序或函数,该字符串和字符串列表可以根据需要设置L格式,并返回或打印运行模糊查找器的结果。搜索应区分大小写。S您可以根据需要对多个字符串中处于相同位置的结果进行排序。

例:

Input: mig, [imig, mig, migd, do, Mig]
Output:
    [mig, migd, imig]
OR
    [migd, mig, imig]

这就是代码高尔夫,所以最短的解决方案是成功的。


我们可以假设所有输入都为小写,还是应该模糊匹配大写?
卡德2015年

1
@ Vioz-不; 搜索必须区分大小写。我更新了问题和示例。
kirbyfan64sos

Answers:


5

Pyth,9个字节

oxNzf}zTQ

在线试用:演示

说明:

            implicit: z = input string, Q = input list
    f   Q   filter Q for elements T, which satisfy:
     }zT      z is substring of T
o           order the remaining strings N by:
 xNz          the index of z in N

1
这与我在测试期间使用的Pyth程序完全相同。:)
kirbyfan64sos

5

Python 2、65

def f(s,l):g=lambda x:x.find(s)+1;print sorted(filter(g,l),key=g)

表达式x.find(s)返回的第一次出现的位置sx,给人-1为不匹配。我们将1结果添加到不匹配对应于0,让我们将filter它们淘汰。然后,我们按匹配位置排序,该位置不受移位1的影响。


5

CJam,18个 15字节

{1$#)}q~2$,@$p;

CJam解释器中在线尝试。

输入输出

输入:

"mig" ["imig" "mig" "migd" "do" "Mig"]

输出:

["mig" "migd" "imig"]

怎么运行的

      q~        e# Read and evaluate the input from STDIN.
                e# Pushes a needle and an array of haystacks.
{    }          e# Define a code block:
 1$             e#   Copy the needle.
   #            e#   Compute the index of the needle in the haystack.
    )           e#   Add 1 to the index.
        2$      e# Copy the block.
          ,     e# Filter: Keep only haystacks for which the code block
                e#         pushed a non-zero value.
           @    e# Rotate the block on top of the stack.
            $   e# Sort: Arrange the haystacks according to the values
                e#       pushed by the code block.
             p  e# Print the filtered and sorted haystacks.
              ; e# Discard the needle.

5

GolfScript,13个字节

~{?)}+\1$,\$`

在少数情况下,GolfScript可以通过使用块级联来击败CJam,并利用输入中的一些自由进行输入,这些输入可以随意格式化

Web GolfScript中在线尝试。

输入输出

输入值

["imig" "mig" "migd" "do" "Mig"] {"mig"}

输出量

["migd" "mig" "imig"]

怎么运行的

~             # Evaluate the input from STDIN.
              # Pushes an array of haystacks and a needle in a block.
 {?)}         # Push a code block that computes an index and increments it.
     +        # Concatenate that block with the needle block.
      \1$     # Swap the block with the arrays of haystacks and copy the block.
         ,    # Filter: Keep only haystacks for which the code block
              #         pushed a non-zero value.
          \   # Swap the array of haystacks with the code block.
           $  # Sort: Arrange the haystacks according to the values
              #       pushed by the code block.
            ` # Inspect: Format the array for pretty printing.

3

JavaScript ES6,68个字节

(s,l,f=j=>j.indexOf(s))=>l.filter(w=>~f(w)).sort((a,b)=>f(a)>f(b))

这是一个匿名函数,它接受参数s(文件路径字符串)和l(字符串数组)。下面的堆栈摘录包含转换为ES5的原始代码,因此更多的人可以轻松对其进行测试。(如果您使用的是Firefox,则可以使用在他的答案中找到的edc65更漂亮的测试套件。)

f=function(s,l){
  g=function(j){
    return j.search(s)
  }
  
  return l.filter(function(w){
    return ~g(w)
  }).sort(function(a,b){
    return g(a)>g(b)
  })
}

id=document.getElementById;run=function(){document.getElementById('output').innerHTML=f(document.getElementById('s').value,document.getElementById('l').value.split(', ')).join(', ')};document.getElementById('run').onclick=run;run()
<label>File path: <input type="text" id="s" value="mig" /></label><br />
<label>Files: <input type="text" id="l" value="imig, mig, migd, do, Mig" /></label><br />
<button id="run">Run</button><br />
Output: <output id="output"></output>


哇!愚蠢的我浪费时间准备测试套件!
edc65

3

[保留] Pyth,24字节

JwKcwdVlK=G.)KI}JGaYG))Y

试试这里

我在Code Golfing / Pyth还是个新手,所以不确定是否最佳,但我正在努力!

更新:我认为我实际上没有正确排序,而且似乎无法正常工作。我知道这o是按顺序排序的,因此我需要按S的位置进行排序,因此我正在使用.:GlJ该命令来找到当前元素的S长度的所有子字符串,G然后x才能找到第一次出现的索引S,但我似乎无法正确设置lambda。


签出zQ。使用它们会立即为您提供18个字节。并且您可以删除lin VlK=> 17个字节(链接
Jakube

顺便说一句,您的代码不起作用。尝试测试案例:imig mig migd do Mig imig
Jakube

我有一个工作的9字节解决方案。如果您需要有关Pyth的任何帮助,请加入聊天。
2015年

哦,太酷了!我会设法弄清楚你今晚是如何做到的。感谢您的所有帮助!(在我聊天之前,您需要再赢得1个声望点:P)
cmxu

1
@Changming告诉你一点。:)
kirbyfan64sos

2

JavaScript(ES6),68

这几乎与@NBM答案相同(即使未复制),所以我不希望投票。还是喜欢这段代码

一个具有字符串和字符串数组参数的函数,返回一个字符串数组。过滤然后排序。

测试以下代码段(仅适用于EcmaScript 6,仅Firefox)

f=(s,l,i=t=>t.indexOf(s))=>l.filter(t=>~i(t)).sort((t,u)=>i(t)-i(u))

$(function(){
  $("#S,#L").on("keyup", 
   function() { 
     $('#O').val(f(S.value,L.value.split('\n')).join('\n'))
   } );
  $("#S").trigger('keyup');
})
#S,#L,#O { width: 400px }
#L,#O { height: 100px }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Input String<br><input id=S value='mig'><br>
Input List<br><textarea id=L>
imig
mig
migd
do
Mig
</textarea><br>
Output List<br><textarea id=O readonly></textarea>


2

甲骨文,60

这算吗?

select * from t where a like '%mig%' order by instr(a,'mig')


它可以计数,但是它必须是一个以数据为参数的Oracle 过程
kirbyfan64sos

无论是否重要,我都认为很酷。我真正了解的第一个代码高尔夫解决方案。做得好!
Thomas Weller 2015年

@ThomasWeller谢谢!这些打高尔夫球的人肯定可以很聪明,但是有时候KISS就是您所需要的!
MonkeyZeus

2

哈斯克尔,129 116

116(感谢Franky):

import Data.List
h s=map snd.sort.map(\x->((head[c|c<-[0..length x],isPrefixOf s(drop c x)]),x)).filter(isInfixOf s)

129:

import Data.List
f s n=map snd(sort(map(\x->((head [c|c<-[0..length x],isPrefixOf s(drop c x)]),x))(filter(\x->isInfixOf s x)n)))

好吧,这已经很长了,也许我会发现如何缩短它的...


1
刮除13:h s=map snd.sort.map(\x->((head[c|c<-[0..length x],isPrefixOf s(drop c x)]),x)).filter(isInfixOf s)
Franky

2

Python 2,69 68 66字节

我刚刚创建了一个函数,该函数s将字符串作为匹配字符串列表的字符串n

编辑1:感谢Jakube打高尔夫球。

lambda s,n:sorted([x for x in n if s in x],key=lambda x:x.find(s))

在这里查看。


1

红宝石,63岁

p=->(w,l){l.find_all{|x|x[w]}.sort{|a,b|a.index(w)-b.index(w)}}

irb(main):022:0> p["mig", ["imig", "mig", "migd", "do", "Mig"]]
=> ["migd", "mig", "imig"]

笔记

  1. 首先找到所有匹配的词 find_all
  2. 按要搜索的单词的索引位置排序。

编辑(作者:daneiro)

Ruby,49岁

p=->w,l{l.select{|x|x[w]}.sort_by{|e|e.index(w)}}

1
49个字符中的相同内容:p=->w,l{l.select{|x|x[w]}.sort_by{|e|e.index(w)}}
daniero

@daniero请张贴它作为您的答案。我会投票!
bsd

1
不,真的可以:)我的版本只是您的改进,我认为它们太相似了,无法单独回答。select是一个别名find_all,,并sortsort_by 基本处于略微不同的包装材料同样的事情。我会为您
投票

0

球拍46字节

(for/list((i l)#:when(string-contains? i s))i)

用法:

(define (f s l)
 (for/list((i l)#:when(string-contains? i s))i))

测试:

(f "mig" '["imig" "mig" "migd" "do" "Mig"])

输出:

'("imig" "mig" "migd")


0

点子,15字节

14个字节的代码,-p标志+1 。

Yq_@?ySKyN_FIg

将列表作为命令行参数和来自stdin的字符串。在线尝试!

说明

Yq              Yank a line of stdin into y
           FIg  Filter array of cmdline args by this function:
        yN_       Count occurrences of y in arg
      SK        Sort the resulting list using this key function:
  _@?y            Index of y in arg
                Print the Pip representation of the list (implicit, -p flag)
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.