LebGeeks

A community for technology geeks in Lebanon.

You are not logged in.

#1 August 29 2013

Joe
Member

[Exercise] Happy Birthday golf

Let's have some code golf fun. The goal is to write a program that outputs exactly the following text using as little characters as possible:

Happy Birthday to you,
Happy Birthday to you,
Happy Birthday dear name!
Happy Birthday to you.

Every language is accepted, although the use of Java is heavily discouraged for obvious reasons.
Have fun :)

Offline

#2 August 29 2013

rtp
Member

Re: [Exercise] Happy Birthday golf

        static void Main(string[] args)
        {
            string hb = "Happy Birthday ";
            for (int i = 0; i < 4; i++)
            {
                if (i == 2)
                {
                    cw(hb+ "dear name!");
                    continue;
                }
                if (i == 3)
                {
                    cw(hb + "to you.");
                    break;
                }
                cw(hb+"to you,");
            }

            Console.ReadLine();
        }


        public static void cw(string s)
        {
            Console.WriteLine(s);
        }

Offline

#3 August 29 2013

Joe
Member

Re: [Exercise] Happy Birthday golf

rtp: According to my wc that's 601characters. I think you're going to need much better than this to compete in code golf ;)

Offline

#4 August 29 2013

mesa177
Member

Re: [Exercise] Happy Birthday golf

Matlab/Octave (I figured that the simplest way is the shortest):

fprintf('Happy Birthday to you,\nHappy Birthday to you,\nHappy Birthday dear name!\nHappy Birthday to you.\n')

Offline

#5 August 29 2013

rolf
Member

Re: [Exercise] Happy Birthday golf

Happy birthday who?

Offline

#6 August 29 2013

rtp
Member

Re: [Exercise] Happy Birthday golf

rahmu wrote:

rtp: According to my wc that's 601characters. I think you're going to need much better than this to compete in code golf ;)

lol, lets just say this is the first strike

Offline

#7 August 29 2013

ILIA_93
Member

Re: [Exercise] Happy Birthday golf

C Language:

#1 The native C code, runs on all compilers, without colored text.

main(){int i;for(i=0;i<4;i++){printf("Happy Birthday");i!=3?i==2?puts(" dear name!"):puts(" to you,"):puts(" to you.");}}

#2 This one is 100% accurate, tested on Pelles C. (Define compatibility names enabled in compiler settings)

main(){int i;for(i=0;i<4;i++){textcolor(3);printf("Happy Birthday");textcolor(7);i!=3?i==2?puts(" dear name!"):puts(" to you,"):puts(" to you.");}}

Offline

#8 August 29 2013

m0ei
Member

Re: [Exercise] Happy Birthday golf

x = 'Happy Birthday '
print x + x.join('dear name!\n' if i==2 else 'to you,\n' for i in range(4))[:-2] + '.'

Number of characters: 108

Edit:
Without spaces

x='Happy Birthday '
print x+x.join('dear name!\n'if i==2 else'to you,\n'for i in range(4))[:-2]+'.'

Number of characters: 99

Last edited by m0ei (August 29 2013)

Offline

#9 August 29 2013

Joe
Member

Re: [Exercise] Happy Birthday golf

Here's my best one so far at 97 characters of Python code:

print "\n".join(map("".join,zip(['Happy Birthday ']*4,['to you,']*2+['dear name!']+['to you.'])))

I think I can go even lower by avoiding "to you" be repeated twice.

@mesa177: I wouldn't have posted the exercise if I didn't know how to beat your solution...

@ILIA_93: Good catch for the colored text, but in the interest of sanity, we can ignore the coloration of the text (which, quite frankly, is a problem with the formatting software of this forum and not at all intentional).

Offline

#10 August 30 2013

scorz
Member

Re: [Exercise] Happy Birthday golf

Put the text to print in a separated file. Let " f " be this file.

Using perl 48 chars:

open(FILE,"f");while(<FILE>){print$_;}closeFILE; 

Using Bash.. well 4 chars:

 cat f 

Cheating :) ?

Offline

#11 August 30 2013

Zef
Member

Re: [Exercise] Happy Birthday golf

Here's my shortest attempt in Ruby at 72 characters.

s="Happy Birthday to you,\n";puts s+s+s[0,15]+"dear name!\n"+s[0,21]+'.'

And some other ideas that are longer.

