From 1440839d153c33ae9216bef959c38affab2d32b0 Mon Sep 17 00:00:00 2001 From: Abigail Date: Thu, 21 Jan 2021 16:48:08 +0100 Subject: Add some standard comments and hash bang lines. --- challenge-096/abigail/lua/ch-2.lua | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) mode change 100644 => 100755 challenge-096/abigail/lua/ch-2.lua (limited to 'challenge-096/abigail/lua/ch-2.lua') diff --git a/challenge-096/abigail/lua/ch-2.lua b/challenge-096/abigail/lua/ch-2.lua old mode 100644 new mode 100755 index 162f06eef0..ef1b039579 --- a/challenge-096/abigail/lua/ch-2.lua +++ b/challenge-096/abigail/lua/ch-2.lua @@ -1,3 +1,13 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-2.lua < input-file +-- + -- -- This is an implementation of the Wagner Fischer algorithm, which -- calculates the Levenshtein distance. @@ -7,11 +17,6 @@ function LevenshteinDistance (first, second) local n = first : len () local m = second : len () - if n < m - then - first, second = second, first - n, m = m, n - end distances = {} for i = 0, n do distances [i] = {} @@ -37,7 +42,7 @@ function LevenshteinDistance (first, second) -- -- We only need the previous row, so we can return the -- memory of rows before that. This reduces the memory - -- usage from O (n * m) to O (min (n, m)) + -- usage from Theta (n * m) to O (n + m) -- if i > 0 then -- cgit From 1c9975a4c79a02569f92310cf5e30ccd2b542033 Mon Sep 17 00:00:00 2001 From: Abigail Date: Sat, 23 Jan 2021 21:44:49 +0100 Subject: Use 'distance' instead of 'distances' for the array name. --- challenge-096/abigail/lua/ch-2.lua | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'challenge-096/abigail/lua/ch-2.lua') diff --git a/challenge-096/abigail/lua/ch-2.lua b/challenge-096/abigail/lua/ch-2.lua index ef1b039579..99058213ca 100755 --- a/challenge-096/abigail/lua/ch-2.lua +++ b/challenge-096/abigail/lua/ch-2.lua @@ -17,13 +17,13 @@ function LevenshteinDistance (first, second) local n = first : len () local m = second : len () - distances = {} + distance = {} for i = 0, n do - distances [i] = {} + distance [i] = {} for j = 0, m do if i == 0 or j == 0 then - distances [i] [j] = i + j + distance [i] [j] = i + j else local cost = 1 if string . sub (first, i, i) == @@ -31,10 +31,10 @@ function LevenshteinDistance (first, second) then cost = 0 end - distances [i] [j] = math . min ( - distances [i - 1] [j] + 1, - distances [i] [j - 1] + 1, - distances [i - 1] [j - 1] + cost + distance [i] [j] = math . min ( + distance [i - 1] [j] + 1, + distance [i] [j - 1] + 1, + distance [i - 1] [j - 1] + cost ) end end @@ -46,10 +46,10 @@ function LevenshteinDistance (first, second) -- if i > 0 then - distances [i - 1] = nil + distance [i - 1] = nil end end - return distances [n] [m] + return distance [n] [m] end -- cgit