21
6
...counted!
You will pass your program a variable which represents a quantity of money in dollars and/or cents and an array of coin values. Your challenge is to output the number of possible combinations of the given array of coin values that would add up to the amount passed to the code. If it is not possible with the coins named, the program should return 0
.
Note on American numismatic terminology:
- 1-cent coin: penny
- 5-cent coin: nickel
- 10-cent coin: dime
- 25-cent coin: quarter (quarter dollar)
Example 1:
Program is passed:
12, [1, 5, 10]
(12 cents)
Output:
4
There are 4 possible ways of combining the coins named to produce 12 cents:
- 12 pennies
- 1 nickel and 7 pennies
- 2 nickels and 2 pennies
- 1 dime and 2 pennies
Example 2:
Program is passed:
26, [1, 5, 10, 25]
(26 cents)
Output:
13
There are 13 possible ways of combining the coins named to produce 26 cents:
- 26 pennies
- 21 pennies and 1 nickel
- 16 pennies and 2 nickels
- 11 pennies and 3 nickels
- 6 pennies and 4 nickels
- 1 penny and 5 nickels
- 16 pennies and 1 dime
- 6 pennies and 2 dimes
- 11 pennies, 1 dime, and 1 nickel
- 6 pennies, 1 dime, and 2 nickels
- 1 penny, 1 dime, and 3 nickels
- 1 penny, 2 dimes, and 1 nickel
- 1 quarter and 1 penny
Example 3:
Program is passed:
19, [2, 7, 12]
Output:
2
There are 2 possible ways of combining the coins named to produce 19 cents:
- 1 12-cent coin and 1 7-cent coin
- 1 7-cent coin and 6 2-cent coins
Example 4:
Program is passed:
13, [2, 8, 25]
Output:
0
There are no possible ways of combining the coins named to produce 13 cents.
This has been through the Sandbox. Standard loopholes apply. This is code golf, so the answer with the fewest bytes wins.
1s/counted/earned – mbomb007 – 2016-10-20T20:56:12.723
1Can we input the coin values in reverse order (i.e.
[25, 10, 5, 1]
)? – ETHproductions – 2016-10-20T20:56:34.260Can we assume that the list is not empty? – Christian Sievers – 2016-10-20T22:49:17.097
@ETHproductions, I would say yes. – anonymous2 – 2016-10-20T23:46:46.797
@ChristianSievers, again, I would say yes. – anonymous2 – 2016-10-20T23:47:02.623
4@mbomb007 For four bytes:
s/count/earn
. – wizzwizz4 – 2016-10-21T06:04:23.0205For me and I guess for other people who don't pay with dollars it's not obvious what a nickel and a dime is. It wasn't hard to figure it out, but maybe you could write it a bit more international? – Kritzefitz – 2016-10-21T07:59:39.527
2@Kritzefitz. I've added that to the question. – TRiG – 2016-10-21T12:04:59.230
and what would be the result of 1209[1,5,10,33,48] and 6000[1,5,10,33] so i can calibrate my code (not i say i would post another answers ) – RosLuP – 2016-10-21T18:57:12.740
Pedantic note on terminology: The official name for the US 1-cent coin is cent. Penny only comes up in informal usage, or when talking about the UK 1-cent coin.
– jpaugh – 2016-10-21T22:50:54.6632
@jpaugh: While coin-o-philes might agree, I'd have to disagree. A penny is the standard coin that has a value of one cent. Fifty-four cents is an amount of money. Fifty-four pennies is explicitly fifty-four coins. It's also called a "one-cent coin", or (officially) a "one-cent piece". I can't think of any formal setting where the word "penny" would be unacceptable. These people, who are specifically about collecting coins, have no problem calling it a "penny".
– MichaelS – 2016-10-22T01:20:30.457I wonder why you didn't make them print the combinations. That would have confounded the Froeniuses :) – GreenAsJade – 2016-10-22T11:20:33.263
@MichaelS As you say, popular usage makes the official name fairly moot, and I find that fascinating. I myself refer to this coin almost exclusively as "the penny," while referring to the amount as "one cent." I imagine the US wanted to distinguish itself from Britain at the time, but changing a long-established convention is not so easy! – jpaugh – 2016-10-23T01:19:19.633
Sorry, @jpaugh, I'm not from the US. In Canada, we definitely follow the nomenclature that MichaelS outlined. I really don't know what the facts are in the States... Sorry for the obscurity there! – anonymous2 – 2016-10-23T02:22:32.773
@anonymous2 No sweat. Just an obscure oddity of language. (In the US, we certainly follow the nomenclature MichaelS mentioned. It's just not "official.") – jpaugh – 2016-10-23T03:43:17.970
Ah, ok. I've got you. I don't know whether it's official or not in Canada... :) – anonymous2 – 2016-10-23T10:54:38.003
So i did not post a solution for this, and have the solutions... Is i'am that cancel my solution of this or who? Thanks... – RosLuP – 2017-09-02T18:38:57.287