From: James Gray Date: 2008-01-31T13:12:51+09:00 Subject: Fwd: Please Forward: Ruby Quiz Submission --Apple-Mail-1-107251906 Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes Content-Transfer-Encoding: 7bit Begin forwarded message: > From: James Bracy > Date: January 30, 2008 8:14:12 PM CST > To: submission@rubyquiz.com > Subject: Please Forward: Ruby Quiz Submission > > Here is the url to my blog with the code posted. This is my first > post, still learning how things are done on ruby quiz. > > http://waratuman86.com/articles/2008/01/30/ruby-quiz-making-change > > And here is the code. It is incomplete, but I have stated and > example on the site and how in theory it should work. > > Basically the code starts with the small coin amount and works it's > way up trying to use the bigger coins instead of the smaller. > > Thanks, > > James > > def make_change(amount, coins=[25,10,5,1], change={}) > coins.sort! > puts change > return change if coins.empty? > > if change.empty? > change[coins.first] = amount / coins.first > amount -= coins.first * change[coins.first] > else > change.each_pair do |k, count| > change_count = count_change(change) > if (amount+change_count) % coins.first == 0 > change[coins.first] = (amount+change_count) / coins.first > change[k] = (change_count + amount - > change[coins.first]*coins.first) / k > amount -= change[coins.first] - change[k] > else > change[coins.first] = 0 > end > end > end > > make_change(amount, coins-change.keys, change) > end > > def count_change(change={}) > total = 0 > change.each_pair do |coin, count| > total += coin * count > end > total > end > > James R. Bracy: LeTourneau University, Computer Science and > Engineering > contact | jamesbracy@letu.edu - 315.935.0302 > --Apple-Mail-1-107251906--