Voting

Category

real language

Bookmarking

Del.icio.us Digg Diigo DZone Earthlink Google Kick.ie
Windows Live LookLater Ma.gnolia Reddit Rojo StumbleUpon Technorati

Language C#

(Concise, using C# 3.5 features)

Date:10/26/07
Author:Jeff Dietrich
URL:http://www.discordant.org
Comments:14
Info:n/a
Score: (3.01 in 81 votes)
/// A short and sweet C# 3.5 / LINQ implementation of 99 Bottles of Beer
/// Jeff Dietrich, jd@discordant.org - October 26, 2007

using System;
using System.Linq;
using System.Text;

namespace NinetyNineBottles
{
  class Beer
  {
    static void Main(string[] args)
    {
        StringBuilder beerLyric = new StringBuilder();
        string nl = System.Environment.NewLine;

        var beers =
            (from n in Enumerable.Range(0, 100)
             select new { 
               Say =  n == 0 ? "No more bottles" : 
                     (n == 1 ? "1 bottle" : n.ToString() + " bottles"),
               Next = n == 1 ? "no more bottles" : 
                     (n == 0 ? "99 bottles" : 
                     (n == 2 ? "1 bottle" : n.ToString() + " bottles")),
               Action = n == 0 ? "Go to the store and buy some more" : 
                                 "Take one down and pass it around"
             }).Reverse();

        foreach (var beer in beers)
        {
            beerLyric.AppendFormat("{0} of beer on the wall, {1} of beer.{2}",
                                    beer.Say, beer.Say.ToLower(), nl);
            beerLyric.AppendFormat("{0}, {1} of beer on the wall.{2}", 
                                    beer.Action, beer.Next, nl);
            beerLyric.AppendLine();
        }
        Console.WriteLine(beerLyric.ToString());
        Console.ReadLine();
    }
  }
}

Download Source | Write Comment

Alternative Versions

VersionAuthorDateCommentsRate
1Paul M. Parks04/20/059
C# Gratuitously FunctionalPaul Stancer04/14/091
corporate styleveteran corporate coder08/16/090
4Mark Hurley05/31/050
Uses LinqSudipta11/19/100
Shows new features of C# 2.0.Bradley Tetzlaff11/24/050
v3.0 Easy Functional RecursiveYelinna Pulliti Carrasco08/04/090

Comments

>>  Zain said on 01/27/08 21:51:58

Zain There are 16 errors in the program :o

>>  Marcelo said on 02/09/08 01:41:27

Marcelo Your programm starts at 98 should start at 99 =)

>>  Jeff Dietrich said on 03/06/08 20:02:51

Jeff Dietrich There are not 16 errors in the program - use .NET 3.5 *final*, build as a console app.

It doesn't start at 98 - the default command window doesn't have a buffer large enough to show 99. You'll see 98 first only if you don't adjust your buffer height beyond 300 rows.

>>  [ICR] said on 04/07/08 13:41:44

[ICR] The line:
(n == 2 ? "1 bottle" : n.ToString() + " bottles";)),
should read:
(n == 2 ? "1 bottle" : (n - 1).ToString() + " bottles";)),

It's also probably benificial to move the .Reverse() up to the Enumerable such that it reads:
(from n in Enumerable.Range(0, 100).Reverse()
as reversing the integers seems cheaper than reversing the objects. Though that's just intuition and I could be wrong.

>>  [ICR] said on 04/07/08 13:42:49

[ICR] Also, I tend to prefer using:
Console.ReadKey(true);
to
Console.ReadLine();

>>  DC said on 06/05/08 16:57:51

DC No one Cares what you prefer to use. Dumbass.

>>  Jack D said on 06/18/08 14:15:26

Jack D That is absolutely totally fantastic.
Cool proggy :)

>>  Krzysiuyo said on 06/23/08 17:13:59

Krzysiuyo StringBuilder beerLyric = new StringBuilder();

