Reputation: 21
I was asked to write a program in python that will correctly find the combination of coins, and the amount of each coin, in a given amount of money. In this problem only nickels and dimes are used.
Ex - Given that there are 10 coins, how many nickels and dimes are in $.85?
This is how I solved it:
Set up equations:
d + n = 10
.10d + .05n = .85
Solve for n:
n = 10 - d
Solve:
.10d + .05(10-d) = .85
.05d + .5 -.05d = .85
.05d = .35
d = 7
n = 3
How would I go about programming this?
Sorry if this is a stupid question but Im pretty new to python and I'm completely lost on this one.
Upvotes: 2
Views: 1471
Reputation: 1
Or rather than iterate through possible combos of coins, you could use (given total and numcoins as input):
justnickels = total/.05
numdimes = justnickels - numcoins
numnickels = numcoins - numdimes
If you get negative numbers as one of the answers, then the particular combo (like .85 composed of 5 coins) is unsolvable.
Upvotes: 0
Reputation: 13410
Let the number of coins be a
, so d + n = a
.
Let the sum be b
, so 0.1d + 0.05n = b
.
Then
n = a - d
0.1d+0.05(a-d)=b
0.05d = b-0.05a
d = 20b - a
n = a - d = a - 20b +a = 2a - 20b
So, given a
and b
:
d = 20b - a
n = a - d
Now we just need to program these 2 formulae in Python.
Look at the examples in the official docs: http://docs.python.org/tutorial/controlflow.html#defining-functions
def count(num, total_sum):
d = 20*total_sum - num
n = num - d
return (n,d)
print count(10, 0.85)
Upvotes: 5
Reputation: 5750
If you only have dimes and nickels, you can just do the following:
>>> total_coins = 10
>>> nickels = 85 / 5 # 85 is the total amount in cents; 5 is the value of a nickel
>>> nickels
17
>>> dimes = 0
>>> while dimes + nickels != total_coins:
... dimes += 1
... nickels -= 2
...
>>> dimes
7
>>> nickels
3
>>>
Since there are 2 nickels per dime, you can figure out how many nickels there are and add one to the dimes for each two nickels, until you have the right number of coins.
Upvotes: 1
Reputation: 14066
No points for style, but a simple search of all possibilities is quick to write and fast enough for practical purposes. Just start with all nickels, no dimes, then keep adding one to dimes and removing one from nickels until you get the answer (or don't).
def solve(ncoins, cents):
nickels = ncoins
dimes = 0
for ii in range(ncoins):
if (nickels * 5) + (dimes * 10) == cents:
return "{nickels} nickels, {dimes} dimes".format(
nickels=nickels, dimes=dimes)
nickels -= 1
dimes += 1
raise AssertionError("{ncoins} coins can't be {cents} cents!".format(
ncoins=ncoins, cents=cents))
print solve(10, 85)
print solve(10, 75)
print solve(100, 75)
output:
3 nickels, 7 dimes
5 nickels, 5 dimes
Traceback (most recent call last):
File "/home/akg/tmp/sacoins.py", line 16, in <module>
print solve(100, 75)
File "/home/akg/tmp/sacoins.py", line 10, in solve
raise AssertionError("{ncoins} coins can't be {cents} cents!".format(ncoins=ncoins, cents=cents))
AssertionError: 100 coins can't be 75 cents!
Upvotes: 2