Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 35s
Recursive Levenshtein edit distance with no memoization (the test
strings are short enough for the exponential-without-memo version to
fit in <2 minutes on contended hosts). Sums distances for five short
pairs:
('abc','abx') + ('ab','ba') + ('abc','axyc') + ('','abcd') + ('ab','')
= 1 + 2 + 2 + 4 + 2 = 11
Exercises:
* curried four-arg recursion
* s.[i] equality test (char comparison)
* min nested twice for the three-way recurrence
* mixed empty-string base cases
19 lines
400 B
OCaml
19 lines
400 B
OCaml
let rec lev s1 s2 i j =
|
|
if i = 0 then j
|
|
else if j = 0 then i
|
|
else if s1.[i - 1] = s2.[j - 1] then
|
|
lev s1 s2 (i - 1) (j - 1)
|
|
else
|
|
1 + min (lev s1 s2 (i - 1) j)
|
|
(min (lev s1 s2 i (j - 1)) (lev s1 s2 (i - 1) (j - 1)))
|
|
|
|
let dist s1 s2 = lev s1 s2 (String.length s1) (String.length s2)
|
|
|
|
;;
|
|
|
|
dist "abc" "abx"
|
|
+ dist "ab" "ba"
|
|
+ dist "abc" "axyc"
|
|
+ dist "" "abcd"
|
|
+ dist "ab" ""
|