How to resolve the algorithm Levenshtein distance step by step in the Common Lisp programming language
How to resolve the algorithm Levenshtein distance step by step in the Common Lisp programming language
Table of Contents
Problem Statement
In information theory and computer science, the Levenshtein distance is a metric for measuring the amount of difference between two sequences (i.e. an edit distance). The Levenshtein distance between two strings is defined as the minimum number of edits needed to transform one string into the other, with the allowable edit operations being insertion, deletion, or substitution of a single character.
The Levenshtein distance between "kitten" and "sitting" is 3, since the following three edits change one into the other, and there isn't a way to do it with fewer than three edits:
The Levenshtein distance between "rosettacode", "raisethysword" is 8. The distance between two strings is same as that when both strings are reversed.
Implements a Levenshtein distance function, or uses a library function, to show the Levenshtein distance between "kitten" and "sitting".
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Levenshtein distance step by step in the Common Lisp programming language
Source code in the common programming language
(defun levenshtein (a b)
(let* ((la (length a))
(lb (length b))
(rec (make-array (list (1+ la) (1+ lb)) :initial-element nil)))
(labels ((leven (x y)
(cond
((zerop x) y)
((zerop y) x)
((aref rec x y) (aref rec x y))
(t (setf (aref rec x y)
(min (+ (leven (1- x) y) 1)
(+ (leven x (1- y)) 1)
(+ (leven (1- x) (1- y)) (if (char= (char a (- la x)) (char b (- lb y))) 0 1))))))))
(leven la lb))))
(print (levenshtein "rosettacode" "raisethysword"))
You may also check:How to resolve the algorithm Speech synthesis step by step in the Tcl programming language
You may also check:How to resolve the algorithm Sum and product puzzle step by step in the Go programming language
You may also check:How to resolve the algorithm Interactive programming (repl) step by step in the ACL2 programming language
You may also check:How to resolve the algorithm Calculating the value of e step by step in the МК-61/52 programming language
You may also check:How to resolve the algorithm Hostname step by step in the Aikido programming language