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

# n0mn0m's solution

## to Nucleotide Count in the C# Track

Published at Oct 27 2020 · 0 comments
Instructions
Test suite
Solution

Given a single stranded DNA string, compute how many times each nucleotide occurs in the string.

The genetic language of every living thing on the planet is DNA. DNA is a large molecule that is built from an extremely long sequence of individual elements called nucleotides. 4 types exist in DNA and these differ only slightly and can be represented as the following symbols: 'A' for adenine, 'C' for cytosine, 'G' for guanine, and 'T' thymine.

Here is an analogy:

• twigs are to birds nests as
• nucleotides are to DNA as
• legos are to lego houses as
• words are to sentences as...

## 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 NucleotideCount.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 DNA Nucleotides_problem at Rosalind http://rosalind.info/problems/dna/

### NucleotideCountTests.cs

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

using System;
using System.Collections.Generic;
using Xunit;

public class NucleotideCountTests
{
[Fact]
public void Empty_strand()
{
var expected = new Dictionary<char, int>
{
['A'] = 0,
['C'] = 0,
['G'] = 0,
['T'] = 0
};
Assert.Equal(expected, NucleotideCount.Count(""));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Can_count_one_nucleotide_in_single_character_input()
{
var expected = new Dictionary<char, int>
{
['A'] = 0,
['C'] = 0,
['G'] = 1,
['T'] = 0
};
Assert.Equal(expected, NucleotideCount.Count("G"));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Strand_with_repeated_nucleotide()
{
var expected = new Dictionary<char, int>
{
['A'] = 0,
['C'] = 0,
['G'] = 7,
['T'] = 0
};
Assert.Equal(expected, NucleotideCount.Count("GGGGGGG"));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Strand_with_multiple_nucleotides()
{
var expected = new Dictionary<char, int>
{
['A'] = 20,
['C'] = 12,
['G'] = 17,
['T'] = 21
};
Assert.Equal(expected, NucleotideCount.Count("AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC"));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Strand_with_invalid_nucleotides()
{
Assert.Throws<ArgumentException>(() => NucleotideCount.Count("AGXXACT"));
}
}``````
``````using System;
using System.Collections.Generic;

public static class NucleotideCount
{
public static IDictionary<char, int> Count(string sequence)
{
var nucleotideFrequency = new Dictionary<char, int> { { 'A', 0 }, { 'C', 0 }, { 'G', 0 }, { 'T', 0 } };

foreach (char c in sequence)
{
if (!nucleotideFrequency.ContainsKey(c)) throw new InvalidNucleotideException("Nucleotide must be A, C, G or T.");
nucleotideFrequency[c] = nucleotideFrequency[c] + 1;
}
return nucleotideFrequency;
}
}

public class InvalidNucleotideException : ArgumentException
{
public InvalidNucleotideException(): base() {}
public InvalidNucleotideException(string message) : base(message) { }
}``````