Created
December 1, 2014 12:59
-
-
Save MikeMKH/bfade8607a6e24d5fd78 to your computer and use it in GitHub Desktop.
Coin Changer kata with Map in C# using 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.Linq; | |
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
namespace CoinChanger | |
{ | |
[TestClass] | |
public class CoinChangerTests | |
{ | |
[TestMethod] | |
public void GivenZeroWithNoCoins_ItMustReturnEmptyArry() | |
{ | |
var changer = new CoinChanger(); | |
var actual = changer.ChangeFor(0); | |
Assert.AreEqual(0, actual.Length); | |
} | |
[TestMethod] | |
public void GivenZeroWithPennies_ItMustReturnEmptyArry() | |
{ | |
var changer = new CoinChanger { Coins = new[] { 1 } }; | |
var expected = new[] { 0 }; | |
var actual = changer.ChangeFor(0); | |
CollectionAssert.AreEqual(expected, actual); | |
} | |
[TestMethod] | |
public void Given1WithPennies_ItMustReturn1Penny() | |
{ | |
var changer = new CoinChanger { Coins = new[] { 1 } }; | |
var expected = new[] { 1 }; | |
var actual = changer.ChangeFor(1); | |
CollectionAssert.AreEqual(expected, actual); | |
} | |
[TestMethod] | |
public void Given2WithPennies_ItMustReturn2Pennies() | |
{ | |
var changer = new CoinChanger { Coins = new[] { 1 } }; | |
var expected = new[] { 2 }; | |
var actual = changer.ChangeFor(2); | |
CollectionAssert.AreEqual(expected, actual); | |
} | |
[TestMethod] | |
public void Given5WithNickels_ItMustReturn1Nickel() | |
{ | |
var changer = new CoinChanger { Coins = new[] { 5 } }; | |
var expected = new[] { 1 }; | |
var actual = changer.ChangeFor(5); | |
CollectionAssert.AreEqual(expected, actual); | |
} | |
[TestMethod] | |
public void Given6WithNickelsAndPennies_ItMustReturn1NickelAnd1Penny() | |
{ | |
var changer = new CoinChanger { Coins = new[] { 5, 1 } }; | |
var expected = new[] { 1, 1 }; | |
var actual = changer.ChangeFor(6); | |
CollectionAssert.AreEqual(expected, actual); | |
} | |
[TestMethod] | |
public void Given7WithNickelsAndPennies_ItMustReturn1NickelAnd2Pennies() | |
{ | |
var changer = new CoinChanger { Coins = new[] { 5, 1 } }; | |
var expected = new[] { 1, 2 }; | |
var actual = changer.ChangeFor(7); | |
CollectionAssert.AreEqual(expected, actual); | |
} | |
[TestMethod] | |
public void Given13WithDimesNickelsAndPennies_ItMustReturn1Dime0NickelsAnd3Pennies() | |
{ | |
var changer = new CoinChanger { Coins = new[] { 10, 5, 1 } }; | |
var expected = new[] { 1, 0, 3 }; | |
var actual = changer.ChangeFor(13); | |
CollectionAssert.AreEqual(expected, actual); | |
} | |
[TestMethod] | |
public void Given99WithQuartersDimesNickelsAndPennies_ItMustReturn3Quarters2Dime0NickelsAnd4Pennies() | |
{ | |
var changer = new CoinChanger { Coins = new[] { 25, 10, 5, 1 } }; | |
var expected = new[] { 3, 2, 0, 4 }; | |
var actual = changer.ChangeFor(99); | |
CollectionAssert.AreEqual(expected, actual); | |
} | |
} | |
public class CoinChanger | |
{ | |
public int[] Coins { get; set; } | |
public int[] ChangeFor(int amount) | |
{ | |
if (Coins == null) return new int[] { }; | |
return Coins.Select(coin => | |
{ | |
var result = amount / coin; | |
amount %= coin; | |
return result; | |
}).ToArray(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment