# artemkorsakov's solution

## to Hamming in the C# Track

Published at Feb 27 2019 · 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.

Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time. Once you get the first test passing, remove the `Skip` property from the next test and work on getting that test passing. Once none of the tests are skipped and they are all passing, you can submit your solution using `exercism submit Hamming.cs`

## Further information

For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language 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.

### HammingTest.cs

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

using System;
using Xunit;

public class HammingTest
{
[Fact]
public void Empty_strands()
{
Assert.Equal(0, Hamming.Distance("", ""));
}

[Fact(Skip = "Remove to run test")]
public void Single_letter_identical_strands()
{
Assert.Equal(0, Hamming.Distance("A", "A"));
}

[Fact(Skip = "Remove to run test")]
public void Single_letter_different_strands()
{
Assert.Equal(1, Hamming.Distance("G", "T"));
}

[Fact(Skip = "Remove to run test")]
public void Long_identical_strands()
{
Assert.Equal(0, Hamming.Distance("GGACTGAAATCTG", "GGACTGAAATCTG"));
}

[Fact(Skip = "Remove to run test")]
public void Long_different_strands()
{
Assert.Equal(9, Hamming.Distance("GGACGGATTCTG", "AGGACGGATTCT"));
}

[Fact(Skip = "Remove to run test")]
public void Disallow_first_strand_longer()
{
Assert.Throws<ArgumentException>(() => Hamming.Distance("AATG", "AAA"));
}

[Fact(Skip = "Remove to run test")]
public void Disallow_second_strand_longer()
{
Assert.Throws<ArgumentException>(() => Hamming.Distance("ATA", "AGTG"));
}

[Fact(Skip = "Remove to run test")]
public void Disallow_left_empty_strand()
{
Assert.Throws<ArgumentException>(() => Hamming.Distance("", "G"));
}

[Fact(Skip = "Remove to run test")]
public void Disallow_right_empty_strand()
{
Assert.Throws<ArgumentException>(() => Hamming.Distance("G", ""));
}
}``````
``````using System;
using System.Linq;

public static class Hamming
{
public static int Distance(string firstStrand, string secondStrand)
{
if (firstStrand.Length != secondStrand.Length)
{
throw new ArgumentException("The Hamming distance is only defined for sequences of equal length");
}

return Enumerable.Range(0, firstStrand.Length).Count(i => firstStrand[i] != secondStrand[i]);
}
}``````