🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of JaeHyoLee

JaeHyoLee's solution

to Hamming in the Lua Track

Published at Jul 13 2018 · 2 comments
Test suite

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.

^ ^ ^  ^ ^    ^^

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.


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.


local compute = require('hamming').compute

describe('hamming', function ()
  it('identical strands', function ()
    assert.are.equal(0, compute('A', 'A'))

  it('long identical strands', function ()
    assert.are.equal(0, compute('GGACTGA', 'GGACTGA'))

  it('complete distance in single nucleotide strands', function ()
    assert.are.equal(1, compute('A', 'G'))

  it('complete distance in small strands', function ()
    assert.are.equal(2, compute('AG', 'CT'))

  it('small distance in small strands', function ()
    assert.are.equal(1, compute('AT', 'CT'))

  it('small distance', function ()
    assert.are.equal(1, compute('GGACG', 'GGTCG'))

  it('small distance in long strands', function ()
    assert.are.equal(2, compute('ACCAGGG', 'ACTATGG'))

  it('non unique character in first strand', function ()
    assert.are.equal(1, compute('AGA', 'AGG'))

  it('non unique character in second strand', function ()
    assert.are.equal(1, compute('AGG', 'AGA'))

  it('same nucleotides in different positions', function ()
    assert.are.equal(2, compute('TAG', 'GAT'))

  it('large distance', function ()
    assert.are.equal(4, compute('GATACA', 'GCATAA'))

  it('large distance in off-by-one strand', function ()
    assert.are.equal(9, compute('GGACGGATTCTG', 'AGGACGGATTCT'))

  it('empty strands', function ()
    assert.are.equal(0, compute('', ''))

  it('disallow first strand longer', function ()
    assert.are.equal(-1, compute('AATG', 'AAA'))

  it('disallow second strand longer', function ()
    assert.are.equal(-1, compute('ATA', 'AGTG'))
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
    return distance

Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of aarti

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)

Avatar of JaeHyoLee

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