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

Published on 12 May 2024 09:40 PM

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

Source code in the bbc programming language

      PRINT "'kitten' -> 'sitting' has distance " ;
      PRINT ; FNlevenshtein("kitten", "sitting")
      PRINT "'rosettacode' -> 'raisethysword' has distance " ;
      PRINT ; FNlevenshtein("rosettacode", "raisethysword")
      END
      
      DEF FNlevenshtein(s$, t$)
      LOCAL i%, j%, m%, d%()
      DIM d%(LENs$, LENt$)
      FOR i% = 0 TO DIM(d%(),1)
        d%(i%,0) = i%
      NEXT
      FOR j% = 0 TO DIM(d%(),2)
        d%(0,j%) = j%
      NEXT
      FOR j% = 1 TO DIM(d%(),2)
        FOR i% = 1 TO DIM(d%(),1)
          IF MID$(s$,i%,1) = MID$(t$,j%,1) THEN
            d%(i%,j%) = d%(i%-1,j%-1)
          ELSE
            m% = d%(i%-1,j%-1)
            IF d%(i%,j%-1) < m% m% = d%(i%,j%-1)
            IF d%(i%-1,j%) < m% m% = d%(i%-1,j%)
            d%(i%,j%) = m% + 1
          ENDIF
        NEXT
      NEXT j%
      = d%(i%-1,j%-1)


  

You may also check:How to resolve the algorithm Detect division by zero step by step in the Seed7 programming language
You may also check:How to resolve the algorithm Forest fire step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Draw a pixel step by step in the GB BASIC programming language
You may also check:How to resolve the algorithm Primality by trial division step by step in the Nim programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the PDP-1 Assembly programming language