This is my code for the Hamming Distance exercise:
function Hamming.compute(a, b)
local hamming_distance = 0
local start_index = 1
local end_index = 1
if #a ~= #b then
return "no"
elseif
#a == #b then
while not(start_index > #a) do
local new_a = string.sub(a, start_index, end_index)
local new_b = string.sub(b, start_index, end_index)
if new_a == new_b and not(start_index == #a and #a > 1) then
hamming_distance = hamming_distance + 1
start_index = start_index +1
end_index = end_index + 1
elseif (new_a ~= new_b) and not (start_index == #a and #a > 1) then
start_index = start_index +1
end_index = end_index + 1
end
end
end
end
return hamming_distance
I keep getting the error "./hamming.lua:1: attempt to index a nil value (global 'Hamming')" and I can't for the life of me figure out why.
I have peeked at the community solutions so I understand why and how other solutions work, I just can't wrap my head around why mine doesn't work.
Can anyone explain?