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

thekeele's solution

to Hamming in the Elixir Track

Published at Aug 29 2019 · 0 comments
Test suite

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.

^ ^ ^  ^ ^    ^^

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!"

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.


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.


defmodule HammingTest do
  use ExUnit.Case

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

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

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

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

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

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


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 =
      |> Enum.zip(strand2)
      |> Enum.reduce(0, fn
        {l_strand, r_strand}, distance when l_strand !== r_strand -> 1 + distance
        _, distance -> distance

    {:ok, hamming_distance}

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?