From e26b2dee1595aac0008643b05a235a15296bdef7 Mon Sep 17 00:00:00 2001 From: Bressain Dinkelman Date: Fri, 2 May 2014 22:46:18 -0600 Subject: [PATCH 1/2] word-count test fixture was misnamed. --- word-count/WordCountTest.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 () From 8018e09544a4143d1d70a025846d6f113c79c2ca Mon Sep 17 00:00:00 2001 From: Bressain Dinkelman Date: Sat, 3 May 2014 00:33:51 -0600 Subject: [PATCH 2/2] Add nucleotide-count in C# --- nucleotide-count/Example.cs | 37 +++++++++++++ nucleotide-count/NucleotideCountTest.cs | 73 +++++++++++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 nucleotide-count/Example.cs create mode 100644 nucleotide-count/NucleotideCountTest.cs 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