🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉 # thekeele's solution

## to Hamming in the Elixir Track

Published at Aug 29 2019 · 0 comments
Instructions
Test suite
Solution

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.

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

The Hamming distance between these two DNA strands is 7.

# 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

Execute the tests with:

``````\$ mix test
``````

### Pending tests

In the test suites, all but the first test have been skipped.

Once you get a test passing, you can unskip the next one by commenting out the relevant `@tag :pending` with a `#` symbol.

For example:

``````# @tag :pending
test "shouting" do
assert Bob.hey("WATCH OUT!") == "Whoa, chill out!"
end
``````

Or, you can enable all the tests by commenting out the `ExUnit.configure` line in the test suite.

``````# ExUnit.configure exclude: :pending, trace: true
``````

If you're stuck on something, it may help to look at some of the available resources out there where answers might be found.

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

``````defmodule HammingTest do
use ExUnit.Case

test "no difference between empty strands" do
assert Hamming.hamming_distance('', '') == {:ok, 0}
end

@tag :pending
test "no difference between identical strands" do
assert Hamming.hamming_distance('GGACTGA', 'GGACTGA') == {:ok, 0}
end

@tag :pending
test "small hamming distance in middle somewhere" do
assert Hamming.hamming_distance('GGACG', 'GGTCG') == {:ok, 1}
end

@tag :pending
test "distance with same nucleotides in different locations" do
assert Hamming.hamming_distance('TAG', 'GAT') == {:ok, 2}
end

@tag :pending
test "larger distance" do
assert Hamming.hamming_distance('ACCAGGG', 'ACTATGG') == {:ok, 2}
end

@tag :pending
test "hamming distance is undefined for strands of different lengths" do
assert {:error, "Lists must be the same length"} =
Hamming.hamming_distance('AAAC', 'TAGGGGAGGCTAGCGGTAGGAC')

assert {:error, "Lists must be the same length"} =
Hamming.hamming_distance('GACTACGGACAGGACACC', 'GACATCGC')
end
end``````

### test_helper.exs

``````ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)``````
``````defmodule Hamming do
@spec hamming_distance([char], [char]) :: {:ok, non_neg_integer} | {:error, String.t()}
def hamming_distance('', ''),
do: {:ok, 0}

def hamming_distance(strand1, strand2) when strand1 === strand2,
do: {:ok, 0}

def hamming_distance(strand1, strand2) when length(strand1) !== length(strand2),
do: {:error, "Lists must be the same length"}

def hamming_distance(strand1, strand2) do
hamming_distance =
strand1
|> Enum.zip(strand2)
|> Enum.reduce(0, fn
{l_strand, r_strand}, distance when l_strand !== r_strand -> 1 + distance
_, distance -> distance
end)

{:ok, hamming_distance}
end
end``````