ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# cedricvaneer's solution

## to Hamming in the CFML Track

Published at Jan 20 2020 · 0 comments
Instructions
Test suite
Solution

Calculate the Hamming Distance between two DNA strands.

Your body is made up of cells that contain DNA. Those cells regularly wear out and need replacing, which they achieve by dividing into daughter cells. In fact, the average human body experiences about 10 quadrillion cell divisions in a lifetime!

When cells divide, their DNA replicates too. Sometimes during this process mistakes happen and single pieces of DNA get encoded with the incorrect information. If we compare two strands of DNA and count the differences between them we can see how many mistakes occurred. This is known as the "Hamming Distance".

We read DNA using the letters C,A,G and T. Two strands might look like this:

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

They have 7 differences, and therefore the Hamming Distance is 7.

The Hamming Distance is useful for lots of things in science, not just biology, so it's a nice phrase to be familiar with :)

# Implementation notes

The Hamming distance is only defined for sequences of equal length, so an attempt to calculate it between sequences of different lengths should not work. The general handling of this situation (e.g., raising an exception vs returning a special value) may differ between languages.

To run the code in this exercise, you will only need to have CommandBox CLI installed. This binary runs CFML code from the command line.

To run the tests, `cd` into the exercise folder and run the following:

``````box task run TestRunner
# Or start up a test watcher that will rerun when files change
``````

The tests leverage a library called TestBox which supports xUnit and BDD style of testing. All test suites will be written in the BDD style which uses closures to define test specs. You won't need to worry about installing TestBox. The CLI test runner will take care of that for you. You just need to be connected to the internet the first time you run it. You can read more about it here:

https://testbox.ortusbooks.com/content/

## 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.

### HammingTest.cfc

``````component extends="testbox.system.BaseSpec" {

function beforeAll(){
SUT = createObject( 'Hamming' );
}

function run(){

describe( "My Hamming class", function(){

it( 'empty strands', function(){
expect( SUT.distance( strand1='', strand2='' ) ).toBe( '0' );
});

it( 'identical strands', function(){
expect( SUT.distance( strand1='A', strand2='A' ) ).toBe( '0' );
});

it( 'long identical strands', function(){
expect( SUT.distance( strand1='GGACTGA', strand2='GGACTGA' ) ).toBe( '0' );
});

it( 'complete distance in single nucleotide strands', function(){
expect( SUT.distance( strand1='A', strand2='G' ) ).toBe( '1' );
});

it( 'complete distance in small strands', function(){
expect( SUT.distance( strand1='AG', strand2='CT' ) ).toBe( '2' );
});

it( 'small distance in small strands', function(){
expect( SUT.distance( strand1='AT', strand2='CT' ) ).toBe( '1' );
});

it( 'small distance', function(){
expect( SUT.distance( strand1='GGACG', strand2='GGTCG' ) ).toBe( '1' );
});

it( 'small distance in long strands', function(){
expect( SUT.distance( strand1='ACCAGGG', strand2='ACTATGG' ) ).toBe( '2' );
});

it( 'non-unique character in first strand', function(){
expect( SUT.distance( strand1='AAG', strand2='AAA' ) ).toBe( '1' );
});

it( 'non-unique character in second strand', function(){
expect( SUT.distance( strand1='AAA', strand2='AAG' ) ).toBe( '1' );
});

it( 'same nucleotides in different positions', function(){
expect( SUT.distance( strand1='TAG', strand2='GAT' ) ).toBe( '2' );
});

it( 'large distance', function(){
expect( SUT.distance( strand1='GATACA', strand2='GCATAA' ) ).toBe( '4' );
});

it( 'large distance in off-by-one strand', function(){
expect( SUT.distance( strand1='GGACGGATTCTG', strand2='AGGACGGATTCT' ) ).toBe( '9' );
});

it( 'disallow first strand longer', function(){
expect( function(){ SUT.distance( strand1='AATG', strand2='AAA' ); } ).toThrow( message='left and right strands must be of equal length' );
});

it( 'disallow second strand longer', function(){
expect( function(){ SUT.distance( strand1='ATA', strand2='AGTG' ); } ).toThrow( message='left and right strands must be of equal length' );
});

});

}

}``````

### SolutionTest.cfc

``````component extends="HammingTest" {

function beforeAll(){
SUT = createObject( 'Solution' );
}

}``````
``````component {

public numeric function distance( required string strand1, required string strand2 ) {

var array1 = listToArray(arguments.strand1, "");
var array2 = listToArray(arguments.strand2, "");
var hammingDistance = 0;
if (ArrayLen(array1) != ArrayLen(array2)) {
throw("left and right strands must be of equal length")
}
for (var i = 1; i <= len(array1); i++) {
if (array1[i] != array2[i]) {
hammingDistance += 1;
}
}
return hammingDistance;
}
}``````