On Jan 29, 2008 11:33 PM, Atsuhiro Teshima <atsuhiro1107 / gmail.com> wrote:
> Hi, I'm Atsuhiro.
> this is the first time I contribute to Ruby-forum.

Hi, welcome to the Ruby community...

> the following is my solution
> (To tell the truth, I have only one-month programming experience and
> Ruby is the first programming language I choose.So, I'll glad if you
> point out some problem in my solution.)

The first problem is that your solution returns
[10,1,1,1,1] for make_change(14, [10,7,1])
when the correct answer would be [7,7].

Next, some comments on the code

> class Making_change
> $change = []

you don't need a global variable here.
If you wanted to store the latest change
given by an instance of this class (but for the
problem at hand you don't even need that), you could
use a instance variable of the class:

@change

> def make_change(amount,coins=[25,10,5,1])
>    @coing = coins

here you are storing the coins in an instance variable,
but it's not needed since every invocation of the make_change
method carries the coins array, so it's not some state you
would need to store anyway, so I'd just remove this altogether.
As you should remove the $change also, change should be
a local variable to this method so:

change = []

>    if amount < 0
>       print "amount should be a positive integer \n"
>       exit
>    end
>
>    coins.each do |i|
>       if amount >= i
>          $change << i
>          amount = amount-i
>          redo
>       elsif amount == 0
>          return $change
>       else next
>       end
>    end
> end
> end

To avoid iterating so much, check at Ilan Berci solution.
For each coin value, making a division you would know
how many of this coins can fit, something like (not tested):

num = amount / coin
num.times {change << coin}
amount %= coin

> a = Making_change.new
> p a.make_change(52) # => [25,25,1,1]
> p a.make_change(-30) # => amount should be a positive integer

Anyway, this algorithm has the problem of the two middles:

p a.make_change(14, [10,7,1]) # => [10,1,1,1,1], should be [7,7]

Hope this helps,

Jesus.