Created
December 2, 2014 13:10
-
-
Save MikeMKH/05db88a0d1ceb0334e92 to your computer and use it in GitHub Desktop.
Coin Changer kata with Reduce using C# with MS Test
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; | |
using System.Collections.Generic; | |
using System.Linq; | |
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
namespace CoinChangerKata | |
{ | |
[TestClass] | |
public class CoinChangerTests | |
{ | |
[TestMethod] | |
public void GivenNoCoins_WhenChangeForIsCalledWith0_ItMustReturnAnEmptyArray() | |
{ | |
var target = new CoinChanger { Coins = null }; | |
Assert.IsFalse(target.ChangeFor(0).ToList().Any()); | |
} | |
[TestMethod] | |
public void GivenPennies_WhenChangeForIsCalledWith0_ItMustReturn0Pennies() | |
{ | |
var target = new CoinChanger { Coins = new[] { 1 } }; | |
var expected = new[] { 0 }; | |
var actual = target.ChangeFor(0); | |
CollectionAssert.AreEqual(expected, actual); | |
} | |
[TestMethod] | |
public void GivenPennies_WhenChangeForIsCalledWith1_ItMustReturn1Penny() | |
{ | |
var target = new CoinChanger { Coins = new[] { 1 } }; | |
var expected = new[] { 1 }; | |
var actual = target.ChangeFor(1); | |
CollectionAssert.AreEqual(expected, actual); | |
} | |
[TestMethod] | |
public void GivenNickels_WhenChangeIsCalledWith5_ItMustReturn1Nickel() | |
{ | |
var target = new CoinChanger { Coins = new[] { 5 } }; | |
var expected = new[] { 1 }; | |
var actual = target.ChangeFor(5); | |
CollectionAssert.AreEqual(expected, actual); | |
} | |
[TestMethod] | |
public void GivenNickelsAndPennies_WhenChangeIsCalledWith5_ItMustReturn1Nickel0Pennies() | |
{ | |
var target = new CoinChanger { Coins = new[] { 5, 1 } }; | |
var expected = new[] { 1, 0 }; | |
var actual = target.ChangeFor(5); | |
CollectionAssert.AreEqual(expected, actual); | |
} | |
[TestMethod] | |
public void GivenNickelsAndPennies_WhenChangeIsCalledWith7_ItMustReturn1Nickel2Pennies() | |
{ | |
var target = new CoinChanger { Coins = new[] { 5, 1 } }; | |
var expected = new[] { 1, 2 }; | |
var actual = target.ChangeFor(7); | |
CollectionAssert.AreEqual(expected, actual); | |
} | |
[TestMethod] | |
public void GivenQuartersDimesNickelsAndPennies_WhenChangeIsCalledWith99_ItMustReturn3Quarters2Dimes0Nickels4Pennies() | |
{ | |
var target = new CoinChanger { Coins = new[] { 25, 10, 5, 1 } }; | |
var expected = new[] { 3, 2, 0, 4 }; | |
var actual = target.ChangeFor(99); | |
CollectionAssert.AreEqual(expected, actual); | |
} | |
} | |
public class CoinChanger | |
{ | |
public int[] Coins { get; set; } | |
public int[] ChangeFor(int change) | |
{ | |
if (Coins == null) return new int[] { }; | |
return Coins.Aggregate( | |
new Tuple<int, IList<int>>(change, new List<int>()), | |
(m, coin) => | |
{ | |
var result = m.Item2; | |
result.Add(m.Item1 / coin); | |
return new Tuple<int, IList<int>>(m.Item1 % coin, result); | |
}).Item2.ToArray(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment