#Lua: Hamming Distance

15 messages · Page 1 of 1 (latest)

lost leaf
#

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?

gleaming horizon
#

Does the test code section show what values are being tested?

hushed knot
#

@lost leaf How about enclosing the beginning and end of your code with 3 backticks (`) to use the code block?

lost leaf
lost leaf
# lost leaf Yes it does

Here:

local compute = require('hamming').compute
describe('hamming', function()
  it('identical strands', function()
    assert.are.equal(0, compute('A', 'A'))
  end)
  it('long identical strands', function()
   assert.are.equal(0, compute('GGACTGA', 'GGACTGA')) 
  end)
  it('complete distance in single nucleotide strands', function()
    assert.are.equal(1, compute('A', 'G'))
  end)     
  it('complete distance in small strands', function()
assert.are.equal(2, compute('AG', 'CT'))
  end)     
  it('small distance in small strands', function()
    assert.are.equal(1, compute('AT', 'CT'))
  end)
  it('small distance', function()      
    assert.are.equal(1, compute('GGACG', 'GGTCG'))
  end)         
  it('small distance in long strands', function()
    assert.are.equal(2, compute('ACCAGGG', 'ACTATGG'))
  end)
  it('non unique character in first strand', function()
    assert.are.equal(1, compute('AGA', 'AGG'))
  end)
  it('non unique character in second strand', function()
    assert.are.equal(1, compute('AGG', 'AGA'))
  end)
  it('same nucleotides in different positions', function()
    assert.are.equal(2, compute('TAG', 'GAT'))
  end)
  it('large distance', function()
    assert.are.equal(4, compute('GATACA', 'GCATAA'))
  end)
  it('large distance in off-by-one strand', function()
    assert.are.equal(9, compute('GGACGGATTCTG', 'AGGACGGATTCT'))
  end)
  it('empty strands', function()
    assert.are.equal(0, compute('', ''))
  end)
  it('disallow first strand longer', function()
    assert.are.equal(-1, compute('AATG', 'AAA'))
  end)
   it('disallow second strand longer', function()
    assert.are.equal(-1, compute('ATA', 'AGTG'))
#

Please forgive the length and the absoltuely atrocious formatting, I tried 😅

hushed knot
lost leaf
hushed knot
lost leaf
hushed knot
lost leaf
#

Thank you!

hushed knot
#

@lost leaf I can't advise you further, but one option might be to submit your researched solutions, request for mentoring, explain the situation and ask for feedback on the problems in your original code.