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: | ![]() |
/// 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
Version | Author | Date | Comments | Rate |
---|---|---|---|---|
1 | Paul M. Parks | 04/20/05 | 9 | ![]() ![]() |
C# Gratuitously Functional | Paul Stancer | 04/14/09 | 1 | ![]() ![]() |
corporate style | veteran corporate coder | 08/16/09 | 0 | ![]() ![]() |
4 | Mark Hurley | 05/31/05 | 0 | ![]() ![]() |
Uses Linq | Sudipta | 11/19/10 | 0 | ![]() ![]() |
Shows new features of C# 2.0. | Bradley Tetzlaff | 11/24/05 | 0 | ![]() ![]() |
v3.0 Easy Functional Recursive | Yelinna Pulliti Carrasco | 08/04/09 | 0 | ![]() ![]() |
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!
Comments
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.
(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.
Console.ReadKey(true);
to
Console.ReadLine();
Cool proggy
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);
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.
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" : ""
Console.WriteLine(bottles + " on the wall, " + bottles + "."
string oneLessBottle = ((i - 1 > 0) ? Convert.ToString(i - 1) : "no"
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"
}
}
}
Also, why not store the whole string in a MemoryStream and then write the whole thing to the buffer. StringBuilder is so 2004.