Please Forward: Ruby Quiz Submission

From: James Bracy <>
Date: January 30, 2008 8:14:12 PM CST
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.

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.



    def make_change(amount, coins=[25,10,5,1], change={})
      puts change
      return change if coins.empty?

      if change.empty?
        change[coins.first] = amount / coins.first
        amount -= coins.first * change[coins.first]
        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]
            change[coins.first] = 0

      make_change(amount, coins-change.keys, change)

    def count_change(change={})
      total = 0
      change.each_pair do |coin, count|
        total += coin * count

James R. Bracy: LeTourneau University, Computer Science and Engineering
contact | - 315.935.0302


Begin forwarded message: