• Advertisement
Sign in to follow this  

99 Bottles Of Beer Challenge With Least Amount Of Characters ?

This topic is 1074 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


I have no idea how to reduce it more without cutting corners in output format...

I guess verbose languages like C# and Java pretty much screwed by the criteria themselves.

 

The variable declarations cost you 20 characters, and the length of Console.WriteLine and ToString cost even more.

Share this post


Link to post
Share on other sites
Advertisement

I have no idea how to reduce it more without cutting corners in output format...

One common way of doing ToString() in "shorthand" is just doing +"" (in both C# and Java), which will convert the object to a string using ToString().

You could also save more by doing Console.Write() (and adding '\n' to the end) instead of Console.WriteLine(). Also, no need to use g. And some parens aren't needed. Here's a smaller version (296 non-whitespace characters):
 
string
b = " bottles of beer",
c = b.Remove(7, 1),
w = " on the wall",
t = "Take one down and pass it around, ",
n = "o more", x;
for (int a = 99; a>0;)
{
    x = a > 2 ? b : c;
    Console.WriteLine(a + x + w + ", " + a + x + (a-- > 1 ? ".\n" + t + a + (a > 1 ? b : c) + w + ".\n" : "."));
}
Console.Write(t + "n" + n + b + w + ".\n\nN" + n + b + w + ", n" + n + b + ".\nGo to the store and buy some more, 99" + b + w + ".\n");
output

I still think we should be counting whitespace, because it's actually significant. Try running a program with no whitespace... Also, I think C# and Java should include their using/import and class definitions. Edited by Cornstalks

Share this post


Link to post
Share on other sites

Wow, thanks Cornstalks for showing me those tricks (mainly string with comma, which I didn't know) :D

Share this post


Link to post
Share on other sites

  Closest I can get using "C" and no sleep!
 

#include <stdio.h>


void r(int i)
{
  if(i <= 0)
  {
    printf("No more bottles of beer");
    return;
  }
  printf("%d bottles of beer on the wall, %d bottles of beer\nTake one down and pass it around, %d bottles of beer on the wall\n\n", i, i, i);
  r(i - 1);
}


void main()
{
  r(99);
}
Edited by ByteTroll

Share this post


Link to post
Share on other sites

Closest I can get using "C" and no sleep!

*snip*

That doesn't print all the right words. The last verse is wrong (you're not going to the store to buy some more), and when you have 1 bottle left you still say bottles. And when you have 1 bottle of beer on the wall and you take it down, you mysteriously still have 1 bottle of beer on the wall. You're also missing periods.

Do \n etc. Count as one character?

Nope, they count as two.

Share this post


Link to post
Share on other sites

 

Closest I can get using "C" and no sleep!

*snip*

That doesn't print all the right words. The last verse is wrong (you're not going to the store to buy some more), and when you have 1 bottle left you still say bottles. And when you have 1 bottle of beer on the wall and you take it down, you mysteriously still have 1 bottle of beer on the wall. You're also missing periods.

Do \n etc. Count as one character?

Nope, they count as two.

 

 

Ah snap! I didn't even notice that ;P  I shall fix it.

Share this post


Link to post
Share on other sites

 

 

Also, I think C# and Java should include their using/import and class definitions.

 

I did, they add ~20-30 characters to your character count.

 

 

Actually, with the Roslyn shell you no longer need the surrounding class / Main function cruft, just as if you were using the python shell. Granted, you should still include the using statements, since they're required.

 

Also, I see your 300+ C# entry and raise you 250 (even beats some of the other languages)  biggrin.png

string f="{0:#;;",e=".\n",o="s;s;''} of beer",w=" on the wall",n="o more} bottle",x=n+"{1:"+o;
for(int a=99;a>=0;)System.Console.WriteLine(f+'N'+x+w+", "+f+'n'+x+e+
"{1:Take one down and pass it around;Go to the store and buy some more}, {1:#;99;n"+n+"{2:"+o+w+e,a,a-1,--a-1);

It doesn't require the conditional operator either, which is nifty.

 

impressive, i wrapped it up to be compilable with ideone: http://ideone.com/MX82jD adding the extra fluff for ideone's compiler bumps it up to 288, but still, quite impressive mate.

Share this post


Link to post
Share on other sites

Also, I see your 300+ C# entry and raise you 250 (even beats some of the other languages)  biggrin.png

string f="{0:#;;",e=".\n",o="s;s;''} of beer",w=" on the wall",n="o more} bottle",x=n+"{1:"+o;
for(int a=99;a>=0;)System.Console.WriteLine(f+'N'+x+w+", "+f+'n'+x+e+
"{1:Take one down and pass it around;Go to the store and buy some more}, {1:#;99;n"+n+"{2:"+o+w+e,a,a-1,--a-1);
It doesn't require the conditional operator either, which is nifty.

