Chris Pine Tutorial 99 Bottles of Beer Program

Just a beginner with a question about this:

<i>• "99 bottles of beer on the wall..." Write a program which prints
out the lyrics to that beloved classic, that field-trip favorite: "99
Bottles of Beer on the Wall."</i>

This is what I came up with:

bottles = 100

while bottles > 1

puts (bottles-1).to_s + " Bottles of beer on the wall, " +
(bottles-1).to_s + " Bottles of beer! You take one down, you pass it
around and " + (bottles-2).to_s + " Bottles of beer on the wall!"

bottles = (bottles-1)

end

Is this ok?

Are there faster or better ways?

Thanks.

Also, would there be an easy way to print the words out instead of the
numbers of bottles on the wall?

Have a look at the #downto method.

hth a little,
Todd

···

On Tue, Aug 26, 2008 at 6:30 PM, danielj <sleepingindian@gmail.com> wrote:

Just a beginner with a question about this:

<i>• "99 bottles of beer on the wall..." Write a program which prints
out the lyrics to that beloved classic, that field-trip favorite: "99
Bottles of Beer on the Wall."</i>

This is what I came up with:

bottles = 100

while bottles > 1

puts (bottles-1).to_s + " Bottles of beer on the wall, " +
(bottles-1).to_s + " Bottles of beer! You take one down, you pass it
around and " + (bottles-2).to_s + " Bottles of beer on the wall!"

bottles = (bottles-1)

end

Is this ok?

Are there faster or better ways?

pls use interpolation:

     puts "#{bottles-1} bottles of beer on the wall"

is much more idiomatic... cleaner too.

···

On Aug 26, 2008, at 16:30 , danielj wrote:

puts (bottles-1).to_s + " Bottles of beer on the wall, " +

Hello all, this is what I came up with, works great.

bottles_of_beer = 99
while bottles_of_beer > 1
  puts bottles_of_beer.to_s + ' bottles of beer on the wall, ' +
  bottles_of_beer.to_s + ' bottles of beer'
  bottles_of_beer = bottles_of_beer - 1
  puts 'You take one down, pass it around, ' + bottles_of_beer.to_s + '
  bottles of beer on the wall.'
end

puts 'Everyone passes out quite drunk.'

···

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

You should, as previously stated, use #downto.
You're also repeatedly calling to_s. Either use interpolation or just do
bottles_of_beer.to_s! before adding it to other strings.

···

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

This problem with this and other examples are people are offering
solution and techniques that have not been taught yet. I'm using the
same book, Chris Pine's, Learn to Program.

I feel if offering help, one should offer help based on the knowledge
imparted thus far, even if more complicated than it should be. For
instance with downto, when the challenge is given in the book, that
method hasn't been talked about or even mentioned in passing. It
shouldn't be given as a solution for people at that step in the book.
I'm trying to learn as well, and this sort of thing is the hardest when
seeking out help online.

···

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

I agree. I really wish Chris Pine's book taught this, but it does not.

James Edward Gray II

···

On Aug 26, 2008, at 7:45 PM, Ryan Davis wrote:

On Aug 26, 2008, at 16:30 , danielj wrote:

puts (bottles-1).to_s + " Bottles of beer on the wall, " +

pls use interpolation:

   puts "#{bottles-1} bottles of beer on the wall"

is much more idiomatic... cleaner too.

Divide the number by 10 and it will give the prefix (ninety-, eighty-,
etc.) You need a list or hash of some sort, of course. Simple
example...

prefixes = {9 => "ninety-", 8 => "eighty-"}

...or even better...

prefixes = ["", "", "twenty-", "thirty-", "forty-"]

