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

# mwchase's solution

## to Hamming in the Erlang Track

Published at Mar 22 2021 · 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.

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

## Questions?

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.

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

### hamming_tests.erl

``````%% 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.

-module(hamming_tests).

-include_lib("erl_exercism/include/exercism.hrl").
-include_lib("eunit/include/eunit.hrl").

'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",
?_assertMatch(0,
hamming:distance("GGACTGAAATCTG", "GGACTGAAATCTG"))}.

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

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

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

-export([distance/2]).

distance(Strand1, Strand2) -> distance(Strand1, Strand2, 0).

distance([], [], Acc) -> Acc;
distance([H|Strand1], [H|Strand2], Acc) -> distance(Strand1, Strand2, Acc);
distance([_H1|Strand1], [_H2|Strand2], Acc) -> distance(Strand1, Strand2, Acc + 1);
distance(_Strand1, _Strang2, _Acc) -> {error, "left and right strands must be of equal length"}.``````