点距50
不使用内置的Levenshtein函数!
xINg?#JgMN[1+(fac:b@>1)1+(fe:a@>1b)(a@0NEb@0)+(fec)]
该代码实现了递归Levenshtein算法 ; 因此,它非常慢,即使长度为5的字符串也要花费几秒钟。我不建议自己运行程序来检查它!
这是我的基本代码,带有空格和注释:
; Note: a Pip program is an implicit function f, which is called with the command-line
; arguments. The args are stored in the list g, as well as being assigned to the local
; variables a-e.
; Is one of the args the empty string? (NB x is initialized to "")
x IN g ?
; If so, join args together and take the length (i.e., length of the non-empty string).
# J g
; If not, take the min of the following:
MN [
; Recursively call f with the first character of a removed; add 1 to the result
(f a@>1 b) + 1
; Recursively call f with the first character of b removed; add 1 to the result
(f a b@>1) + 1
; Recursively call f with the first characters of both removed; iff the two characters
; were not equal, add 1 to the result
(f a@>1 b@>1) + (a@0 NE b@0)
]
最终版本中的主要更改是为一些临时变量c
和赋了一些值e
,这些变量出现在质询字符串中,从而稍微减少了Levenshtein距离。