-4

I'm looking for the most efficient way to figure out a change amount (Quarters, dimes, nickels, and pennies) from a purchase amount. The purchase amount must be less than $1, and the change is from one dollar. I need to know how many quarters, dimes, nickels, and pennies someone would get back.

Would it be best to set up a dictionary?

tshepang
  • 12,111
  • 21
  • 91
  • 136
Jb.
  • 109
  • 2
  • 2
  • 9
  • Yes this is a homework problem, but what I'm trying to find is the most efficient method. It seems everyone is voting for the recursion method, which from what I understand is much more process hungry than a loop method. – Jb. Mar 08 '09 at 02:16
  • Process hungry? Just how many denominations of coins are you working with? – Chris B. Mar 08 '09 at 04:43
  • I know it's minimal, but why not learn to trim on small programs to pass along to much longer programs. Better style. – Jb. Mar 08 '09 at 05:41

6 Answers6

7

Gee, you mean this isn't problem 2b in every programming course any more? Eh, probably not, they don't seem to teach people how to make change any more either. (Or maybe they do: is this a homework assignment?)

If you find someone over about 50 and have them make change for you, it works like this. Say you have a check for $3.52 and you hand the cashier a twnty. They'll make change by saying "three fifty-two" then

  • count back three pennies, saying "three, four, five" (3.55)
  • count back 2 nickels, (3.60, 3.65)
  • count back a dime (3.75)
  • a quarter (4 dollars)
  • a dollar bill (five dollars)
  • a $5 bill (ten dollars)
  • a $10 bill (twenty.)

That's at heart a recursive process: you count back the current denomination until the current amount plus the next denomination comes out even. Then move up to the next denomination.

You can, of course, do it iteratively, as above.

Charlie Martin
  • 110,348
  • 25
  • 193
  • 263
4

This is probably pretty fast - just a few operations per denomination:

def change(amount):
    money = ()
    for coin in [25,10,5,1]
        num = amount/coin
        money += (coin,) * num
        amount -= coin * num

    return money
Kenan Banks
  • 207,056
  • 34
  • 155
  • 173
0

The above soloution working.

amount=int(input("Please enter amount in pence"))
coins = [50, 25, 10, 5, 2, 1]
coinsReturned = []
for i in coins:
  while amount >=i:
        coinsReturned.append(i)
        amount = amount - i
print(coinsReturned)

Alternatively a solution can reached by using the floor and mod functions.

amount = int(input( "Please enter amount in pence" ))
# math floor of 50
fifty = amount // 50
# mod of 50 and floor of 20
twenty = amount % 50 // 20
# mod of 50 and 20 and floor of 10
ten = amount % 50 % 20 // 10
# mod of 50 , 20 and 10 and floor of 5
five = amount % 50 % 20 % 10 // 5
# mod of 50 , 20 , 10 and 5 and floor of 2
two = amount % 50 % 20 % 10 % 5 // 2
# mod of 50 , 20 , 10 , 5 and 2 and floor of 1
one = amount % 50 % 20 % 10 % 5 % 2 //1

print("50p>>> " , fifty , " 20p>>> " , twenty , " 10p>>> " , ten , " 5p>>> " , five , " 2p>>> " , two , " 1p>>> " , one )

Or another solution

amount=int(input("Please enter the change to be given"))
endAmount=amount

coins=[50,25,10,5,2,1]
listOfCoins=["fifty" ,"twenty five", "ten", "five", "two" , "one"]
change = []

for coin in coins:
    holdingAmount=amount
    amount=amount//coin
    change.append(amount)
    amount=holdingAmount%coin

print("The minimum coinage to return from " ,endAmount, "p is as follows")
for i in range(len(coins)):
  print("There's " , change[i] ,"....",  listOfCoins[i] , "pence pieces in your change" )
user3234931
  • 83
  • 1
  • 3
0

Your best bet is to probably have a sorted dictionary of coin sizes, and then loop through them checking if your change is greater than the value, add that coin and subtract the value, otherwise move along to the next row in the dictionary.

Eg

Coins = [50, 25, 10, 5, 2, 1]
ChangeDue = 87
CoinsReturned = []
For I in coins:
   While I >= ChangeDue:
        CoinsReturned.add(I)
        ChangeDue = ChangeDue - I

Forgive my lousy python syntax there. Hope that's enough to go on.

Matthew Steeples
  • 7,858
  • 4
  • 34
  • 49
0

This problem could be solved pretty easy with integer partitions from number theory. I wrote a recursive function that takes a number and a list of partitions and returns the number of possible combinations that would make up the given number.

http://sandboxrichard.blogspot.com/2009/03/integer-partitions-and-wiki-smarts.html

It's not exactly what you want, but it could be easily modified to get your result.

Ben Hayden
  • 1,349
  • 9
  • 15
0

I have an improve solution from above solutions

coins=[]
cost = float(input('Input the cost: '))
give = float(input('Tipe Amount given: '))

change = (give - cost)
change2 = change-int(change)
change2 = round(change2,2)*100
coin = [50,25,10,5,1]
for c in coin:
    while change2>=c:
        coins.append(c)
        change2 = change2-c
    
half=coins.count(50)
qua = coins.count(25)
dime=coins.count(10)
ni=coins.count(5)
pen=coins.count(1)
dolars = int(change)
if half !=0 or qua != 0 or dime!=0 or ni!=0 or pen!=0:
    print ('The change of', round(give,2), 'is:',change, 'like \n-dolas:',dolars,'\n-halfs:',half,'\n-quarters:',qua,'\n-dime:',dime,'\n-nickels:',ni,'\n-pennies:',pen)
else:
     print ('The change from', round(give,2), 'is:',change,'and no coins')