Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions etl/ETLTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System.Collections.Generic;
using NUnit.Framework;

[TestFixture]
public class ETLTest
{
[Test]
public void TransformsOneValue()
{
var old = new Dictionary<int, IList<string>> { { 1, new List<string> { "A" } } };
var expected = new Dictionary<string, int> { { "a", 1 } };
Assert.That(ETL.Transform(old), Is.EquivalentTo(expected));
}

[Test]
public void TransformsMultipleValues()
{
var old = new Dictionary<int, IList<string>> { { 1, new List<string> { "A", "E", "I", "O", "U" } } };
var expected = new Dictionary<string, int> { { "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<int, IList<string>> { { 1, new List<string> { "A", "E" } }, { 2, new List<string> { "D", "G" } } };
var expected = new Dictionary<string, int> { { "a", 1 }, { "e", 1 }, { "d", 2 }, { "g", 2 } };
Assert.That(ETL.Transform(old), Is.EquivalentTo(expected));
}

[Test]
public void TransformsAFullDataset()
{
var old = new Dictionary<int, IList<string>>
{
{ 1, new List<string> { "A", "E", "I", "O", "U", "L", "N", "R", "S", "T" } },
{ 2, new List<string> { "D", "G" } },
{ 3, new List<string> { "B", "C", "M", "P" } },
{ 4, new List<string> { "F", "H", "V", "W", "Y" } },
{ 5, new List<string> { "K" } },
{ 8, new List<string> { "J", "X" } },
{ 10, new List<string> { "Q", "Z" } },
};
var expected = new Dictionary<string, int>
{
{ "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));
}
}
15 changes: 15 additions & 0 deletions etl/Example.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Collections.Generic;

public class ETL
{
public static IDictionary<string, int> Transform(IDictionary<int, IList<string>> old)
{
var transformed = new Dictionary<string, int>();

foreach (var pair in old)
foreach (var item in pair.Value)
transformed.Add(item.ToLower(), pair.Key);

return transformed;
}
}
102 changes: 102 additions & 0 deletions grade-school/Example.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using System.Collections.Generic;

public class School
{
public IDictionary<int, IList<string>> Roster { get; private set; }

public School()
{
Roster = new Dictionary<int, IList<string>>();
}

public void Add(string student, int grade)
{
if (Roster.ContainsKey(grade))
Roster[grade].Add(student);
else
Roster.Add(grade, new SortedList<string> { student });
}

public IList<string> Grade(int grade)
{
IList<string> students;
if (Roster.TryGetValue(grade, out students))
return students;
return new List<string>(0);
}
}

public class SortedList<T> : IList<T>
{
private readonly List<T> list = new List<T>();

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<T> GetEnumerator()
{
return list.GetEnumerator();
}

System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return list.GetEnumerator();
}
}
75 changes: 75 additions & 0 deletions grade-school/GradeSchoolTest.cs
Original file line number Diff line number Diff line change
@@ -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<string> { "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<string> { "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<string> { "Chelsea" }));
Assert.That(school.Roster[7], Is.EqualTo(new List<string> { "Logan" }));
}

[Test]
public void GradeReturnsTheStudentsInThatGradeInAlphabeticalOrder()
{
school.Add("Franklin", 5);
school.Add("Bradley", 5);
school.Add("Jeff", 1);
var expected = new List<string> { "Bradley", "Franklin" };
Assert.That(school.Grade(5), Is.EqualTo(expected));
}

[Test]
public void GradeReturnsAnEmptyListIfThereAreNoStudentsInThatGrade()
{
Assert.That(school.Grade(1), Is.EqualTo(new List<string>()));
}

[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<string> { "Kyle" }));
Assert.That(school.Roster[4], Is.EqualTo(new List<string> { "Christopher", "Jennifer" }));
Assert.That(school.Roster[6], Is.EqualTo(new List<string> { "Kareem" }));
}
}
9 changes: 9 additions & 0 deletions leap/Example.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
29 changes: 29 additions & 0 deletions leap/LeapTest.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
37 changes: 37 additions & 0 deletions nucleotide-count/Example.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;

public class DNA
{
public IDictionary<char, int> NucleotideCounts { get; private set; }

public DNA(string sequence)
{
InitializeNucleotideCounts(sequence);
}

private void InitializeNucleotideCounts(string sequence)
{
NucleotideCounts = new Dictionary<char, int> { { '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 { }
Loading