var beers =
(from n in Enumerable.Range(0, 100)
select new
{
Say = n == 0 ? "No more bottles" :
(n == 1 ? "1 bottle" : n.ToString() + " bottles";),
Next = n == 1 ? "no more bottles" :
(n == 0 ? "99 bottles" :
(n == 2 ? "1 bottle" : (n-1).ToString() + " bottles";)),
Action = n == 0 ? "Go to the store and buy some more" :
"Take one down and pass it around"
}).Reverse();

foreach (var beer in beers)
{
beerLyric.AppendFormat("{0} of beer on the wall.\n{1}, {2} of beer on the wall.\n\n",
beer.Say, beer.Action, beer.Next);
}

Console.WriteLine(beerLyric.ToString());
Console.ReadKey(true);

>>  anon said on 06/27/08 03:05:31

anon That wood B newlength << oldlength in C.

>>  Ely_bob said on 04/29/09 20:37:55

Ely_bob You use entirely too much code... not to mention memory...

should be:

using System;
using System.Text;

namespace NinetyNineBottles
{
class Beer
{
static void Main(string[] args)
{
for(int i = 99;i>-1;)
{
if(i>2)
{
Console.WriteLine(i.ToString()+" bottles of beer on the
wall, "i.ToString()+" bottles of beer.";);
i--;
Console.WriteLine("Take one down pass it arround."+i.ToString()+
" bottles of beer on the wall.);
}
else if(i==2)
{
Console.WriteLine(i.ToString()+" bottles of beer on the
wall, "i.ToString()+" bottles of beer.";);
i--;
Console.WriteLine("Take one down pass it arround."+i.ToString()+
" bottle of beer on the wall.);

}
else if(i==1)
{
Console.WriteLine(i.ToString()+" bottle of beer on the
wall, "i.ToString()+" bottle of beer.";);
i--;
Console.WriteLine("Take it down pass it arround. No more bottles of"
" beer on the wall.";);


}
else
{
Console.WriteLine(i.ToString()+" bottles of beer on the
wall, "i.ToString()+" bottles of beer.";);
i--;
Console.WriteLine("Go to the store and buy some more." );


}
}
}
}
}

1 class 1 var(Int16)... nothing fancy.

>>  Rabbit said on 04/30/09 09:25:25

Rabbit I love this lenguage!!!!!!!

>>  yeerk said on 05/01/09 02:12:41

yeerk Absolute genius, less code and more intuitive than I would have made it. Your distinction between the numbers and actions and then combining them again was a good idea, I probably would have just put it all together in a series of if statements inside of a for loop but what you did is much better.

>>  Alex Bevilacqua said on 05/22/09 03:28:45

Alex Bevilacqua Here ya go. This was done for mono, but it should be fine in VS ;)

using System;

namespace Bottles
{
class MainClass
{
public static void Main(string[] args)
{
for( int i = 99; i > 0; i--)
{
string bottles = i + " bottle" + ((i > 1) ? "s" : "";) + " of beer";
Console.WriteLine(bottles + " on the wall, " + bottles + ".";);
string oneLessBottle = ((i - 1 > 0) ? Convert.ToString(i - 1) : "no";) + " bottle" + ((i - 1 != 1) ? "s" : "";);
if( i > 0 )
Console.WriteLine("Take one down, pass it around, " + oneLessBottle + " of beer on the wall." );
}
Console.WriteLine("Go to the store and buy some more";);
}
}
}

>>  antonio said on 06/25/09 07:20:23

antonio Stop appending strings and use string.format. .NET is not very good appending strings.

Also, why not store the whole string in a MemoryStream and then write the whole thing to the buffer. StringBuilder is so 2004.

Download Source | Write Comment

Add Comment

Please provide a value for the fields Name, Comment and Security Code.
This is a gravatar-friendly website.
E-mail addresses will never be shown.
Enter your e-mail address to use your gravatar.

Please don't post large portions of code here! Use the form to submit new examples or updates instead!

Name:

eMail:

URL:

Security Code:
  
Comment: