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

The-F00L's solution

to Hamming in the CFML Track

Published at Apr 12 2021 · 0 comments
Test suite

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:

^ ^ ^  ^ ^    ^^

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

* Your implmentation of the Hamming exercise
component {
	* @returns 
	 function distance( strand1, strand2 ) {
		if (len(strand1)!=len(strand2)) {
			throw "not equal seq";
		for (i = 1; i < arrayLen(strand1)+1; i++) {
		return diff;

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?