-
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Hamming distance for strings (#15)
- Loading branch information
Showing
2 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
describe("Hamming distance", function() | ||
local hamming_distance = require("string.distance.hamming") | ||
|
||
local function check_basic(a, b, expected) | ||
assert.equal(expected, hamming_distance(a, b)) | ||
assert.equal(0, hamming_distance(a, a)) | ||
end | ||
|
||
local function check_with_reversed_inputs(a, b, expected) | ||
assert.equal(expected, hamming_distance(a:reverse(), b:reverse())) | ||
assert.equal(0, hamming_distance(a:reverse(), a:reverse())) | ||
end | ||
|
||
local function check_all(a, b, expected) | ||
check_basic(a, b, expected) | ||
check_with_reversed_inputs(a, b, expected) | ||
end | ||
|
||
local function test(a, b, expected) | ||
check_all(a, b, expected) | ||
check_all(b, a, expected) | ||
end | ||
|
||
it("should handle general cases", function() | ||
test("", "", 0) | ||
test("a", "a", 0) | ||
test("a", "A", 1) | ||
test("cąx", "cąy", 1) | ||
test("mama", "tata", 2) | ||
test("xxx", "Xxx", 1) | ||
test("1234", "2345", 4) | ||
end) | ||
|
||
it("should throw error for inputs of different length", function() | ||
assert.has_error(function() | ||
hamming_distance("z", "") | ||
end) | ||
end) | ||
end) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
-- Computes the Hamming "edit" distance for two strings of the same length: | ||
-- The number of indices at which the corresponding bytes are different. | ||
return function( | ||
a, -- some string | ||
b -- other string | ||
) | ||
assert(#a == #b, "lengths don't match") | ||
local dist = 0 | ||
for i = 1, #a do | ||
if a:byte(i) ~= b:byte(i) then | ||
dist = dist + 1 | ||
end | ||
end | ||
return dist | ||
end |