 # Some's solution

## to Change in the C# Track

Published at Jul 11 2019 · 0 comments
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.

## For example

• 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]

## Edge cases

• 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?

## Running the tests

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`

## Further information

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.

## Source

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

### ChangeTest.cs

``````// 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")]
{
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;
using System.Linq;

public static class Change
{
public static int[] FindFewestCoins(int[] coins, int target)
{
if (target == 0)
{
return new List<int>().ToArray();
}

if (getBiggestCoinSmallerThanTarget(target , coins ) == -1)
{
throw new ArgumentException();
}

List<int[]> myResults = new List<int[]>();

for (int i = 0; i<coins.Length; i++)
{
int[] usedCoins = coins.OrderByDescending(x => x)
.Skip(i)
.Take(coins.Length - i)
.ToArray();
int[] result = Go(usedCoins, target, new List<int>());

if (result.Length != 0)  { myResults.Add(result);  }
}
if (myResults.Count == 0){
throw new ArgumentException();
}
return myResults.OrderBy(x=>x.Length)
.ToArray()
.OrderBy(x=>x)
.ToArray();
}

public static int[] Go(int[] coins, int target, List<int> changeCoins)
{

int biggestCoin = getBiggestCoinSmallerThanTarget(target, coins);
if (biggestCoin > 0)
{
target -= biggestCoin;
}

else
{
return new List<int>().ToArray();
}

if (target == 0)
{
return changeCoins.ToArray();
}

for (int i = 0; i < coins.Length; i++)
{
int[] newCoins = coins.Skip(i).Take(coins.Length - i).ToArray();
int[] result = Go(newCoins, target, changeCoins);
if (result.Length != 0)
{
return result.ToArray();
}
}

changeCoins.RemoveAt(changeCoins.Count-1);
return new List<int>().ToArray();
}

public static int getBiggestCoinSmallerThanTarget(int target, int[] coins)
{
for (int i = 0; i < coins.Length; i++)
{
int currentCoin = coins[i];
if (target >= currentCoin)
{
return currentCoin;
}
}
return -1;
}
}``````