From 10e99fad163b00d6368b553362f2ea6a76447a6c Mon Sep 17 00:00:00 2001 From: Bressain Dinkelman Date: Fri, 2 May 2014 22:46:18 -0600 Subject: [PATCH 1/7] 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 9f075cc1bb9a2457131ae881112f885fb87d5a1c Mon Sep 17 00:00:00 2001 From: Bressain Dinkelman Date: Sat, 3 May 2014 00:33:51 -0600 Subject: [PATCH 2/7] 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 From bd57e1233ff9029cb7ac171c2fc887c3d0be5031 Mon Sep 17 00:00:00 2001 From: Bressain Dinkelman Date: Sat, 3 May 2014 20:53:21 -0600 Subject: [PATCH 3/7] Add phone-number in C# --- phone-number/Example.cs | 50 ++++++++++++++++++++++++++++++ phone-number/PhoneNumberTest.cs | 54 +++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 phone-number/Example.cs create mode 100644 phone-number/PhoneNumberTest.cs diff --git a/phone-number/Example.cs b/phone-number/Example.cs new file mode 100644 index 0000000000..908db1bd78 --- /dev/null +++ b/phone-number/Example.cs @@ -0,0 +1,50 @@ +using System.Text.RegularExpressions; + +public class PhoneNumber +{ + private static readonly Regex DigitsOnly = new Regex(@"[^\d]"); + + public string Number { get; private set; } + public string AreaCode { get; private set; } + + public PhoneNumber(string phoneNumber) + { + Number = GetValidatedPhoneNumber(phoneNumber); + AreaCode = Number.Substring(0, 3); + } + + private static string GetValidatedPhoneNumber(string phoneNumber) + { + var stripped = StripOutNonNumerics(phoneNumber); + + if (IsInvalidPhoneNumber(stripped)) + return GetInvalidPhoneNumberReplacement(stripped); + + return stripped; + } + + private static string StripOutNonNumerics(string value) + { + return DigitsOnly.Replace(value, ""); + } + + private static bool IsInvalidPhoneNumber(string phoneNumber) + { + return phoneNumber.Length != 10; + } + + private static string GetInvalidPhoneNumberReplacement(string phoneNumber) + { + return IsPhoneNumberWithUSAreaCode(phoneNumber) ? phoneNumber.Substring(1) : "0000000000"; + } + + private static bool IsPhoneNumberWithUSAreaCode(string value) + { + return value.Length == 11 && value.StartsWith("1"); + } + + public override string ToString() + { + return string.Format("({0}) {1}-{2}", AreaCode, Number.Substring(3, 3), Number.Substring(6)); + } +} \ No newline at end of file diff --git a/phone-number/PhoneNumberTest.cs b/phone-number/PhoneNumberTest.cs new file mode 100644 index 0000000000..1556b58541 --- /dev/null +++ b/phone-number/PhoneNumberTest.cs @@ -0,0 +1,54 @@ +using NUnit.Framework; + +[TestFixture] +public class PhoneNumberTest +{ + [Test] + public void CleansParensSpacesAndDashes() + { + var phone = new PhoneNumber("(123) 456-7890"); + Assert.That(phone.Number, Is.EqualTo("1234567890")); + } + + [Test] + public void CleansNumbersWithDots() + { + var phone = new PhoneNumber("123.456.7890"); + Assert.That(phone.Number, Is.EqualTo("1234567890")); + } + + [Test] + public void AllowsUsCountryCode() + { + var phone = new PhoneNumber("11234567890"); + Assert.That(phone.Number, Is.EqualTo("1234567890")); + } + + [Test] + public void InvalidWhen11Digits() + { + var phone = new PhoneNumber("21234567890"); + Assert.That(phone.Number, Is.EqualTo("0000000000")); + } + + [Test] + public void InvalidWhen9Digits() + { + var phone = new PhoneNumber("123456789"); + Assert.That(phone.Number, Is.EqualTo("0000000000")); + } + + [Test] + public void HasAnAreaCode() + { + var phone = new PhoneNumber("1234567890"); + Assert.That(phone.AreaCode, Is.EqualTo("123")); + } + + [Test] + public void FormatsANumber() + { + var phone = new PhoneNumber("1234567890"); + Assert.That(phone.ToString(), Is.EqualTo("(123) 456-7890")); + } +} \ No newline at end of file From 9333b976e841033ebec80e4a8f6e52b98a429abd Mon Sep 17 00:00:00 2001 From: Bressain Dinkelman Date: Sat, 3 May 2014 22:05:33 -0600 Subject: [PATCH 4/7] Add grade-school in C# --- grade-school/Example.cs | 102 ++++++++++++++++++++++++++++++++ grade-school/GradeSchoolTest.cs | 75 +++++++++++++++++++++++ 2 files changed, 177 insertions(+) create mode 100644 grade-school/Example.cs create mode 100644 grade-school/GradeSchoolTest.cs diff --git a/grade-school/Example.cs b/grade-school/Example.cs new file mode 100644 index 0000000000..b556d33043 --- /dev/null +++ b/grade-school/Example.cs @@ -0,0 +1,102 @@ +using System.Collections.Generic; + +public class School +{ + public IDictionary> Roster { get; private set; } + + public School() + { + Roster = new Dictionary>(); + } + + public void Add(string student, int grade) + { + if (Roster.ContainsKey(grade)) + Roster[grade].Add(student); + else + Roster.Add(grade, new SortedList { student }); + } + + public IList Grade(int grade) + { + IList students; + if (Roster.TryGetValue(grade, out students)) + return students; + return new List(0); + } +} + +public class SortedList : IList +{ + private readonly List list = new List(); + + public int IndexOf(T item) + { + return list.IndexOf(item); + } + + public void Insert(int index, T item) + { + throw new System.NotSupportedException("Insert would ruin sort"); + } + + public void RemoveAt(int index) + { + list.RemoveAt(index); + } + + public T this[int index] + { + get { return list[index]; } + set + { + list.RemoveAt(index); + Add(value); + } + } + + public void Add(T item) + { + list.Insert(~list.BinarySearch(item), item); + } + + public void Clear() + { + list.Clear(); + } + + public bool Contains(T item) + { + return list.Contains(item); + } + + public void CopyTo(T[] array, int arrayIndex) + { + list.CopyTo(array, arrayIndex); + } + + public int Count + { + get { return list.Count; } + } + + public bool IsReadOnly + { + get { return false; } + } + + public bool Remove(T item) + { + return list.Remove(item); + } + + public IEnumerator GetEnumerator() + { + return list.GetEnumerator(); + } + + System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() + { + return list.GetEnumerator(); + } +} \ No newline at end of file diff --git a/grade-school/GradeSchoolTest.cs b/grade-school/GradeSchoolTest.cs new file mode 100644 index 0000000000..cf104b4890 --- /dev/null +++ b/grade-school/GradeSchoolTest.cs @@ -0,0 +1,75 @@ +using System.Collections.Generic; +using NUnit.Framework; + +[TestFixture] +public class GradeSchoolTest +{ + private School school; + + [SetUp] + public void Setup() + { + school = new School(); + } + + [Test] + public void NewSchoolHasAnEmptyRoster() + { + Assert.That(school.Roster, Has.Count.EqualTo(0)); + } + + [Test] + public void AddingAStudentAddsThemToTheRosterForTheGivenGrade() + { + school.Add("Aimee", 2); + var expected = new List { "Aimee" }; + Assert.That(school.Roster[2], Is.EqualTo(expected)); + } + + [Test] + public void AddingMoreStudentsToTheSameGradeAddsThemToTheRoster() + { + school.Add("Blair", 2); + school.Add("James", 2); + school.Add("Paul", 2); + var expected = new List { "Blair", "James", "Paul" }; + Assert.That(school.Roster[2], Is.EqualTo(expected)); + } + + [Test] + public void AddingStudentsToDifferentGradesAddsThemToTheRoster() + { + school.Add("Chelsea", 3); + school.Add("Logan", 7); + Assert.That(school.Roster[3], Is.EqualTo(new List { "Chelsea" })); + Assert.That(school.Roster[7], Is.EqualTo(new List { "Logan" })); + } + + [Test] + public void GradeReturnsTheStudentsInThatGradeInAlphabeticalOrder() + { + school.Add("Franklin", 5); + school.Add("Bradley", 5); + school.Add("Jeff", 1); + var expected = new List { "Bradley", "Franklin" }; + Assert.That(school.Grade(5), Is.EqualTo(expected)); + } + + [Test] + public void GradeReturnsAnEmptyListIfThereAreNoStudentsInThatGrade() + { + Assert.That(school.Grade(1), Is.EqualTo(new List())); + } + + [Test] + public void StudentNamesInEachGradeInRosterAreSorted() + { + school.Add("Jennifer", 4); + school.Add("Kareem", 6); + school.Add("Christopher", 4); + school.Add("Kyle", 3); + Assert.That(school.Roster[3], Is.EqualTo(new List { "Kyle" })); + Assert.That(school.Roster[4], Is.EqualTo(new List { "Christopher", "Jennifer" })); + Assert.That(school.Roster[6], Is.EqualTo(new List { "Kareem" })); + } +} \ No newline at end of file From a58faf99f8ef0b42ec246b64f425072bfe6b841d Mon Sep 17 00:00:00 2001 From: Bressain Dinkelman Date: Sat, 3 May 2014 22:42:15 -0600 Subject: [PATCH 5/7] Add robot-name in C# --- robot-name/Example.cs | 29 +++++++++++++++++++++++++++ robot-name/RobotNameTest.cs | 40 +++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 robot-name/Example.cs create mode 100644 robot-name/RobotNameTest.cs diff --git a/robot-name/Example.cs b/robot-name/Example.cs new file mode 100644 index 0000000000..98d45f30d9 --- /dev/null +++ b/robot-name/Example.cs @@ -0,0 +1,29 @@ +using System; + +public class Robot +{ + private static readonly Random Random = new Random(); + private const string ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + + public string Name { get; private set; } + + public Robot() + { + Reset(); + } + + private static string GenerateName() + { + return GetRandomCharacter() + GetRandomCharacter() + Random.Next(999); + } + + private static string GetRandomCharacter() + { + return ALPHABET[Random.Next(25)].ToString(); + } + + public void Reset() + { + Name = GenerateName(); + } +} \ No newline at end of file diff --git a/robot-name/RobotNameTest.cs b/robot-name/RobotNameTest.cs new file mode 100644 index 0000000000..277d926a29 --- /dev/null +++ b/robot-name/RobotNameTest.cs @@ -0,0 +1,40 @@ +using NUnit.Framework; + +[TestFixture] +public class RobotNameTest +{ + private Robot robot; + + [SetUp] + public void Setup() + { + robot = new Robot(); + } + + [Test] + public void RobotHasAName() + { + StringAssert.IsMatch(@"\w{2}\d{3}", robot.Name); + } + + [Test] + public void NameIsTheSameEachTime() + { + Assert.That(robot.Name, Is.EqualTo(robot.Name)); + } + + [Test] + public void DifferentRobotsHaveDifferentNames() + { + var robot2 = new Robot(); + Assert.That(robot.Name, Is.Not.EqualTo(robot2.Name)); + } + + [Test] + public void CanResetTheName() + { + var originalName = robot.Name; + robot.Reset(); + Assert.That(robot.Name, Is.Not.EqualTo(originalName)); + } +} \ No newline at end of file From d8fcff95a4bbedbae92cb7ec866fa2cccb33df7d Mon Sep 17 00:00:00 2001 From: Bressain Dinkelman Date: Sat, 3 May 2014 22:54:01 -0600 Subject: [PATCH 6/7] Add leap in C# --- leap/Example.cs | 9 +++++++++ leap/LeapTest.cs | 29 +++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 leap/Example.cs create mode 100644 leap/LeapTest.cs diff --git a/leap/Example.cs b/leap/Example.cs new file mode 100644 index 0000000000..e27fcae576 --- /dev/null +++ b/leap/Example.cs @@ -0,0 +1,9 @@ +public class Year +{ + public static bool IsLeap(int year) + { + if (year % 100 == 0) + return year % 400 == 0; + return year % 4 == 0; + } +} \ No newline at end of file diff --git a/leap/LeapTest.cs b/leap/LeapTest.cs new file mode 100644 index 0000000000..75b42dc063 --- /dev/null +++ b/leap/LeapTest.cs @@ -0,0 +1,29 @@ +using NUnit.Framework; + +[TestFixture] +public class LeapTest +{ + [Test] + public void ValidLeapYear() + { + Assert.That(Year.IsLeap(1996), Is.True); + } + + [Test] + public void InvalidLeapYear() + { + Assert.That(Year.IsLeap(1997), Is.False); + } + + [Test] + public void TurnOfThe20thCenturyIsNotALeapYear() + { + Assert.That(Year.IsLeap(1900), Is.False); + } + + [Test] + public void TurnOfThe25thCenturyIsALeapYear() + { + Assert.That(Year.IsLeap(2400), Is.True); + } +} \ No newline at end of file From c2b2f872be463e22daa8497481ee2736ddaa1a18 Mon Sep 17 00:00:00 2001 From: Bressain Dinkelman Date: Sat, 3 May 2014 23:23:48 -0600 Subject: [PATCH 7/7] Add etl in C# --- etl/ETLTest.cs | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++ etl/Example.cs | 15 +++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 etl/ETLTest.cs create mode 100644 etl/Example.cs diff --git a/etl/ETLTest.cs b/etl/ETLTest.cs new file mode 100644 index 0000000000..bc149060fa --- /dev/null +++ b/etl/ETLTest.cs @@ -0,0 +1,52 @@ +using System.Collections.Generic; +using NUnit.Framework; + +[TestFixture] +public class ETLTest +{ + [Test] + public void TransformsOneValue() + { + var old = new Dictionary> { { 1, new List { "A" } } }; + var expected = new Dictionary { { "a", 1 } }; + Assert.That(ETL.Transform(old), Is.EquivalentTo(expected)); + } + + [Test] + public void TransformsMultipleValues() + { + var old = new Dictionary> { { 1, new List { "A", "E", "I", "O", "U" } } }; + var expected = new Dictionary { { "a", 1 }, { "e", 1 }, { "i", 1 }, { "o", 1 }, { "u", 1 } }; + Assert.That(ETL.Transform(old), Is.EquivalentTo(expected)); + } + + [Test] + public void TransformsMultipleKeys() + { + var old = new Dictionary> { { 1, new List { "A", "E" } }, { 2, new List { "D", "G" } } }; + var expected = new Dictionary { { "a", 1 }, { "e", 1 }, { "d", 2 }, { "g", 2 } }; + Assert.That(ETL.Transform(old), Is.EquivalentTo(expected)); + } + + [Test] + public void TransformsAFullDataset() + { + var old = new Dictionary> + { + { 1, new List { "A", "E", "I", "O", "U", "L", "N", "R", "S", "T" } }, + { 2, new List { "D", "G" } }, + { 3, new List { "B", "C", "M", "P" } }, + { 4, new List { "F", "H", "V", "W", "Y" } }, + { 5, new List { "K" } }, + { 8, new List { "J", "X" } }, + { 10, new List { "Q", "Z" } }, + }; + var expected = new Dictionary + { + { "a", 1 }, { "b", 3 }, { "c", 3 }, { "d", 2 }, { "e", 1 }, { "f", 4 }, { "g", 2 }, { "h", 4 }, { "i", 1 }, + { "j", 8 }, { "k", 5 }, { "l", 1 }, { "m", 3 }, { "n", 1 }, { "o", 1 }, { "p", 3 }, { "q", 10 }, { "r", 1 }, + { "s", 1 }, { "t", 1 }, { "u", 1 }, { "v", 4 }, { "w", 4 }, { "x", 8 }, { "y", 4 }, { "z", 10 } + }; + Assert.That(ETL.Transform(old), Is.EquivalentTo(expected)); + } +} \ No newline at end of file diff --git a/etl/Example.cs b/etl/Example.cs new file mode 100644 index 0000000000..891725eaa7 --- /dev/null +++ b/etl/Example.cs @@ -0,0 +1,15 @@ +using System.Collections.Generic; + +public class ETL +{ + public static IDictionary Transform(IDictionary> old) + { + var transformed = new Dictionary(); + + foreach (var pair in old) + foreach (var item in pair.Value) + transformed.Add(item.ToLower(), pair.Key); + + return transformed; + } +} \ No newline at end of file