Skip to content

Commit a048e9e

Browse files
authored
Add Hamming distance for strings (#15)
1 parent 0428a2a commit a048e9e

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
describe("Hamming distance", function()
2+
local hamming_distance = require("string.distance.hamming")
3+
4+
local function check_basic(a, b, expected)
5+
assert.equal(expected, hamming_distance(a, b))
6+
assert.equal(0, hamming_distance(a, a))
7+
end
8+
9+
local function check_with_reversed_inputs(a, b, expected)
10+
assert.equal(expected, hamming_distance(a:reverse(), b:reverse()))
11+
assert.equal(0, hamming_distance(a:reverse(), a:reverse()))
12+
end
13+
14+
local function check_all(a, b, expected)
15+
check_basic(a, b, expected)
16+
check_with_reversed_inputs(a, b, expected)
17+
end
18+
19+
local function test(a, b, expected)
20+
check_all(a, b, expected)
21+
check_all(b, a, expected)
22+
end
23+
24+
it("should handle general cases", function()
25+
test("", "", 0)
26+
test("a", "a", 0)
27+
test("a", "A", 1)
28+
test("cąx", "cąy", 1)
29+
test("mama", "tata", 2)
30+
test("xxx", "Xxx", 1)
31+
test("1234", "2345", 4)
32+
end)
33+
34+
it("should throw error for inputs of different length", function()
35+
assert.has_error(function()
36+
hamming_distance("z", "")
37+
end)
38+
end)
39+
end)

src/string/distance/hamming.lua

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
-- Computes the Hamming "edit" distance for two strings of the same length:
2+
-- The number of indices at which the corresponding bytes are different.
3+
return function(
4+
a, -- some string
5+
b -- other string
6+
)
7+
assert(#a == #b, "lengths don't match")
8+
local dist = 0
9+
for i = 1, #a do
10+
if a:byte(i) ~= b:byte(i) then
11+
dist = dist + 1
12+
end
13+
end
14+
return dist
15+
end

0 commit comments

Comments
 (0)