Jump to content

Levenshtein distance: Difference between revisions

→‎{{header|Raku}}: swap out underscores for hyphens and spaces
No edit summary
(→‎{{header|Raku}}: swap out underscores for hyphens and spaces)
Line 4,817:
(formerly Perl 6)
{{works with|rakudo|2015-09-16}}
Implementation of the wikipediaWikipedia algorithm. Since column 0 and row 0 are used for base distances, the original algorithm would require us to compare "@s[$i-1] eq @t[$j-1]", and reference the $m and $n separately. Prepending an unused value (undef) onto @s and @t makes their indices align with the $i,$j numbering of @d, and lets us use .end instead of $m,$n.
<lang perl6>sub levenshtein_distancelevenshtein-distance ( Str $s, Str $t --> Int ) {
my @s = *, |$s.comb;
my @t = *, |$t.comb;
Line 4,841:
 
for @a -> [$s, $t] {
say "levenshtein_distanceLevenshtein distance('$s', '$t') == ", levenshtein_distancelevenshtein-distance($s, $t);
}</lang>
{{out}}
<pre>levenshtein_distanceLevenshtein distance('kitten', 'sitting') == 3
levenshtein_distanceLevenshtein distance('saturday', 'sunday') == 3
levenshtein_distanceLevenshtein distance('rosettacode', 'raisethysword') == 8</pre>
 
=={{header|REXX}}==
1,480

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.