Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Rubyquiz: Making Change (#154)

Reply
Thread Tools

Rubyquiz: Making Change (#154)

 
 
Ian Evans
Guest
Posts: n/a
 
      01-30-2008

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/=

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Making a small change to a large XML document Dan Stromberg Python 3 09-25-2007 06:59 AM
A Paradise DNS address change? What change? There was no change. Tony Neville NZ Computing 7 09-22-2006 01:02 PM
Re: A fresh new approach to making making online Fakename Computer Information 0 11-18-2005 02:10 AM
Making new Flavors : Making a custom transferhandler for and drop applications ebby83@gmail.com Java 5 01-12-2005 11:10 AM
Data Structure and making the size change dsa89 C++ 3 11-08-2004 06:34 AM



Advertisments