[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: Learn to Program, by Chris Pine

William (Bill) Froelich

4/1/2006 5:16:00 AM

First off, thanks to Dave for providing the explanation that should have
been in place of just my solution.

I also recommend putting the "beer = beer - 1" at the end of the loop
but that won't immediately allow you to remove the if unless you also
look at changing the loop criteria or provide a special check to handle
the last time through the loop. Assuming you want to end with 0 bottles
of beer on the wall, then your last iteration through the loop will need
to be when beer = 1 which means you should also look to change your
while criteria. Either of the following would work:

While beer >= 1

Or

While beer > 0

Both of these will allow you to remove the extra if check.

Keep it up!

--Bill


-----Original Message-----
From: Dave Burt [mailto:dave@burt.id.au]
Sent: Friday, March 31, 2006 8:14 PM
To: ruby-talk ML
Subject: Re: Learn to Program, by Chris Pine

JB wrote:
> Okay, here's what I got, and it works too, heh. The 'if' had to be in

> there, or else it kept ending with negative numbers (usually -2). This

> way is as good as Bill's, but a lot shorter (not putting you down or
> anything Bill!).
>
> beer = 99
> while beer >= 0
> beer = beer - 1
> if (beer - 1) >= 0
> puts beer.to_s + ' ' 'bottles of beer on the wall,' +
> beer.to_s + ' bottles of beer,' +
> 'take one down, pass it around,' +
> (beer - 1).to_s + ' ' + 'bottles of beer on the wall!'
> end
> end

Like I told Jan, put the "beer = beer - 1" at the end of the loop - then
you won't need that "if". By the way, that "if" covers the whole loop.
Wouldn't it be simpler just to exit the loop instead of going through
again but doing nothing? Here are the two alternatives I'm suggesting:

beer = 100 # have to add 1, because the first thing we're doing is
subtracting 1!
while (beer - 1) >= 0 # a little better - no need for the inside "if"
beer = beer - 1
puts ...
end

beer = 99
while beer >= 0
puts ...
beer = beer - 1
end

Also, check out the typo here:
> puts beer.to_s + ' ' 'bottles of beer on the wall,' +

' ' 'bottles...' is the same as ' ' + 'bottles...'. You should change it
to ' bottles...'.

> Now, to clean it up even more...when it gets down to '1 bottle', I
> don't want it to say '1 bottles...' (plural). How would I do this? I
> tried with an 'else' in there, but for some reason I either made it an

> infinite loop or it kept getting the negative numbers all over again.
> I tried with an 'else (beer - 1) == 1' (no quotes of course and that
> didn't work either.

Bill's solution did this! You might want to read it and figure out how.
Still, I would prefer to do it a different way:

beer = 99
s = ''
while beer >= 0
if beer == 1
s = ''
else
s = 's'
end
puts ... + 'bottle' + s + ...
beer = beer - 1
end

> I do appreciate all the help you guys are giving, and yes, the
> interactive way will make me work a little harder too to get things
> right, plus I'm the kind who *needs* that kind of help or I fail
> miserably and give up, heh.

We'll keep you accountable. Don't give up.

Cheers,
Dave