Rubyquiz: Making Change (#154)

I

Ian Evans

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.msn.com/=
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top