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

# shmibs's solution

## to Hamming in the Elixir Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

#### Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

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. This means that based on the definition, each language could deal with getting sequences of equal length differently.

## Running tests

Execute the tests with:

``````\$ elixir hamming_test.exs
``````

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

For more detailed information about the Elixir track, please see the help page.

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

``````if !System.get_env("EXERCISM_TEST_EXAMPLES") do
end

ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)

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``````
``````defmodule DNA do
@doc """
Returns number of differences between two ss of DNA, known as the Hamming Distance.

## Examples

iex> DNA.hamming_distance('AAGTCATA', 'TAGCGATC')
{:ok, 4}
"""
@spec hamming_distance([char], [char]) :: non_neg_integer
def hamming_distance(s1, s2) when length(s1) != length(s2) do
{:error, "Lists must be the same length."}
end
def hamming_distance(s1, s2), do: {:ok, distance_count(s1, s2)}

defp distance_count(s1, s2) do
Stream.zip(s1, s2)
|> Stream.filter( &(elem(&1, 0) != elem(&1, 1)) )
|> Enum.count
end
end``````