Skip to content

Instantly share code, notes, and snippets.

@MikeMKH
Created December 4, 2014 13:07
Show Gist options
  • Save MikeMKH/1b7f890037d88fa61aaa to your computer and use it in GitHub Desktop.
Save MikeMKH/1b7f890037d88fa61aaa to your computer and use it in GitHub Desktop.
Coin Changer kata with Reduce in C# using MS Tests
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace CoinChanger
{
[TestClass]
public class CoinChangerTests
{
[TestMethod]
[ExpectedException(typeof(ApplicationException))]
public void GivenNoCoins_WhenInvokedWith0_ItMustThrowAnApplicationException()
{
var target = new CoinChanger();
target.ChangeFor(0);
}
[TestMethod]
public void GivenPennies_WhenInvokedWith0_ItMustReturn0Pennies()
{
var target = new CoinChanger { Coins = new[] { 1 } };
var actual = target.ChangeFor(0);
var expected = new[] { 0 };
CollectionAssert.AreEqual(expected, actual);
}
[TestMethod]
public void GivenPennies_WhenInvokedWith1_ItMustReturn1Penny()
{
var target = new CoinChanger { Coins = new[] { 1 } };
var actual = target.ChangeFor(1);
var expected = new[] { 1 };
CollectionAssert.AreEqual(expected, actual);
}
[TestMethod]
public void GivenPennies_WhenInvokedWith3_ItMustReturn3Pennies()
{
var target = new CoinChanger { Coins = new[] { 1 } };
var actual = target.ChangeFor(3);
var expected = new[] { 3 };
CollectionAssert.AreEqual(expected, actual);
}
[TestMethod]
public void GivenNickels_WhenInvokedWith5_ItMustReturn1Nickel()
{
var target = new CoinChanger { Coins = new[] { 5 } };
var actual = target.ChangeFor(5);
var expected = new[] { 1 };
CollectionAssert.AreEqual(expected, actual);
}
[TestMethod]
public void GivenNickelsAndPennies_WhenInvokedWith6_ItMustReturn1NickelAnd1Penny()
{
var target = new CoinChanger { Coins = new[] { 5, 1 } };
var actual = target.ChangeFor(6);
var expected = new[] { 1, 1 };
CollectionAssert.AreEqual(expected, actual);
}
[TestMethod]
public void GivenNickelsAndPennies_WhenInvokedWith9_ItMustReturn1NickelAnd4Pennies()
{
var target = new CoinChanger { Coins = new[] { 5, 1 } };
var actual = target.ChangeFor(9);
var expected = new[] { 1, 4 };
CollectionAssert.AreEqual(expected, actual);
}
[TestMethod]
public void GivenDimesNickelsAndPennies_WhenInvokedWith19_ItMustReturn1Dime1NickelAnd4Pennies()
{
var target = new CoinChanger { Coins = new[] { 10, 5, 1 } };
var actual = target.ChangeFor(19);
var expected = new[] { 1, 1, 4 };
CollectionAssert.AreEqual(expected, actual);
}
[TestMethod]
public void GivenQuartersDimesNickelsAndPennies_WhenInvokedWith99_ItMustReturn3Quarters2Dime0NickelsAnd4Pennies()
{
var target = new CoinChanger { Coins = new[] { 25, 10, 5, 1 } };
var actual = target.ChangeFor(99);
var expected = new[] { 3, 2, 0, 4 };
CollectionAssert.AreEqual(expected, actual);
}
}
public class CoinChanger
{
public int[] Coins { get; set; }
public int[] ChangeFor(int amount)
{
if (Coins == null) throw new ApplicationException("Must have some coins before being called.");
return Coins.Aggregate(
new Working
{
Change = new List<int>(),
Amount = amount
},
(m, coin) =>
{
m.Change.Add(m.Amount / coin);
m.Amount %= coin;
return m;
}).Change.ToArray();
}
class Working
{
public IList<int> Change { get; set; }
public int Amount { get; set; }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment