Instructions

Test suite

Solution

Correctly determine the fewest number of coins to be given to a customer such that the sum of the coins' value would equal the correct amount of change.

- An input of 15 with [1, 5, 10, 25, 100] should return one nickel (5) and one dime (10) or [5, 10]
- An input of 40 with [1, 5, 10, 25, 100] should return one nickel (5) and one dime (10) and one quarter (25) or [5, 10, 25]

- Does your algorithm work for any given set of coins?
- Can you ask for negative change?
- Can you ask for a change value smaller than the smallest coin value?

To run the tests, run the command `dotnet test`

from within the exercise directory.

Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time.
Once you get the first test passing, remove the `Skip`

property from the next test and work on getting that test passing.
Once none of the tests are skipped and they are all passing, you can submit your solution
using `exercism submit Change.cs`

For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language page.

Software Craftsmanship - Coin Change Kata https://web.archive.org/web/20130115115225/http://craftsmanship.sv.cmu.edu:80/exercises/coin-change-kata

```
// This file was auto-generated based on version 1.3.0 of the canonical data.
using System;
using Xunit;
public class ChangeTest
{
[Fact]
public void Single_coin_change()
{
var coins = new[] { 1, 5, 10, 25, 100 };
var target = 25;
var expected = new[] { 25 };
Assert.Equal(expected, Change.FindFewestCoins(coins, target));
}
[Fact(Skip = "Remove to run test")]
public void Multiple_coin_change()
{
var coins = new[] { 1, 5, 10, 25, 100 };
var target = 15;
var expected = new[] { 5, 10 };
Assert.Equal(expected, Change.FindFewestCoins(coins, target));
}
[Fact(Skip = "Remove to run test")]
public void Change_with_lilliputian_coins()
{
var coins = new[] { 1, 4, 15, 20, 50 };
var target = 23;
var expected = new[] { 4, 4, 15 };
Assert.Equal(expected, Change.FindFewestCoins(coins, target));
}
[Fact(Skip = "Remove to run test")]
public void Change_with_lower_elbonia_coins()
{
var coins = new[] { 1, 5, 10, 21, 25 };
var target = 63;
var expected = new[] { 21, 21, 21 };
Assert.Equal(expected, Change.FindFewestCoins(coins, target));
}
[Fact(Skip = "Remove to run test")]
public void Large_target_values()
{
var coins = new[] { 1, 2, 5, 10, 20, 50, 100 };
var target = 999;
var expected = new[] { 2, 2, 5, 20, 20, 50, 100, 100, 100, 100, 100, 100, 100, 100, 100 };
Assert.Equal(expected, Change.FindFewestCoins(coins, target));
}
[Fact(Skip = "Remove to run test")]
public void Possible_change_without_unit_coins_available()
{
var coins = new[] { 2, 5, 10, 20, 50 };
var target = 21;
var expected = new[] { 2, 2, 2, 5, 10 };
Assert.Equal(expected, Change.FindFewestCoins(coins, target));
}
[Fact(Skip = "Remove to run test")]
public void Another_possible_change_without_unit_coins_available()
{
var coins = new[] { 4, 5 };
var target = 27;
var expected = new[] { 4, 4, 4, 5, 5, 5 };
Assert.Equal(expected, Change.FindFewestCoins(coins, target));
}
[Fact(Skip = "Remove to run test")]
public void No_coins_make_0_change()
{
var coins = new[] { 1, 5, 10, 21, 25 };
var target = 0;
Assert.Empty(Change.FindFewestCoins(coins, target));
}
[Fact(Skip = "Remove to run test")]
public void Error_testing_for_change_smaller_than_the_smallest_of_coins()
{
var coins = new[] { 5, 10 };
var target = 3;
Assert.Throws<ArgumentException>(() => Change.FindFewestCoins(coins, target));
}
[Fact(Skip = "Remove to run test")]
public void Error_if_no_combination_can_add_up_to_target()
{
var coins = new[] { 5, 10 };
var target = 94;
Assert.Throws<ArgumentException>(() => Change.FindFewestCoins(coins, target));
}
[Fact(Skip = "Remove to run test")]
public void Cannot_find_negative_change_values()
{
var coins = new[] { 1, 2, 5 };
var target = -5;
Assert.Throws<ArgumentException>(() => Change.FindFewestCoins(coins, target));
}
}
```

```
using System;
using System.Collections.Generic;
public static class Change
{
public static int[] FindFewestCoins(int[] coins, int target)
{
if (target < coins[0] && target != 0) throw new ArgumentException("Wrong input!");
int m = coins.Length;
int[,] ChangeCoins = new int[target + 1, 2];
ChangeCoins[0, 0] = 0;
ChangeCoins[0, 1] = 0;
int[] table = new int[target + 1];
table[0] = 0;
for (int i = 1; i <= target; i++) table[i] = int.MaxValue;
for (int i = 1; i <= target; i++)
{
for (int j = 0; j < m; j++)
{
if (coins[j] <= i)
{
int sub_res = table[i - coins[j]];
if (sub_res != int.MaxValue && sub_res + 1 < table[i])
{
table[i] = sub_res + 1;
ChangeCoins[i, 0] = coins[j];
ChangeCoins[i, 1] = i - coins[j];
}
}
}
}
if (table[target] == int.MaxValue) throw new ArgumentException("No possible result!");
List<int> Result = new List<int>();
while (target > 0)
{
Result.Add(ChangeCoins[target, 0]);
target = ChangeCoins[target, 1];
}
return Result.ToArray();
}
}
```

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

- What compromises have been made?
- Are there new concepts here that you could read more about to improve your understanding?

Level up your programming skills with 3,388 exercises across 50 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments