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.

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.

To run the tests, run the command `busted`

from within the exercise directory.

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.

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

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

```
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
}
```

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?
- Are there new concepts here that you could read more about to improve your understanding?

Level up your programming skills with 3,276 exercises across 51 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments

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)

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