Exercism v3 launches on Sept 1st 2021. Learn more! ๐Ÿš€๐Ÿš€๐Ÿš€
Avatar of GustavoChierici

GustavoChierici's solution

to Hamming in the Erlang Track

Published at Mar 08 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.

Running tests

In order to run the tests, issue the following command from the exercise directory:

For running the tests provided, rebar3 is used as it is the official build and dependency management tool for erlang now. Please refer to the tracks installation instructions on how to do that.

In order to run the tests, you can issue the following command from the exercise directory.

$ rebar3 eunit


For detailed information about the Erlang track, please refer to the help page on the Exercism site. This covers the basic information on setting up the development environment expected by the exercises.


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.


%% Generated with 'testgen v0.2.0'
%% Revision 1 of the exercises generator was used
%% https://github.com/exercism/problem-specifications/raw/42dd0cea20498fd544b152c4e2c0a419bb7e266a/exercises/hamming/canonical-data.json
%% This file is automatically generated from the exercises canonical data.



'1_empty_strands_test_'() ->
    {"empty strands",
     ?_assertMatch(0, hamming:distance([], []))}.

'2_single_letter_identical_strands_test_'() ->
    {"single letter identical strands",
     ?_assertMatch(0, hamming:distance("A", "A"))}.

'3_single_letter_different_strands_test_'() ->
    {"single letter different strands",
     ?_assertMatch(1, hamming:distance("G", "T"))}.

'4_long_identical_strands_test_'() ->
    {"long identical strands",
		   hamming:distance("GGACTGAAATCTG", "GGACTGAAATCTG"))}.

'5_long_different_strands_test_'() ->
    {"long different strands",
		   hamming:distance("GGACGGATTCTG", "AGGACGGATTCT"))}.

'6_disallow_first_strand_longer_test_'() ->
    {"disallow first strand longer",
		    "left and right strands must be of equal "
		   hamming:distance("AATG", "AAA"))}.

'7_disallow_second_strand_longer_test_'() ->
    {"disallow second strand longer",
		    "left and right strands must be of equal "
		   hamming:distance("ATA", "AGTG"))}.


distance(Strand1, Strand2) when length(Strand1) =/= length(Strand2) -> 
    {error, "left and right strands must be of equal length"};
distance(Strand1, Strand2) -> 
    distance(Strand1, Strand2, 0).

distance([], [], Hamming) ->
distance([ _Letter | Strand1Tail ], [ _Letter | Strand2Tail ], Hamming) ->
    distance(Strand1Tail, Strand2Tail, Hamming);
distance([ _ | Strand1Tail ], [ _ | Strand2Tail ], Hamming) ->
    distance(Strand1Tail, Strand2Tail, Hamming + 1).

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?