How to resolve the algorithm Longest common substring step by step in the Lambdatalk programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Longest common substring step by step in the Lambdatalk programming language

Table of Contents

Problem Statement

Write a function that returns the longest common substring of two strings. Use it within a program that demonstrates sample output from the function, which will consist of the longest common substring between "thisisatest" and "testing123testing". Note that substrings are consecutive characters within a string.   This distinguishes them from subsequences, which is any sequence of characters within a string, even if there are extraneous characters in between them. Hence, the longest common subsequence between "thisisatest" and "testing123testing" is "tsitest", whereas the longest common substring is just "test".

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Longest common substring step by step in the Lambdatalk programming language

Source code in the lambdatalk programming language

{def lcs
 {def lcs.rec
  {lambda {:a :b :w}
   {if {or {< {W.length :a} 2} {< {W.length :b} 2} }
    then {W.rest :w}
    else {if {W.equal? {W.first :a} {W.first :b}} 
          then {lcs.rec {W.rest :a} {W.rest :b} :w{W.first :a}}
          else {let { {:x {lcs.rec :a {W.rest :b} :w}} 
                      {:y {lcs.rec {W.rest :a} :b :w}}
                    } {if {> {W.length :x} {W.length :y}}
                       then :x
                       else :y} }}}}}
 {lambda {:a :b}
  {lcs.rec :a# :b# #}}}  
-> lcs 

{lcs testing123testing thisisatest}
-> tsitest   // 23000ms


{jslcs testing123testing thisisatest}
-> tsitest    // 130ms

{script 
  // the lcs function code is in the javascript entry

  LAMBDATALK.DICT["jslcs"] = function() {
    var args = arguments[0].split(" ");
    return lcs( args[0], args[1] )
  };
}


  

You may also check:How to resolve the algorithm AKS test for primes step by step in the Scheme programming language
You may also check:How to resolve the algorithm Fraction reduction step by step in the Haskell programming language
You may also check:How to resolve the algorithm Variable declaration reset step by step in the Java programming language
You may also check:How to resolve the algorithm Roman numerals/Decode step by step in the 8080 Assembly programming language
You may also check:How to resolve the algorithm XML/Input step by step in the AArch64 Assembly programming language