Velocity Reviews > Ruby > [QUIZ] Making Change (#154)

# [QUIZ] Making Change (#154)

Atsuhiro Teshima
Guest
Posts: n/a

 01-30-2008
Jesús Gabriel y Galán wrote:

> 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].

Now,I'm trying to fix this problem.

> 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

Oh, obviously this code is better than mine.I'll fix it.

Atsuhiro Teshima
--
Posted via http://www.ruby-forum.com/.

Pit Capitain
Guest
Posts: n/a

 01-31-2008
2008/1/30, Raffa <(E-Mail Removed)>:
> (... interesting post ...)

Raffa, I think your algorithm doesn't always find the best solution. Try it with

make_change(24, [10, 7, 1])

What would be the answer of your algorithm? I think it would be one of

2*10 + 4*1
3*7 + 3*3

but not

1*10 + 2*7

Regards,
Pit

Amey Dhoke
Guest
Posts: n/a

 01-31-2008
[Note: parts of this message were removed to make it a legal post.]

Hi,

I joined Ruby Talk couple of days before and this is my first post.
I am a newbie and tring to learn Ruby from last one month.

I have written spec for the problem (This is the first time I am playing
with RSpec) - http://pastie.caboo.se/145869

And this is my (naive ) solution - http://pastie.caboo.se/145888

Solving this first problem was fun. I learned a lot from everyone's
post.

Cheers,
Amey