4 kyu

Making Change: Part 2

39 of 111hencethus

Description:

Making Change: Part 2

This kata is a sequel to the first installment which can be found here: Making Change

Create a Currency class with instance methods num_ways and min_change (numWays and minChange in Javascript). A Currency instance will be initialized with an array of denominations of coins, for example, [100, 50, 25, 10, 5, 1] would represent denominations of US cents. All denominations arrays will have a 1-value coin, and all will be sorted from highest value to lowest.


num_ways or numWays

The num_ways method will take one argument, an amount of money, and will return the total possible number of ways that that amount can be made with the given denominations of coins. For example, given:

  • Ruby: us_cents = Currency.new([100, 50, 25, 10, 5, 1])
  • JavaScript: usCents = new Currency([100, 50, 25, 10, 5, 1]);

Number of ways to make 6 cents with US coin denominations, should return 2, since you can make 6 cents with 6 pennies, or with 1 nickel and 1 penny:

  • Ruby: us_cents.num_ways(6) --> 2
  • JavaScript: usCents.numWays(6) --> 2

Number of ways to make 10 cents with US coin denominations, should return 4, since you can make 10 cents with 10 pennies, 5 pennies and a nickel, 2 nickels, or 1 dime:

  • Ruby: us_cents.num_ways(10) --> 4
  • JavaScript: usCents.numWays(10) --> 4

min_change or minChange

The min_change (or minChange) method will take one argument, an amount of money, and will return the fewest possible number of coins required to make that amount. For example, given:

  • Ruby: euro_cents = Currency.new([200, 100, 50, 20, 10, 5, 2, 1])
  • JavaScript: euroCents = new Currency([200, 100, 50, 20, 10, 5, 2, 1]);

Minimum number of coins required to make 7 Euro cents, should return 2, since the fewest number of coins possible is 1 5-cent coin, and 1 2-cent coin:

  • Ruby: euro_cents.min_change(7) --> 2
  • JavaScript: euroCents.minChange(7) --> 2

Given:

  • Ruby: weird_currency = Currency.new([10, 7, 1])
  • JavaScript: weirdCurrency = new Currency([10, 7, 1]);

Minimum number of coins required to equal a value of 14 in a currency with denominations of only values 10, 7, and 1. Should return 2, since the fewest number possible is 2 7-value coins:

  • Ruby: weird_currency.min_change(14) --> 2
  • JavaScript: weirdCurrency.minChange(14) --> 2
Algorithms
Mathematics

Stats:

CreatedMay 4, 2014
PublishedMay 5, 2014
Warriors Trained459
Total Skips57
Total Code Submissions1576
Total Times Completed111
Ruby Completions39
JavaScript Completions79
Total Stars19
% of votes with a positive feedback rating92% of 42
Total "Very Satisfied" Votes38
Total "Somewhat Satisfied" Votes1
Total "Not Satisfied" Votes3
Total Rank Assessments5
Average Assessed Rank
3 kyu
Highest Assessed Rank
3 kyu
Lowest Assessed Rank
4 kyu
Ad
Contributors
  • hencethus Avatar
  • cliffstamp Avatar
Ad