Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   [QUIZ] Making Change (#154) (http://www.velocityreviews.com/forums/t847666-quiz-making-change-154-a.html)

 Ruby Quiz 01-25-2008 03:50 PM

[QUIZ] Making Change (#154)

The three rules of Ruby Quiz:

1. Please do not post any solutions or spoiler discussion for this quiz until
48 hours have passed from the time on this message.

2. Support Ruby Quiz by submitting ideas as often as you can:

http://www.rubyquiz.com/

3. Enjoy!

Suggestion: A [QUIZ] in the subject of emails about the problem helps everyone
if you can.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

In "Practical Ruby Projects," the author includes a couple of chapters involving
coin simulations. These simulators are used to explore the possibilities of
replacing a certain coin or adding a new coin.

One interesting subproblem of these simulations is that of making change. For
example, if we need to give 39 cents change in the United States (where there
are 25, 10, 5, and 1 cent pieces), we can give:

>> make_change(39)

=> [25, 10, 1, 1, 1, 1]

What if the coins were 10, 7, and 1 cent pieces though and we wanted to make 14
cents change? We would probably want to do:

>> make_change(14, [10, 7, 1])

=> [7, 7]

This week's Ruby Quiz is to complete a change making function with this
skeleton:

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

end

Your function should always return the optimal change with optimal being the
least amount of coins involved. You can assume you have an infinite number of
coins to work with.

 Warren Brown 01-25-2008 04:55 PM

Re: [QUIZ] Making Change (#154)

James,

> What if the coins were 10, 7, and 1 cent pieces though
> and we wanted to make 14 cents change? We would
> probably want to do:
>=20
> >> make_change(14, [10, 7, 1])
> =3D> [7, 7]
> ...
> Your function should always return the optimal change
> with optimal being the least amount of coins involved.

Do you have a preference for breaking ties? For example, which
would you prefer for make_change(21, [10, 7, 1]): [7, 7, 7] or [10, 10,
1]?

Warren Brown

 James Gray 01-25-2008 06:06 PM

Re: [QUIZ] Making Change (#154)

On Jan 25, 2008, at 10:55 AM, Warren Brown wrote:

> James,
>
>> What if the coins were 10, 7, and 1 cent pieces though
>> and we wanted to make 14 cents change? We would
>> probably want to do:
>>
>> >> make_change(14, [10, 7, 1])
>> => [7, 7]
>> ...
>> Your function should always return the optimal change
>> with optimal being the least amount of coins involved.

>
> Do you have a preference for breaking ties? For example, which
> would you prefer for make_change(21, [10, 7, 1]): [7, 7, 7] or [10,
> 10,
> 1]?

No preference. Either is a valid answer.

James Edward Gray II

 Joshua Ballanco 01-25-2008 06:30 PM

Re: Making Change (#154)

James Gray wrote:
> On Jan 25, 2008, at 10:55 AM, Warren Brown wrote:
>
>>> with optimal being the least amount of coins involved.

>>
>> Do you have a preference for breaking ties? For example, which
>> would you prefer for make_change(21, [10, 7, 1]): [7, 7, 7] or [10,
>> 10,
>> 1]?

>
> No preference. Either is a valid answer.
>
> James Edward Gray II

I don't know...I hate pennies. IMHO, any answer that minimizes the
number of pennies should win out.
--
Posted via http://www.ruby-forum.com/.

 tho_mica_l 01-25-2008 07:49 PM

Re: Making Change (#154)

Are the coin values always given in descending order?

regards,
thomas.

 James Gray 01-25-2008 08:20 PM

Re: Making Change (#154)

On Jan 25, 2008, at 1:49 PM, tho_mica_l wrote:

> Are the coin values always given in descending order?

Is it that hard to call sort()? :)

I don't mind if you want to assume they are.

James Edward Gray II

 Dominik Honnef 01-25-2008 08:53 PM

Re: [QUIZ] Making Change (#154)

On [Sat, 26.01.2008 00:50], Ruby Quiz wrote:
> In "Practical Ruby Projects," the author includes a couple of chapters involving
> coin simulations. These simulators are used to explore the possibilities of
> replacing a certain coin or adding a new coin.

Are there any advanced combinations of change/array of coins which are likely to fail
on some implementations?
--
Dominik Honnef

 Jesús Gabriel y Galán 01-25-2008 09:09 PM

Re: [QUIZ] Making Change (#154)

On Jan 25, 2008 9:53 PM, Dominik Honnef <dominikho@gmx.net> wrote:
> On [Sat, 26.01.2008 00:50], Ruby Quiz wrote:
> > In "Practical Ruby Projects," the author includes a couple of chapters involving
> > coin simulations. These simulators are used to explore the possibilities of
> > replacing a certain coin or adding a new coin.

>
> Are there any advanced combinations of change/array of coins which are likely to fail
> on some implementations?

Is it ok to share test cases before the spoiler? I assume it is, and
it this case everybody could send a couple...

Jesus.

 James Gray 01-25-2008 09:24 PM

Re: [QUIZ] Making Change (#154)

On Jan 25, 2008, at 3:09 PM, Jes=FAs Gabriel y Gal=E1n wrote:

> On Jan 25, 2008 9:53 PM, Dominik Honnef <dominikho@gmx.net> wrote:
>> On [Sat, 26.01.2008 00:50], Ruby Quiz wrote:
>>> In "Practical Ruby Projects," the author includes a couple of =20
>>> chapters involving
>>> coin simulations. These simulators are used to explore the =20
>>> possibilities of
>>> replacing a certain coin or adding a new coin.

>>
>> Are there any advanced combinations of change/array of coins which =20=

>> are likely to fail
>> on some implementations?

>
> Is it ok to share test cases before the spoiler?

Sure.

James Edward Gray II

 Jesús Gabriel y Galán 01-25-2008 10:27 PM

Re: [QUIZ] Making Change (#154)

On Jan 25, 2008 10:24 PM, James Gray <james@grayproductions.net> wrote:
> On Jan 25, 2008, at 3:09 PM, Jes=FAs Gabriel y Gal=E1n wrote:
> > Is it ok to share test cases before the spoiler?

>
> Sure.

This is what I'm currently working on:

require 'test/unit'

class TestMakeChange < Test::Unit::TestCase
def test_zero
assert_equal([], make_change(0))
end

def test_change_equal_to_one_coin
assert_equal([10], make_change(10, [10, 7, 1]))
assert_equal([7], make_change(7, [10, 7, 1]))
end

def test_two_middles
assert_equal([7, 7], make_change(14, [10, 7, 1]))
end
end

For now:

3 tests, 3 assertions, 3 failures, 0 errors

:-(

It's not really surprising, since I still have an empty method :-)

Jesus.

All times are GMT. The time now is 12:25 PM.