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

# n0mn0m's solution

## to Hamming in the F# Track

Published at Nov 05 2020 · 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 the tests

To run the tests, run the command `dotnet test` from within the exercise directory.

## Autoformatting the code

F# source code can be formatted with the Fantomas tool.

After installing it with `dotnet tool restore`, run `dotnet fantomas .` to format code within the current directory.

## Further information

For more detailed information about the F# track, including how to get help if you're having trouble, please visit the exercism.io F# language page.

## Source

The Calculating Point Mutations problem at Rosalind http://rosalind.info/problems/hamm/

### HammingTests.fs

``````// This file was auto-generated based on version 2.3.0 of the canonical data.

module HammingTests

open FsUnit.Xunit
open Xunit

open Hamming

[<Fact>]
let ``Empty strands`` () =
distance "" "" |> should equal (Some 0)

[<Fact(Skip = "Remove this Skip property to run this test")>]
let ``Single letter identical strands`` () =
distance "A" "A" |> should equal (Some 0)

[<Fact(Skip = "Remove this Skip property to run this test")>]
let ``Single letter different strands`` () =
distance "G" "T" |> should equal (Some 1)

[<Fact(Skip = "Remove this Skip property to run this test")>]
let ``Long identical strands`` () =
distance "GGACTGAAATCTG" "GGACTGAAATCTG" |> should equal (Some 0)

[<Fact(Skip = "Remove this Skip property to run this test")>]
let ``Long different strands`` () =
distance "GGACGGATTCTG" "AGGACGGATTCT" |> should equal (Some 9)

[<Fact(Skip = "Remove this Skip property to run this test")>]
let ``Disallow first strand longer`` () =
distance "AATG" "AAA" |> should equal None

[<Fact(Skip = "Remove this Skip property to run this test")>]
let ``Disallow second strand longer`` () =
distance "ATA" "AGTG" |> should equal None

[<Fact(Skip = "Remove this Skip property to run this test")>]
let ``Disallow left empty strand`` () =
distance "" "G" |> should equal None

[<Fact(Skip = "Remove this Skip property to run this test")>]
let ``Disallow right empty strand`` () =
distance "G" "" |> should equal None``````
``````module Hamming

let distance (strand1: string) (strand2: string): int option =
match Seq.length strand1 = Seq.length strand2 with
| true ->
Seq.map2 (fun a b ->
if a <> b then 1 else 0) strand1 strand2
|> Seq.sum
|> Some
| false -> None``````