[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Rubyquiz: Making Change (#154

Ian E^n

1/30/2008 9:41:00 PM


This is my first rubyquiz solution. I think it does as intended although it=
doesn't have much error checking for the arguments. Enjoy!


def make_change(amount, coins =3D [25, 10, 5, 1])
return "no solution" if amount=3D=3D0
init_amount=3Damount
possible_change =3D []
change =3D []
coins.sort!
=09
#iterates through each coin to make the change, making all possible sets o=
f change
(coins.length-1).downto(0) do |start_coin|
start_coin.downto(0) do |x|
while amount>=3D coins[x]
if (amount - coins[x])>=3D 0
amount -=3D coins[x]
change<< coins[x]
else
next
end
end
=09
if amount=3D=3D0 then
possible_change< 0
change =3D possible_change.sort[0][1]
else
change =3D ["no solution"]
end

#print some pretty text
print "#{init_amount}: #{change.join(', ')}\n"
end

Ian
_________________________________________________________________
Shed those extra pounds with MSN and The Biggest Loser!
http://biggestloser...