...and so on. And then modify the numbers ten through nineteen after
the transformation (you don't want "one" for "eleven" do you) since
they differ in nomenclature from the others.

You could automagically use suffixes instead of prefixes if the
number, when divided by 10 is 1, but that wouldn't help you with the
edge cases of 'ten', 'eleven', and 'twelve'.

Todd

Todd

···

On Tue, Aug 26, 2008 at 6:30 PM, danielj <sleepingindian@gmail.com> wrote:

Also, would there be an easy way to print the words out instead of the
numbers of bottles on the wall?

def beersong(n=99)
  n.downto(1) do |i|
  puts <<SONG
#{i} bottles of beer on the wall,
#{i} bottles of beer!
Take one down, pass it around,
#{i > 2 ? i-1 : "no more"} bottles of beer!

SONG
  end
  puts "... and they all fell down drunk!"
end

···

On Tue, Mar 26, 2013 at 2:00 AM, Phil H. <lists@ruby-forum.com> wrote:

Hello all, this is what I came up with, works great.

bottles_of_beer = 99
while bottles_of_beer > 1
  puts bottles_of_beer.to_s + ' bottles of beer on the wall, ' +
  bottles_of_beer.to_s + ' bottles of beer'
  bottles_of_beer = bottles_of_beer - 1
  puts 'You take one down, pass it around, ' + bottles_of_beer.to_s + '
  bottles of beer on the wall.'
end

puts 'Everyone passes out quite drunk.'

Fixnum#to_s! doesn't exit.

···

On Tue, Mar 26, 2013 at 3:11 AM, Joel Pearson <lists@ruby-forum.com> wrote:

You should, as previously stated, use #downto.
You're also repeatedly calling to_s. Either use interpolation or just do
bottles_of_beer.to_s! before adding it to other strings.

its most interesting because tips demonstrate in ruby how to build succinct
and penetrating code. good tips are a godsend also coaching creates the
culture.

···

On Fri, Mar 29, 2013 at 1:33 AM, Phil H. <lists@ruby-forum.com> wrote:

This problem with this and other examples are people are offering
solution and techniques that have not been taught yet. I'm using the
same book, Chris Pine's, Learn to Program.

I feel if offering help, one should offer help based on the knowledge
imparted thus far, even if more complicated than it should be. For
instance with downto, when the challenge is given in the book, that
method hasn't been talked about or even mentioned in passing. It
shouldn't be given as a solution for people at that step in the book.
I'm trying to learn as well, and this sort of thing is the hardest when
seeking out help online.

--
Posted via http://www.ruby-forum.com/\.

One of the big problems with asking questions in an anonymous mailing
list is expecting the people answering to be your teacher. If you're
really at that level and need that much handholding, take a class, or
find a tutor, but if you shout out a question into a large space where
you know no one and no one knows you, stop complaining about the
answers.

···

On Thu, Mar 28, 2013 at 9:33 AM, Phil H. <lists@ruby-forum.com> wrote:

This problem with this and other examples are people are offering
solution and techniques that have not been taught yet. I'm using the
same book, Chris Pine's, Learn to Program.

Okay, but we don't know what the book has or hasn't taught, so how can we
know not to say to use downto? Besides, even if it hasn't been covered, I
don't think it's too much to go look the method up in the docs and
understand it.

-Josh

···

On Thu, Mar 28, 2013 at 9:33 AM, Phil H. <lists@ruby-forum.com> wrote:

This problem with this and other examples are people are offering
solution and techniques that have not been taught yet. I'm using the
same book, Chris Pine's, Learn to Program.

I feel if offering help, one should offer help based on the knowledge
imparted thus far, even if more complicated than it should be. For
instance with downto, when the challenge is given in the book, that
method hasn't been talked about or even mentioned in passing. It
shouldn't be given as a solution for people at that step in the book.
I'm trying to learn as well, and this sort of thing is the hardest when
seeking out help online.

--
Posted via http://www.ruby-forum.com/\.

http://p.ramaze.net/1900
my stab at it

^ manveru

···

On Wed, Aug 27, 2008 at 9:57 AM, Todd Benson <caduceass@gmail.com> wrote:

On Tue, Aug 26, 2008 at 6:30 PM, danielj <sleepingindian@gmail.com> wrote:

Also, would there be an easy way to print the words out instead of the
numbers of bottles on the wall?

Divide the number by 10 and it will give the prefix (ninety-, eighty-,
etc.) You need a list or hash of some sort, of course. Simple
example...

prefixes = {9 => "ninety-", 8 => "eighty-"}

...or even better...

prefixes = ["", "", "twenty-", "thirty-", "forty-"]

...and so on. And then modify the numbers ten through nineteen after
the transformation (you don't want "one" for "eleven" do you) since
they differ in nomenclature from the others.

You could automagically use suffixes instead of prefixes if the
number, when divided by 10 is 1, but that wouldn't help you with the
edge cases of 'ten', 'eleven', and 'twelve'.

Hello all, this is what I came up with, works great.

bottles_of_beer = 99
while bottles_of_beer > 1
  puts bottles_of_beer.to_s + ' bottles of beer on the wall, ' +
  bottles_of_beer.to_s + ' bottles of beer'
  bottles_of_beer = bottles_of_beer - 1
  puts 'You take one down, pass it around, ' + bottles_of_beer.to_s + '
  bottles of beer on the wall.'
end

puts 'Everyone passes out quite drunk.'

def beersong(n=99)
  n.downto(1) do |i|
  puts <<SONG
#{i} bottles of beer on the wall,
#{i} bottles of beer!
Take one down, pass it around,
#{i > 2 ? i-1 : "no more"} bottles of beer!

AAUGH, off by one:

#{i>1 ? i-1 : "No more"} bottles of beer!

···

On Tue, Mar 26, 2013 at 7:08 AM, tamouse mailing lists <tamouse.lists@gmail.com> wrote:

On Tue, Mar 26, 2013 at 2:00 AM, Phil H. <lists@ruby-forum.com> wrote:

SONG
  end
  puts "... and they all fell down drunk!"
end

Josh Cheek wrote in post #1103316:

···

On Tue, Mar 26, 2013 at 3:11 AM, Joel Pearson <lists@ruby-forum.com> > wrote:

You should, as previously stated, use #downto.
You're also repeatedly calling to_s. Either use interpolation or just do
bottles_of_beer.to_s! before adding it to other strings.

Fixnum#to_s! doesn't exit.

I stand corrected! In that case, "bottles_of_beer =
bottles_of_beer.to_s"
Either way, interpolation looks clearer and neater.

--
Posted via http://www.ruby-forum.com/\.

Michael Fellinger wrote:

Also, would there be an easy way to print the words out instead of the
numbers of bottles on the wall?

Divide the number by 10 and it will give the prefix (ninety-, eighty-,
etc.) You need a list or hash of some sort, of course. Simple
example...

prefixes = {9 => "ninety-", 8 => "eighty-"}

...or even better...

prefixes = ["", "", "twenty-", "thirty-", "forty-"]

...and so on. And then modify the numbers ten through nineteen after
the transformation (you don't want "one" for "eleven" do you) since
they differ in nomenclature from the others.

You could automagically use suffixes instead of prefixes if the
number, when divided by 10 is 1, but that wouldn't help you with the
edge cases of 'ten', 'eleven', and 'twelve'.

http://p.ramaze.net/1900
my stab at it

^ manveru

I did something slightly different, which works on multiple levels, not just two. For example, it can go all the way up to 999 without any extra code.

#!/usr/bin/env ruby

class Fixnum
   ENGLISH = {
     0 => 'zero',
     1 => 'one',
     2 => 'two',
     3 => 'three',
     4 => 'four',
     5 => 'five',
     6 => 'six',
     7 => 'seven',
     8 => 'eight',
     9 => 'nine',

     10 => 'ten',
     11 => 'eleven',
     12 => 'twelve',
     13 => 'thirteen',
     14 => 'fourteen',
     15 => 'fifteen',
     16 => 'sixteen',
     17 => 'seventeen',
     18 => 'eighteen',
     19 => 'ninteen',

     20 => 'twenty',
     30 => 'thirty',
     40 => 'forty',
     50 => 'fifty',
     60 => 'sixty',
     70 => 'seventy',
     80 => 'eighty',
     90 => 'ninety',

     100 => 'one hundred and',
     200 => 'two hundred and',
     300 => 'three hundred and',
     400 => 'four hundred and',
     500 => 'five hundred and',
     600 => 'six hundred and',
     700 => 'seven hundred and',
     800 => 'eight hundred and',
     900 => 'nine hundred and'
   }

   def to_english
     i = ENGLISH.keys.select{|n| n <= self}.max
     ENGLISH[i] + (i < self ? " " + (self-i).to_english : '')
   end
end

99.downto(1) do|i|
   puts "#{i.to_english} bottles of beer on the wall"
end

···

On Wed, Aug 27, 2008 at 9:57 AM, Todd Benson <caduceass@gmail.com> wrote:

On Tue, Aug 26, 2008 at 6:30 PM, danielj <sleepingindian@gmail.com> wrote:

--
Michael Morin
Guide to Ruby

Become an About.com Guide: beaguide.about.com
About.com is part of the New York Times Company

Who's thirsty?