Created
December 5, 2014 13:03
-
-
Save MikeMKH/69e5c20ba6f4db3e861a to your computer and use it in GitHub Desktop.
Roman Numeral kata with Reduce in C# using MS Tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Linq; | |
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
namespace RomanNumerals | |
{ | |
[TestClass] | |
public class RomanNumeralTests | |
{ | |
[TestMethod] | |
public void Given0_WhenRomanizeIsInvoke_ItMustReturnAnEmptyString() | |
{ | |
var target = new Numerals(); | |
var actual = target.Romanize(0); | |
var expected = string.Empty; | |
Assert.AreEqual(expected, actual); | |
} | |
[TestMethod] | |
public void Given1_WhenRomanizeIsInvoke_ItMustReturnI() | |
{ | |
var target = new Numerals(); | |
var actual = target.Romanize(1); | |
const string expected = "I"; | |
Assert.AreEqual(expected, actual); | |
} | |
[TestMethod] | |
public void Given2_WhenRomanizeIsInvoke_ItMustReturnII() | |
{ | |
var target = new Numerals(); | |
var actual = target.Romanize(2); | |
const string expected = "II"; | |
Assert.AreEqual(expected, actual); | |
} | |
[TestMethod] | |
public void Given3_WhenRomanizeIsInvoke_ItMustReturnIII() | |
{ | |
var target = new Numerals(); | |
var actual = target.Romanize(3); | |
const string expected = "III"; | |
Assert.AreEqual(expected, actual); | |
} | |
[TestMethod] | |
public void Given4_WhenRomanizeIsInvoke_ItMustReturnIV() | |
{ | |
var target = new Numerals(); | |
var actual = target.Romanize(4); | |
const string expected = "IV"; | |
Assert.AreEqual(expected, actual); | |
} | |
[TestMethod] | |
public void Given5_WhenRomanizeIsInvoke_ItMustReturnV() | |
{ | |
var target = new Numerals(); | |
var actual = target.Romanize(5); | |
const string expected = "V"; | |
Assert.AreEqual(expected, actual); | |
} | |
[TestMethod] | |
public void Given6_WhenRomanizeIsInvoke_ItMustReturnVI() | |
{ | |
var target = new Numerals(); | |
var actual = target.Romanize(6); | |
const string expected = "VI"; | |
Assert.AreEqual(expected, actual); | |
} | |
[TestMethod] | |
public void Given7_WhenRomanizeIsInvoke_ItMustReturnVII() | |
{ | |
var target = new Numerals(); | |
var actual = target.Romanize(7); | |
const string expected = "VII"; | |
Assert.AreEqual(expected, actual); | |
} | |
[TestMethod] | |
public void Given9_WhenRomanizeIsInvoke_ItMustReturnIX() | |
{ | |
var target = new Numerals(); | |
var actual = target.Romanize(9); | |
const string expected = "IX"; | |
Assert.AreEqual(expected, actual); | |
} | |
[TestMethod] | |
public void Given10_WhenRomanizeIsInvoke_ItMustReturnX() | |
{ | |
var target = new Numerals(); | |
var actual = target.Romanize(10); | |
const string expected = "X"; | |
Assert.AreEqual(expected, actual); | |
} | |
[TestMethod] | |
public void Given29_WhenRomanizeIsInvoke_ItMustReturnXXIX() | |
{ | |
var target = new Numerals(); | |
var actual = target.Romanize(29); | |
const string expected = "XXIX"; | |
Assert.AreEqual(expected, actual); | |
} | |
[TestMethod] | |
public void Given30_WhenRomanizeIsInvoke_ItMustReturnXXX() | |
{ | |
var target = new Numerals(); | |
var actual = target.Romanize(30); | |
const string expected = "XXX"; | |
Assert.AreEqual(expected, actual); | |
} | |
[TestMethod] | |
public void Given89_WhenRomanizeIsInvoke_ItMustReturnLXXXIX() | |
{ | |
var target = new Numerals(); | |
var actual = target.Romanize(89); | |
const string expected = "LXXXIX"; | |
Assert.AreEqual(expected, actual); | |
} | |
[TestMethod] | |
public void Given49_WhenRomanizeIsInvoke_ItMustReturnXLIX() | |
{ | |
var target = new Numerals(); | |
var actual = target.Romanize(49); | |
const string expected = "XLIX"; | |
Assert.AreEqual(expected, actual); | |
} | |
} | |
public class Numerals | |
{ | |
private readonly Dictionary<int, string> _lookup; | |
public Numerals() | |
{ | |
_lookup = new Dictionary<int, string> | |
{ | |
{50, "L"}, {40, "XL"}, | |
{10, "X"}, { 9, "IX"}, | |
{ 5, "V"}, { 4, "IV"}, | |
{ 1, "I"} | |
}; | |
} | |
public string Romanize(int value) | |
{ | |
return _lookup.Aggregate(string.Empty, (m, l) => | |
{ | |
while (value >= l.Key) | |
{ | |
m += l.Value; | |
value -= l.Key; | |
} | |
return m; | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment