Please Forward: Ruby Quiz Submission

From: James Bracy <waratuman86@gmail.com>
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

···

Begin forwarded message: