diff --git a/nucleotide-count/Example.cs b/nucleotide-count/Example.cs new file mode 100644 index 0000000000..48f9c729f8 --- /dev/null +++ b/nucleotide-count/Example.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; + +public class DNA +{ + public IDictionary NucleotideCounts { get; private set; } + + public DNA(string sequence) + { + InitializeNucleotideCounts(sequence); + } + + private void InitializeNucleotideCounts(string sequence) + { + NucleotideCounts = new Dictionary { { 'A', 0 }, { 'T', 0 }, { 'C', 0 }, { 'G', 0 } }; + foreach (var s in sequence) + NucleotideCounts[s] += 1; + } + + public int Count(char nucleotide) + { + if (IsUracil(nucleotide)) + return 0; + + int count; + if (!NucleotideCounts.TryGetValue(nucleotide, out count)) + throw new InvalidNucleotideException(); + return count; + } + + private static bool IsUracil(char nucleotide) + { + return nucleotide == 'U'; + } +} + +public class InvalidNucleotideException : Exception { } diff --git a/nucleotide-count/NucleotideCountTest.cs b/nucleotide-count/NucleotideCountTest.cs new file mode 100644 index 0000000000..77fd162dcf --- /dev/null +++ b/nucleotide-count/NucleotideCountTest.cs @@ -0,0 +1,73 @@ +using System.Collections.Generic; +using NUnit.Framework; + +[TestFixture] +public class NucleoTideCountTest +{ + [Test] + public void HasNoNucleotides() + { + var dna = new DNA(""); + var expected = new Dictionary { { 'A', 0 }, { 'T', 0 }, { 'C', 0 }, { 'G', 0 } }; + Assert.That(dna.NucleotideCounts, Is.EquivalentTo(expected)); + } + + [Test] + public void HasNoAdenosine() + { + var dna = new DNA(""); + Assert.That(dna.Count('A'), Is.EqualTo(0)); + } + + [Test] + public void RepetitiveCytidineGetsCounts() + { + var dna = new DNA("CCCCC"); + Assert.That(dna.Count('C'), Is.EqualTo(5)); + } + + [Test] + public void RepetitiveSequenceHasOnlyGuanosine() + { + var dna = new DNA("GGGGGGGG"); + var expected = new Dictionary { { 'A', 0 }, { 'T', 0 }, { 'C', 0 }, { 'G', 8 } }; + Assert.That(dna.NucleotideCounts, Is.EquivalentTo(expected)); + } + + [Test] + public void CountsOnlyThymidine() + { + var dna = new DNA("GGGGTAACCCGG"); + Assert.That(dna.Count('T'), Is.EqualTo(1)); + } + + [Test] + public void CountsANucleotideOnlyOnce() + { + var dna = new DNA("GGTTGG"); + dna.Count('T'); + Assert.That(dna.Count('T'), Is.EqualTo(2)); + } + + [Test] + public void HasNoUracil() + { + var dna = new DNA("GGTTGG"); + Assert.That(dna.Count('U'), Is.EqualTo(0)); + } + + [Test] + public void ValidatesNucleotides() + { + var dna = new DNA("GGTTGG"); + Assert.Throws(() => dna.Count('X')); + } + + [Test] + public void CountsAllNucleotides() + { + var dna = new DNA("AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC"); + var expected = new Dictionary { { 'A', 20 }, { 'T', 21 }, { 'C', 12 }, { 'G', 17 } }; + Assert.That(dna.NucleotideCounts, Is.EquivalentTo(expected)); + } +} \ No newline at end of file diff --git a/word-count/WordCountTest.cs b/word-count/WordCountTest.cs index 2beaa725f3..65a1bbdfef 100644 --- a/word-count/WordCountTest.cs +++ b/word-count/WordCountTest.cs @@ -2,7 +2,7 @@ using NUnit.Framework; [TestFixture] -public class BobTest +public class WordCountTest { [Test] public void CountOneWord ()