[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

[QUIZ] Making Change (#154

James Gray

1/25/2008 3:51:00 PM

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

3. Enjoy!

Suggestion: A [QUIZ] in the subject of emails about the problem helps everyone
on Ruby Talk follow the discussion. Please reply to the original quiz message,
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.

102 Answers

Warren Brown

1/25/2008 4:55:00 PM

0

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

1/25/2008 6:07:00 PM

0

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

1/25/2008 6:31:00 PM

0

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

ThoML

1/25/2008 7:50:00 PM

0

Are the coin values always given in descending order?

regards,
thomas.

James Gray

1/25/2008 8:21:00 PM

0

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

1/25/2008 8:54:00 PM

0

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

1/25/2008 9:10:00 PM

0

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

1/25/2008 9:24:00 PM

0

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

1/25/2008 10:27:00 PM

0

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.

Robert Dober

1/25/2008 10:30:00 PM

0

On Jan 25, 2008 9:20 PM, James Gray <james@grayproductions.net> wrote:
> 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()? :)
funny mistake of yours James :)
Robert