How to resolve the algorithm Levenshtein distance step by step in the zkl programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Levenshtein distance step by step in the zkl 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 zkl programming language

Source code in the zkl programming language

fcn levenshtein(s1,s2){
   sz2,costs:=s2.len() + 1, List.createLong(sz2,0);  // -->zero filled List
   foreach i in (s1.len() + 1){
      lastValue:=i;
      foreach j in (sz2){
         if (i==0) costs[j]=j;
	 else if (j>0){
	    newValue:=costs[j-1];
	    if (s1[i-1]!=s2[j-1])
	       newValue=newValue.min(lastValue, costs[j]) + 1;
	    costs[j-1]=lastValue;
	    lastValue =newValue;
	 }
      }
      if (i>0) costs[-1]=lastValue;
   }
   costs[-1]
}

foreach a,b in (T(T("kitten","sitting"), T("rosettacode","raisethysword"),
	T("yo",""), T("","yo"), T("abc","abc")) ){
   println(a," --> ",b,": ",levenshtein(a,b));
}

  

You may also check:How to resolve the algorithm Jump anywhere step by step in the Nim programming language
You may also check:How to resolve the algorithm Horner's rule for polynomial evaluation step by step in the Wren programming language
You may also check:How to resolve the algorithm 15 puzzle game step by step in the Julia programming language
You may also check:How to resolve the algorithm One-dimensional cellular automata step by step in the C# programming language
You may also check:How to resolve the algorithm Anagrams/Deranged anagrams step by step in the Sidef programming language