s="Happy Birthday to you,";[s,s,s[0,15]+"dear name!",s.chop+'.'].each{|l|puts l}
s="Happy Birthday to you,";puts [s,s,s[0,15]+"dear name!",s.chop+'.'].join("\n")
s="Happy Birthday to you,\n";puts s+s+s[0,15]+"dear name!\n"+s.chop.chop+'.'
s="Happy Birthday to you,\n";puts s+s+s[0,15]+"dear name!\n"+s.sub(',','.')

Fun stuff!

Offline

#12 August 30 2013

Tempto
Member

Re: [Exercise] Happy Birthday golf

C++

#include <iostream>,<string>
using namespace std;int main(){string x="Happy Birthday",z=" dear name!",y=" to you";for(int i=0;i<2;++i)cout<<x+y+','<<endl;cout<<x+z<<endl<<x+y+'.';}

Didn't spend much on it and it kind of shows, 181 (or 179, if you disregard the obligatory new line) characters.

Offline

#13 August 30 2013

geek
Member

Re: [Exercise] Happy Birthday golf

Print@Uncompress@"1:eJxTTMoPCs5iYGDwSCwoqFRwyiwqyUhJrFQoyVeozC/ViTEwNCJFJiU1sUghLzE3VRGnTj0AnLYm2g=="

Offline

#14 August 31 2013

samer
Admin

Re: [Exercise] Happy Birthday golf

Very close to zef's solution.

Javascript / 92 characters:

h="Happy Birthday to you,\n";console.log(h+h+h.slice(0,15)+"dear name!\n"+h.slice(0,21)+'.')

Offline

#15 August 31 2013

arithma
Member

Re: [Exercise] Happy Birthday golf

There's always this too:

s="Happy Birthday to you";print"%s,\n%s,\n%s dear name!\n%s."%(s,s,s,s)

Offline

#16 August 31 2013

jsaade
Member

Re: [Exercise] Happy Birthday golf

php:

echo gzinflate(base64_decode("80gsKKhUcMosKslISaxUKMlXqMwv1eHyIEo4JTWxSCEvMTdVEbsGPS4A"));

@arithma nice simple and clean solution

Offline

#17 August 31 2013

m0ei
Member

Re: [Exercise] Happy Birthday golf

@arithma your solution is incorrect.

%s dear name!

Output:

Happy Birthday to you dear name!

Should be:

s="Happy Birthday to you";print"%s,\n%s,\n%s dear name!\n%s."%(s,s,s[:14],s)

Offline

#18 August 31 2013

arithma
Member

Re: [Exercise] Happy Birthday golf

@m0ei: Damn that , good find.

Offline

#19 August 31 2013

arithma
Member

Re: [Exercise] Happy Birthday golf

Making up for my last hickup (70 chars):

s='Happy Birthday to you,\n'*4;print s[:61]+"dear name!"+s[68:90]+'.'

Last edited by arithma (August 31 2013)

Offline

#20 October 25 2013

raja
Member

Re: [Exercise] Happy Birthday golf

@Arithma:

You can shorten that by one more character. By moving the newlines around and using one fewer character in array indices:

s='\nHappy Birthday to you,'*4;print s[1:62]+"dear name!"+s[:22]+'.'

Last edited by raja (October 25 2013)

Offline

#21 October 29 2013

m.sabra
Member

Re: [Exercise] Happy Birthday golf

a='Happy Birtday'
b='to you'
print(a,b,',\n',a,b,',\n',a,'dear name!\n',a,b,'.')

81 char python

Offline

#22 November 4 2013

NuclearVision
Member

Re: [Exercise] Happy Birthday golf

a="happy birthday"
b=" to you,\n"
print a+b+a+b+a+" dear name!\n"+a+b[:7]+"."

Last edited by NuclearVision (November 4 2013)

Offline

#23 November 4 2013

m0ei
Member

Re: [Exercise] Happy Birthday golf

69 characters so far.

a='to you,';print"Happy Birthday %s\n"*4%(a,a,'dear name!','to you.')

Last edited by m0ei (November 4 2013)

Offline

#24 November 4 2013

NuclearVision
Member

Re: [Exercise] Happy Birthday golf

m0ei wrote:

69 characters so far.

a='to you,';print"Happy Birthday %s\n"*4%(a,a,'dear name!','to you.')

Nice, I was thinking of similar stuff but i could not get the *4 to work...

Offline

#25 November 4 2013

arithma
Member

Re: [Exercise] Happy Birthday golf

@raja: Your answer inserts an extra newline at the beginning. ---Edit My bad it doesn't.
@m0ei: Your answer inserts an extra newline at the end.

Last edited by arithma (November 4 2013)

Offline

Board footer