Avatar of davearonson

davearonson's solution

to Hamming in the Elixir Track

Published at Jul 13 2018 · 1 comment
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
  Code.load_file("hamming.exs", __DIR__)
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 strands 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(strand1, strand2) do
    do_hamming_distance(strand1, strand2, 0)
  end

  defp do_hamming_distance([head1|rest1], [head1|rest2], count_so_far) do
    do_hamming_distance(rest1, rest2, count_so_far)
  end

  defp do_hamming_distance([_head1|rest1], [_head2|rest2], count_so_far) do
    do_hamming_distance(rest1, rest2, count_so_far + 1)
  end

  defp do_hamming_distance([], [], count_so_far), do: { :ok, count_so_far }

  defp do_hamming_distance(_, _, _) do
    { :error, "Lists must be the same length." }
  end

end

Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of davearonson

Also tried without the extra arg on the do_ version, adding 1 to the count in case of mismatch of list heads. However, that required piping it through Tuple.to_list, List.update_at (with a function that checked if that item is an integer), and lastly through List.to_tuple, which seemed rather unwieldy. Also tried a couple other approaches I don't remember that well. Adding the accumulator to increment on the way down rather than back up, yielded much simpler code.

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?