 # JaeHyoLee's solution

## to Hamming in the Lua Track

Published at Jul 13 2018 · 2 comments
Instructions
Test suite
Solution

Calculate the Hamming difference between two DNA strands.

A mutation is simply a mistake that occurs during the creation or copying of a nucleic acid, in particular DNA. Because nucleic acids are vital to cellular functions, mutations tend to cause a ripple effect throughout the cell. Although mutations are technically mistakes, a very rare mutation may equip the cell with a beneficial attribute. In fact, the macro effects of evolution are attributable by the accumulated result of beneficial microscopic mutations over many generations.

The simplest and most common type of nucleic acid mutation is a point mutation, which replaces one base with another at a single nucleotide.

By counting the number of differences between two homologous DNA strands taken from different genomes with a common ancestor, we get a measure of the minimum number of point mutations that could have occurred on the evolutionary path between the two strands.

This is called the 'Hamming distance'.

It is found by comparing two DNA strands and counting how many of the nucleotides are different from their equivalent in the other string.

``````GAGCCTACTAACGGGAT
CATCGTAATGACGGCCT
^ ^ ^  ^ ^    ^^
``````

The Hamming distance between these two DNA strands is 7.

# Implementation notes

The Hamming distance is only defined for sequences of equal length. This means that based on the definition, each language could deal with getting sequences of equal length differently.

## Running the tests

To run the tests, run the command `busted` from within the exercise directory.

## Further information

For more detailed information about the Lua track, including how to get help if you're having trouble, please visit the exercism.io Lua language page.

## Source

The Calculating Point Mutations problem at Rosalind http://rosalind.info/problems/hamm/

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### hamming_spec.lua

``````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'))
end)
end)``````
``````return {
compute = function(ancestor, mine)
local distance = 0
for i = 1, #ancestor do
if ancestor:byte(i) ~= mine:byte(i) then
distance = distance + 1 end
end
return distance
end
}`````` ancestor:byte(i) ~= mine:byte(i) neat that you can call the byte method, I had it coded as ancestor:sub(i, i) ~= mine:sub(i, i) Solution Author
commented over 4 years ago

@aarti Thanks. There are lots of useful method that lua built in provides

### What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

• What compromises have been made?