Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
21
Task/Levenshtein-distance/Clojure/levenshtein-distance-2.clj
Normal file
21
Task/Levenshtein-distance/Clojure/levenshtein-distance-2.clj
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
(defn levenshtein [w1 w2]
|
||||
(letfn [(cell-value [same-char? prev-row cur-row col-idx]
|
||||
(min (inc (nth prev-row col-idx))
|
||||
(inc (last cur-row))
|
||||
(+ (nth prev-row (dec col-idx)) (if same-char?
|
||||
0
|
||||
1))))]
|
||||
(loop [row-idx 1
|
||||
max-rows (inc (count w2))
|
||||
prev-row (range (inc (count w1)))]
|
||||
(if (= row-idx max-rows)
|
||||
(last prev-row)
|
||||
(let [ch2 (nth w2 (dec row-idx))
|
||||
next-prev-row (reduce (fn [cur-row i]
|
||||
(let [same-char? (= (nth w1 (dec i)) ch2)]
|
||||
(conj cur-row (cell-value same-char?
|
||||
prev-row
|
||||
cur-row
|
||||
i))))
|
||||
[row-idx] (range 1 (count prev-row)))]
|
||||
(recur (inc row-idx) max-rows next-prev-row))))))
|
||||
Loading…
Add table
Add a link
Reference in a new issue