1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
alias levens { var %lenA = $len($1), %lenB = $len($2) if (%lenA == 0) return %lenB if (%lenB == 0) return %lenA var %i = 0 while (%i <= %lenB) { var %j = 0 while (%j <= %lenA) { ;if %i or %j are 0 set up the initial var for matrix if (%j == 0 || %i == 0) { set -l %matrix. [ $+ [ %j ] $+ . $+ [ %i ] ] $calc(%j + %i) | inc %j | continue } ;Find the cost if they're the same letter its 0 else its 1 var %cost = $iif($mid($1,%j,1) == $mid($2,%i,1),0,1) ;a) total of possision top+1 var %a = $calc(%matrix. [ $+ [ $calc(%j - 1) ] $+ . $+ [ %i ] ] + 1) ;b) total of possision left+1 var %b = $calc(%matrix. [ $+ [ %j ] $+ . $+ [ $calc(%i - 1) ] ] + 1) ;c) total of posistion top+left + cost var %c = $calc(%matrix. [ $+ [ $calc(%j - 1) ] $+ . $+ [ $calc(%i - 1) ] ] + %cost) ;find the lowest of %a %b %c and set to the matrix set -l %matrix. [ $+ [ %j ] $+ . $+ [ %i ] ] $gettok($sorttok(%a %b %c,32,n),1,32) inc %j } inc %i } ;Show matrix in debug mode /* var %i = 0 while (%i <= %lenB) { var %j = 0, %x = $null while (%j <= %lenA) { ;set up the outside border replace 0 with letters if (%i == 0) && (%j == 0) set %matrix.0.0 $chr(160) elseif (%i == 0) set %matrix. [ $+ [ %j ] $+ ] .0 $mid($1,%j,1) elseif (%j == 0) set %matrix.0. [ $+ [ %i ] ] $mid($2,%i,1) ;set %x to the whole line across plus make sure we add the | as the second char for the grid. var %x = %x %matrix. [ $+ [ %j ] $+ . $+ [ %i ] ] $iif(%j == 0,|) inc %j } echo -ag %x ;add a row of - the same len as %x for the second line. if (%i == 0) echo -ag $str(-,$len(%x)) inc %i } */ ;return the amount left in the matrix at the bottom right location. return %matrix. [ $+ [ %lenA ] $+ . $+ [ %lenB ] ] } |
| Parsed for bracket errors and indented: | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
alias levens { var %lenA = $len($1), %lenB = $len($2) if (%lenA == 0) return %lenB if (%lenB == 0) return %lenA var %i = 0 while (%i <= %lenB) { var %j = 0 while (%j <= %lenA) { ;if %i or %j are 0 set up the initial var for matrix if (%j == 0 || %i == 0) { set -l %matrix. [ $+ [ %j ] $+ . $+ [ %i ] ] $calc(%j + %i) | inc %j | continue } ;Find the cost if they're the same letter its 0 else its 1 var %cost = $iif($mid($1,%j,1) == $mid($2,%i,1),0,1) ;a) total of possision top+1 var %a = $calc(%matrix. [ $+ [ $calc(%j - 1) ] $+ . $+ [ %i ] ] + 1) ;b) total of possision left+1 var %b = $calc(%matrix. [ $+ [ %j ] $+ . $+ [ $calc(%i - 1) ] ] + 1) ;c) total of posistion top+left + cost var %c = $calc(%matrix. [ $+ [ $calc(%j - 1) ] $+ . $+ [ $calc(%i - 1) ] ] + %cost) ;find the lowest of %a %b %c and set to the matrix set -l %matrix. [ $+ [ %j ] $+ . $+ [ %i ] ] $gettok($sorttok(%a %b %c,32,n),1,32) inc %j } inc %i } ;Show matrix in debug mode /* var %i = 0 while (%i <= %lenB) { var %j = 0, %x = $null while (%j <= %lenA) { ;set up the outside border replace 0 with letters if (%i == 0) && (%j == 0) set %matrix.0.0 $chr(160) elseif (%i == 0) set %matrix. [ $+ [ %j ] $+ ] .0 $mid($1,%j,1) elseif (%j == 0) set %matrix.0. [ $+ [ %i ] ] $mid($2,%i,1) ;set %x to the whole line across plus make sure we add the | as the second char for the grid. var %x = %x %matrix. [ $+ [ %j ] $+ . $+ [ %i ] ] $iif(%j == 0,|) inc %j } echo -ag %x ;add a row of - the same len as %x for the second line. if (%i == 0) echo -ag $str(-,$len(%x)) inc %i } */ ;return the amount left in the matrix at the bottom right location. return %matrix. [ $+ [ %lenA ] $+ . $+ [ %lenB ] ] } |