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

petertseng's solution

to Hamming in the CFML Track

Published at Jul 13 2018 · 0 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.

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
box task run TestRunner --:watcher

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:



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.


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' );



component extends="HammingTest" {

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

component {
  * @returns the Hamming distance between the two
  function distance(strand1, strand2) {
    if (strand1.len() != strand2.len()) {
      throw 'unequal length';
    return strand1.map(function(c, i) {
      return c == strand2[i] ? '' : 'x';
    }, '').len();

Community comments

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

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?