That is a thing of beauty.

Share this post


Link to post
Share on other sites

Also, I see your 300+ C# entry and raise you 250 (even beats some of the other languages)  biggrin.png

string f="{0:#;;",e=".\n",o="s;s;''} of beer",w=" on the wall",n="o more} bottle",x=n+"{1:"+o;
for(int a=99;a>=0;)System.Console.WriteLine(f+'N'+x+w+", "+f+'n'+x+e+
"{1:Take one down and pass it around;Go to the store and buy some more}, {1:#;99;n"+n+"{2:"+o+w+e,a,a-1,--a-1);

It doesn't require the conditional operator either, which is nifty.

You, sir, just blew my mind

Mind-Blown.jpg

 

Edit: this thread needs to be somewhere where I can upvote... I'm learning all sorts of tricks in this wide variety of languages.

Edited by Cornstalks

Share this post


Link to post
Share on other sites

 

Also, I see your 300+ C# entry and raise you 250 (even beats some of the other languages)  biggrin.png
string f="{0:#;;",e=".\n",o="s;s;''} of beer",w=" on the wall",n="o more} bottle",x=n+"{1:"+o;
for(int a=99;a>=0;)System.Console.WriteLine(f+'N'+x+w+", "+f+'n'+x+e+
"{1:Take one down and pass it around;Go to the store and buy some more}, {1:#;99;n"+n+"{2:"+o+w+e,a,a-1,--a-1);

It doesn't require the conditional operator either, which is nifty.

 

 

What? Wait... what? Can somebody translate that for me, because some things I didn't see yet in C# (or I saw but didn't have the need to use hence forgot it).

Share this post


Link to post
Share on other sites


What? Wait... what? Can somebody translate that for me, because some things I didn't see yet in C# (or I saw but didn't have the need to use hence forgot it).

It's mostly some very clever applications of string formatting.

 

That said, Mike clearly knows way more fun facts about string formatting than I do :)

Share this post


Link to post
Share on other sites

 


What? Wait... what? Can somebody translate that for me, because some things I didn't see yet in C# (or I saw but didn't have the need to use hence forgot it).

It's mostly some very clever applications of string formatting.

 

That said, Mike clearly knows way more fun facts about string formatting than I do smile.png

 

 

Oh, the things you learn by trawling through the MSDN documentation at random :)   So many bits of the BCL are completely unknown to most people.

Share this post


Link to post
Share on other sites

Thanks for thorough explanation. I'm still stunned at that "black magic", and I believe I won't ever have to use that kind of formatting XD

Share this post


Link to post
Share on other sites

It's actually pretty common in business applications. Sometimes people want negatives done with parenthesis around them, or instead of displaying a 0, a single dash. For rich text formatting it's also common to also specify color as part of these, so you can have something like negatives also be red. 

Share this post


Link to post
Share on other sites
n=:<'no more'
a=:(n,~<1)0}(>:(;"0)+)i.99
b=:<'bottles of beer'
o=:<'on the wall'
t=:<'take one down, pass it around'
c=:(99#b),"0~{."1 a
(|.c,"1 o,"1 c,"1 t,"1(}."1 a),"1 b,o),o,~b,~n,b,o,n,b,'go to the store and buy some more';99
 
201 non-whitespace, 230 whitespace characters in J. I could probably get it shorter but I've already forgotten how it works.

Share this post


Link to post
Share on other sites

I've got a Python3 solution of 160 non-whitespace characters or 162 if you count whitespace:

 

[source]import urllib.request,re

print(re.sub('<br/?> ?','\n',re.sub('</p>\n?<p>','\n\n',urllib.request.urlopen('http://goo.gl/PEeK').read().decode('utf-8')[3592:-1028])))[/source]

 

Requires an internet connection wink.png

Share this post


Link to post
Share on other sites
This was my best attempt, without looking at any of the others: 232/261* characters in Ruby
 
def b(i,c)"#{i==0? "#{c==1? ?N:?n}o more":i} bottle#{i==1?'':?s} of beer"end
def w(i,c)b(i,c)+" on the wall"end
99.downto(0){|i|puts "#{w(i,1)}, #{b(i,0)}.\n#{i>0? "Take one down and pass it around, #{w(i-1,0)}":"Go to the store and buy some more, #{w(99,0)}"}."}
It might be a mild case of delirium, but I think I can still almost read it!

[edit: * 261 total characters, the "non whitespace" site tells me 232] Edited by rip-off

Share this post


Link to post
Share on other sites

You blew my 250 non white space code right out of the water ( Ruby ) !!

Right now I am seeing if I can abuse Python 2.7 into submission, but I see it's going to be tough to get under 218

Share this post


Link to post
Share on other sites

This topic is 1074 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Guest
This topic is now closed to further replies.
Sign in to follow this  

  • Advertisement