Created
July 21, 2014 12:08
-
-
Save MikeMKH/8fef264127a34b231f21 to your computer and use it in GitHub Desktop.
Coin Changer kata in C# using NUnit
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 NUnit.Framework; | |
namespace CoinChanger | |
{ | |
[TestFixture] | |
public class ChangerTests | |
{ | |
[TestCase(new int[]{}, 0, Result = new int[]{}, TestName="No coins for 0 cents")] | |
[TestCase(new[]{1}, 0, Result = new[]{0}, TestName="Pennies for 0 cents")] | |
[TestCase(new[]{1}, 1, Result = new[]{1}, TestName="Pennies for 1 cents")] | |
[TestCase(new[]{1}, 4, Result = new[]{4}, TestName="Pennies for 4 cents")] | |
[TestCase(new[]{5, 1}, 5, Result = new[]{1, 0}, TestName="Nickles and Pennies for 5 cents")] | |
[TestCase(new[]{5, 1}, 8, Result = new[]{1, 3}, TestName="Nickles and Pennies for 8 cents")] | |
[TestCase(new[]{25, 10, 5, 1}, 99, Result = new[]{3, 2, 0, 4}, TestName="Quarters, Dimes, Nickles and Pennies for 99 cents")] | |
public IList<int> Given_Nothing_For_0_It_Must_Give_Empty_Back(IList<int> coins, int amount) | |
{ | |
var changer = new Changer | |
{ | |
Coins = coins | |
}; | |
return changer.For(amount); | |
} | |
} | |
public class Changer | |
{ | |
public IList<int> Coins { get; set; } | |
public IList<int> For(int amount) | |
{ | |
if (!Coins.Any()) return new List<int>(); | |
return Coins.Aggregate(new Working {Amount = amount, Result = new List<int>()}, | |
(m, coin) => | |
{ | |
m.Result.Add(m.Amount/coin); | |
m.Amount %= coin; | |
return m; | |
}).Result; | |
} | |
class Working | |
{ | |
public int Amount { get; set; } | |
public IList<int> Result { get; set; } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment