# Rubyquiz: Making Change (#154)

**URL:** <https://www.ruby-forum.com/t/rubyquiz-making-change-154/127623>\
**Category:** Ruby\
**Created:** [January 30, 2008, 10:42pm UTC](https://www.ruby-forum.com/t/rubyquiz-making-change-154/127623 "2008-01-30T22:42:19Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![peterpiper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3e96dc/32.png) [@peterpiper](https://www.ruby-forum.com/u/peterpiper)\
**Post date:** [January 30, 2008, 10:42pm UTC](https://www.ruby-forum.com/t/rubyquiz-making-change-154/127623/1 "2008-01-30T22:42:19Z")

</div>

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 = [25, 10, 5, 1])  
return “no solution” if amount==0  
init\_amount=amount  
possible\_change = []  
change = []  
coins.sort!

#iterates through each coin to make the change, making all possible  
sets of change  
(coins.length-1).downto(0) do |start\_coin|  
start\_coin.downto(0) do |x|  
while amount\>= coins[x]  
if (amount - coins[x])\>= 0  
amount -= coins[x]  
change\<\< coins[x]  
else  
next  
end  
end

```
  if amount==0 then
    possible_change< 0
change = possible_change.sort[0][1]

```

else  
change = [“no solution”]  
end

```
    #print some pretty text

```

print “#{init\_amount}: #{change.join(’, ')}\n”  
end

Ian
