Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Dilbert is awesome
source: Dilbert, September 8, 1992

I'm hoping to add a new twist on the classic "Hello World!" program.

Code a program that outputs Hello World! without:

  1. String/Charachter literals
  2. Numbers (any base)
  3. pre-built functions that return "Hello World!"
  4. RegEx literals

With the exceptions of "O" and 0.

†"O" is capitalized, "o" is not acceptable.

share|improve this question
3  
One of [code-golf] and [code-challenge] please, not both. The point of these tags to to help people find questions with the rules they want to use. Essentially every question on this site should be a game of some kind or another. –  dmckee Mar 11 '11 at 22:29
1  
-1 We've already had Obfuscated Hello World, and I think this challenge is too similar. I'd have cast a "close as duplicate" vote, if I weren't a mod. –  Chris Jester-Young Mar 11 '11 at 22:35
2  
@zzzzBov: I don't think it's different enough to warrant another question in the "hello world" theme; a different theme would have been better. But, that's just my opinion. –  Chris Jester-Young Mar 11 '11 at 23:39
1  
I think this is a fine code golf - and better than the prior one. –  MtnViewMark Mar 12 '11 at 6:58
2  
Some people seem to assume that "O"* means they can have a string literal with any number of O’s, including zero. I don’t think that was the intention. Please clarify. –  Timwi Mar 12 '11 at 21:12

71 Answers 71

up vote 45 down vote accepted

Windows PowerShell, way too much

Yes, indeed, back in the day we had to write a »Hello world« using (almost exclusively) zeroes ...

&{-join($args|%{[char]$_.Length})} `
O00000000000000000000000000000000000000000000000000000000000000000000000 `
O0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 `
O00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 `
O00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 `
O00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 `
O0000000000000000000000000000000 `
O00000000000000000000000000000000000000000000000000000000000000000000000000000000000000 `
O00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 `
O00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 `
O00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 `
O000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 `
O00000000000000000000000000000000

On a more serious note:

Windows PowerShell, 25

Write-Host Hello World!

No string literal. The Hello World! in there just happens to be parsed as a string since PowerShell is in command parsing mode there.

share|improve this answer
2  
How is that real?? O_o –  Josh Mar 11 '11 at 22:06
6  
+1 for so many zeros –  zzzzBov Mar 12 '11 at 2:07
1  
I searched the internet for some examples or history of this. This is so weird. Could you provide any links? –  Caleb Jares Dec 5 '11 at 5:44

C, 327 chars

#define O(O)-~O
#define OO(o)O(O(o))
#define Oo(o)OO(OO(o))
#define oO(o)Oo(Oo(o))
#define oo(o)oO(oO(o))
#define O0 putchar
main() {
    O0(OO(oO(!O0(~O(Oo(OO(-O0(~O(Oo(-O0(O(OO(O0(oo(oO(O0(O(oo(oO(OO(Oo(oo(oO(
    O0(oo(oo(!O0(O(OO(O0(O0(O(OO(Oo(O0(O(Oo(oo(oO(O0(oo(oo(oO(oo(oo(0))))))))
    ))))))))))))))))))))))))))))))))))))));
}

Strangely, it does't lose its beauty after preprocessing:

main() {
putchar(-~-~-~-~-~-~-~-~-~-~!putchar(~-~-~-~-~-~-~-~-putchar(~-~-~-~-~-~-
putchar(-~-~-~putchar(-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~putchar(
-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~putchar(-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
-~-~-~-~-~-~-~-~-~-~-~-~-~!putchar(-~-~-~putchar(putchar(-~-~-~-~-~-~-~putchar
(-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~putchar(-~-~-~-~-~-
~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~0))))))))))));
}
share|improve this answer
1  
Awesome. Missing the !, though. –  Mark Reed Nov 21 '13 at 15:07
    
This is real art! –  Bill Askaga Feb 7 at 19:42

BrainFuck, 102 111 characters

++++++++[>++++[>++>+++>+++>+<<<<-]>+>->+>>+[<]<-]>>.>>---.+++++++..+++.>.<<-.>.+++.------.--------.>+.

Meets all of the rules.

Credit goes to Daniel Cristofani.

share|improve this answer
1  
I can't decide if the rules should also state 0 and/or "O" must be used. It's a little mean to BrainFuck and golfscript, but they don't exactly have a hard time with this challenge. –  zzzzBov Mar 11 '11 at 21:32
22  
@zzzzBov: If they must be used, then Brainfuck code will just include them. They don't affect the program at all, though. –  Joey Mar 11 '11 at 21:47

C - 182

#define decode(c,o,d,e,g,O,l,f) e##c##d##o
#define HelloWorld decode(a,n,i,m,a,t,e,d)
#define Puzzles(flog) #flog
#define CodeGolf Puzzles(Hello World!)
HelloWorld(){puts(CodeGolf);}
share|improve this answer
1  
+1,Lol,You still using the token pasting operator trick! Nice mate :-) –  Quixotic Mar 12 '11 at 7:52

Haskell - 143 characters

o%y=o.o.o$y;o&y=(o%)%y;o!y=o$o$(o%)&y
r=succ;w=pred;a=r%y;e=r$w&l;l=r!'O';o=r e;u=(w&)&a;y=r%l
main=putStrLn[w!o,o,l,l,y,w u,w$r&'O',y,a,l,e,u]

oy, that was woolly!

No numbers, no numeric operations, variables renamed for amusement.

Some exposition might be nice:

  • o%y, o&y, and o!y each applies the function o to y multiple times: 3, 9, and 29 times respectively. 29?!?! Yes, 29!
  • r and w are next and previous character, which when applied using the above higher- order functions can be made to get all the characters needed from 'O'.

The sequence of jumps needed is:

'O' +29    -> 'l'
'O'  +9 -1 -> 'W'
'l'  -9 +1 -> 'd'
'l'  +3    -> 'o'
'd'  +1    -> 'e'
'o'  +3    -> 'r'
'e' -29    -> 'H'
'r' -81    -> '!'
'!'  -1    -> ' '

  • Edit: (134 -> 144) Forgot to output an exclamation point, sigh....
  • Edit: (144 -> 143) Removed a unnecessary $, renamed # to ! for Hugs.
share|improve this answer
    
o rly? o.o .... –  Joey Adams Mar 12 '11 at 7:12
    
It doesn't work. codepad.org/lyKyj1Ox –  nyuszika7h Mar 13 '11 at 15:31
    
@Nyuszika7H That's because this service turns on some compiler options by default. It should compile with plain GHC. –  FUZxxl Mar 13 '11 at 16:10
    
@Nyuszika7H It works just fine with GHC. The problem is that that service is using Hugs from 2006. There appear to be two things that Hugs can't deal with: 1) Using '#' as an operator. Changing to '!' makes it work. 2) The definitions of r=succ and w=pred run afoul of how Hugs implements the monomorphism restriction. Changing to r x=succ x and w x=pred x makes it work (at the cost of 4 characters). These seem to be problems in Hugs. –  MtnViewMark Mar 13 '11 at 18:09
    
Is it still valid after the @Community edit? –  hosch250 Feb 21 at 23:58

C program - 45

(cheating)

Lexically, this doesn't use any string literals or regex literals. It takes advantage of the stringification feature of the C preprocessor. s(x) is a macro that turns its argument into a string.

#define s(x)#x
main(){puts(s(Hello World!));}
share|improve this answer
2  
For a very convenient notion of "lexically" :) But cheekiness FTW! –  J B Mar 12 '11 at 22:39

Mathematica 12 chars

Only symbols, no strings.

Hello World!   

The ! is a factorial operator, but as the symbols Hello and World are undefined, returns the input unchanged.

If we modify the program a bit:

Hello=2;
World=3;
Hello World!  

Then it prints 12 (2 * 3!)

share|improve this answer

i386 assembly (Linux, gcc syntax), 440 442 435

Today's my assembly day, and after that I'll have had enough for a while. I allowed myself number 128, see below program for discussion of why. Nothing extraordinary: I'm just encoding "Hello World!" as assembly opcodes where that made sense without numeric constants, and filled in the rest with arithmetic.

#define M mov
M $0,%ecx;inc %cx;M %ecx,%ebx;inc %cx;M %ecx,%eax;add %ax,%ax
M %ecx,%edx;shl %cl,%dx;M (e),%ch;add %dl,%ch;dec %ch;M %ch,(l)
M %ch,(j);M %ch,(z);M $0,%ch;shl %cl,%edx;M %dl,(s);inc %dl
M %dl,(b);M (o),%dl;M %dl,(u);add %al,%dl;dec %dl;M %dl,(r)
M $m,%ecx;M $n,%edx;int $c;M %ebx,%eax;M $0,%ebx;int $c
.data
m:dec %eax;e:gs;l:es;j:es;o:outsl (%esi),(%dx)
s:es;push %edi;u:es;r:es;z:es;fs;b:es;n=.-m
t=(n+n)/n;c=t<<(t*t+t)

(assemble with gcc -nostartfiles hello.S -o hello, possibly -m32 depending on your arch)

Why the tolerance for 128? I need syscalls to actually show anything; Linux syscalls are on INT 80h (128 decimal); the only operand format for INT is immediate, so it's not possible to have anything else than a constant (to the code) there. I could (after I get sober) attempt to express it as a function of other symbolic constants in the code, likely n, but that's getting very boring for not much gain. I read the constraint on numbers as a way to prevent ASCII coding, and that's definitely not what I'm doing here, so I feel innocent enough to submit this. (FWIW, I also tried self-modifying code, but that segfaulted) There's now no 128 left either. The code's pure!

  • Edit1 reformatted to save lines; removed a numeric 1 (nobody noticed?!)
  • Edit2 compressed mov with CPP macros; eliminated the remaining 128.
share|improve this answer
3  
This is how real men program. –  Nit Mar 4 at 17:30

Javascript - 305

A bit long but I like the method used.

O=0;O++;O=O.toString();alert([O+0+0+O+0+0+0,0+O+O+0+0+O+0+O,O+O+0+O+O+0+0,O+O+0+O+O+0+0,O+O+0+O+O+O+O,O+0+0+0+0+0,O+0+O+0+O+O+O,O+O+0+O+O+O+O, O+O+O+0+0+O+0,O+O+0+O+O+0+0,O+O+0+0+O+0+0,O+0+0+0+0+O].map(function(e){O=0;O++;O++;return String.fromCharCode(parseInt(e,O))}).reduce(function (a,b){return a+b}))
share|improve this answer
    
I like it. It would be better with function parameters as OO, OOO, but of course that would make it longer. –  zzzzBov Mar 12 '11 at 19:31
    
Really nice. Does the ',new String()' need to be there at the end? It seems to work without it. –  Stephen Perelson Mar 12 '11 at 23:32
2  
Ah, no it doesn't. Sometimes I forget just how weakly typed Javascript is. –  david4dev Mar 12 '11 at 23:38
    
You don't need left 0 bits ('0+') either, and the space and the exclamation have two left 0 bits. Total reduction of 28 characters. –  Stephen Perelson Mar 12 '11 at 23:50
    
+1 for the original MapReduce use. –  Kwaio Jan 3 at 13:51

Python (126 130)

O=ord("O")
N=O/O
T=N+N
R=N+T
E=T**R
E<<T
print'O'[0].join(chr(c+O)for c in[N-E,E*R-T,_-R,_-R,_,N-_-E-E,E,_,_+R,_-R,E*R-R,T-_-E-E])
share|improve this answer
    
The literal '' is not allowed. –  romkyns Mar 12 '11 at 21:12
1  
@romkyns: Very true, fixed. –  Hoa Long Tam Mar 13 '11 at 0:40
1  
Not fixed (wrong output). You are probably thinking of [:0]. –  WolframH Nov 22 '13 at 15:39

GolfScript, 63 chars

[0))):O.)?O.*-.O.?+)).O.*+((..O+.O(.O+?.O-O*@.O+O.+$.O.*-).O/]+

What, no GolfScript entry yet?

This one uses a single numeric literal 0 and a variable named O (which is used to store the number 3). Everything else is arithmetic and stack manipulation. The string Hello World! is built up from its ASCII codes, character by character.

Here's how it works:

[             # insert start-of-array marker
  0))):O      # increment 0 thrice to get 3, and save it in the variable O
  .)?O.*-     # calculate 3^(3+1) - 3*3 = 81 - 9 = 72 = "H" 
  .O.?+))     # calculate 72 + 3^3 + 1 + 1 = 72 + 27 + 2 = 101 = "e"
  .O.*+((     # calculate 101 + 3*3 - 1 - 1 = 101 + 9 - 2 = 108 = "l"
  .           # ...and duplicate it for another "l"
  .O+         # calculate 108 + 3 = 111 = "o"
  .           # ...and duplicate it for later use
  O(.O+?      # calculate (3-1)^(3-1+3) = 2^5 = 32 = " "
  .O-O*       # calculate (32 - 3) * 3 = 29 * 3 = 87 = "W"
  @           # pull the second 111 = "o" to the top of the stack
  .O+         # calculate 111 + 3 = 114 = "r"
  O.+$        # copy the (3+3 = 6)th last element on the stack, 108 = "l", to top
  .O.*-)      # calculate 108 - (3*3) + 1 = 108 - 9 + 1 = 100 = "d"
  .O/         # calculate int(100 / 3) = 33 = "!"
]             # collect everything after the [ into an array
+             # stringify the array by appending it to the input string
share|improve this answer

C# (131 chars)

141 chars 142 chars

enum X{Hello,World,A,B=A<<A<<A}class Y{static void Main(){var c=(char)X.B;System.Console.Write(X.Hello.ToString()+c+++X.World+c);}}

Readable:

// Define some constants (B = 32)
enum X { Hello, World, A, B = A << A << A }
class Y
{
    static void Main()
    {
        // Generate the space (character #32)
        var c = (char) X.B;

        // Remember that “!” is character #33
        System.Console.Write(X.Hello.ToString() + c++ + X.World + c);
    }
}
share|improve this answer
1  
this is twisted and nice. I love it. –  jcolebrand Mar 14 '11 at 16:33
    
+1 nice way to generate certain numbers. Gotta remember that! –  Igby Largeman Sep 19 '13 at 6:03
    
LINQPad program, 102 chars: enum X{Hello,World,A,B=A<<A<<A}void Main(){var c=(char)X.B;(X.Hello.ToString()+c+++X.World+c).Dump();} –  Cœur Jul 8 at 16:10
    
.ToString() -> +"" saves some cahrs –  Firo Jul 10 at 10:18
    
@Firo: ... and breaks the rules. (No string literals allowed.) –  Timwi Jul 10 at 13:55

C++, 141, 146

First time trying one of these, can probably be improved quite a bit yet:

char o='O'/'O',T=o+o,X=T+o,f=T+X,t=f+f,F=t*f,h=F+F,l=h+t-T,O=l+X;
char c[]={F+t+t+T,h+o,l,l,O,o<<f,h-t-X,O,l+f+o,l,h,0};
cout<<c;

EDIT:

Stole the divide trick from another post, can't believe I didn't think of that :(

share|improve this answer
    
You're technically supposed to include the entire working program in the character count, including things like main, any included libraries, std::, etc etc –  Wug Nov 9 '12 at 22:01

Lua 144 97 86 chars

A different approach, based on the fact that table keys are also strings, and the fact that #Hello == #World == 32 == string.byte'\n'

e=#"O"t=e+e for k,v in pairs{Hello=0,World=e}do T=t^#k io.write(k,string.char(T+v))end

145 char solution

  • no strings except "O" or 0
  • no Regexes
  • no pre-built functions

Did delta encoding of the bytes, then some primenumbers etc etc :)

Golfed version:

e=#"O"d=e+e t=d+e v=d+t z=v+t T=t^d*d^t n=0 for l,m in pairs{T,T/t-z,z,0,d,-T-z,z*z+t*d,T/d,d,-d*t,-t^d,-T+v}do n=n+m io.write(string.char(n))end

Commented:

-- without numbers, strings, regex
-- except "O" and 0
e=#"0"
t=e+e --2
d=t+e -- 3
v=d+t -- 5
z=v+t -- 7
n=0
T=t^d*d^t -- 72 = 2^3+3^2
for l,m in pairs{T, --72
T/t-z, -- 29 = 72/2-7
z, --7
0, -- 0
d, -- 3
-T-z, -- -79 = -72 - 7
z*z+t*d, -- 55 = 7*7 + 2*3
T/d, -- 24 = 72/3
d, -- 3
-d*t, -- -6
-t^d, -- -8
-T+v -- -67 = -72+5
} do
    n=n+q[k]
    io.write(string.char(n))
end

Edit: Changed multiple O strings, and found some more optimalisations.

share|improve this answer
    
"O"* - I think the * was for the footnote, not "any number of O's –  romkyns Mar 12 '11 at 21:13
    
Well, regex'ing is what I naturally do :p. Could replace it at the cost of 5 or 3 extra characters for respectively the first and second solution –  jpjacobs Mar 12 '11 at 21:33
    
But anyway, I'll edit them out, thanks for the heads up –  jpjacobs Mar 12 '11 at 21:45

Scala (357 423 361 characters)

Not the shortest answer, unfortunately, but hoping to get bonus marks for the most use of 'O' and '0'

def h(){type t=scala.Char;def OO(c:t)={(c-('O'/'O')).toChar};def O(c:t)={OO(OO(OO(c)))};def O00(c:t)={(c+('O'/'O')).toChar};def O0(c:t)={O00(O00(O00(c)))};val l=O('O');val z=O0(O0(O0(O0(O0(O0(O0(O0(O0(O0(O0(0)))))))))));print(OO(O(O('O'))).toString+(OO(O(O(O('O')))).toString+l+l+'O'+OO(z)+O0(O0(O0(OO('O'))))+'O'+O0('O')+l+OO(OO(O(O(O('O')))))+z).toLowerCase)}

Previously:

def h(){type t=scala.Char;print(OO(O(O('O'))).toString+(OO(O(O(O('O')))).toString+O('O')+O('O')+'O'+OO(O0(O0(O0(O0(O0(O0(O0(O0(O0(O0(O0(0))))))))))))+O0(O0(O0(OO('O'))))+'O'+O0('O')+O('O')+OO(OO(O(O(O('O')))))+O0(O0(O0(O0(O0(O0(O0(O0(O0(O0(O0(0)))))))))))).toLowerCase);def OO[Char](c:t)={(c-('O'/'O')).toChar};def O[Char](c:t)={OO(OO(OO(c)))};def O00[Char](c:t)={(c+('O'/'O')).toChar};def O0[Char](c:t)={O00(O00(O00(c)))}}

Old (illegal) version:

def h(){type t=scala.Char;print(""+OO(O(O('O')))+(""+OO(O(O(O('O'))))+O('O')+O('O')+'O'+OO(O(O(O(O(O('0'))))))+O0(O0(O0(OO('O'))))+'O'+O0('O')+O('O')+OO(OO(O(O(O('O')))))+O(O(O(O(O('0')))))).toLowerCase);def O0[Char](c:t)={O00(O00(O00(c)))};def O[Char](c:t)={OO(OO(OO(c)))};def OO[Char](c:t)={(c-('O'/'O')).toChar};def O00[Char](c:t)={(c+('O'/'O')).toChar}}
share|improve this answer
    
I believe the empty string ("") and strings/character literals containing the character zero ('0') are not allowed. Only the string "O" (capital O) and the number 0 are. –  Timwi Mar 12 '11 at 22:59
    
Bugger. Some editing required then... –  Gareth Mar 12 '11 at 23:08

C (or C++) (body segment: 49) (cheating)

when compiling, compile to a binary called Hello\ World\!, the code is:

#include <stdio.h>
#include <string.h>
int main(int i,char**a)
{
  int j=i+i,k=j<<j;puts(strrchr(*a,'O'-(k<<j))+i);
}

The strrchr segment is required to remove the full path in the event the program name passed in contains the full path, also no arguments must be passed in..

Typical compile line could be: gcc -o Hello\ World\! foo.c

share|improve this answer

I believe, Brainfuck is the best tool for this puzzle:

++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++
.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.
------.--------.>+.>.

code length: 111

Algorithm explained

Increment cell 0 to 10 (it will be loop counter)
Repeat 10 times ; will stop at cell 0
  Increment cell 1 to 7
  Increment cell 2 to 10
  Increment cell 3 to 3
  Increment cell 4 to 1
Increment cell 1 by 2 and output it ; Thus, output ASCII 72 'H'
etc. for all symbols in 'Hello World!'

Longer version without loop:

+++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++.+++++++++++++++++
++++++++++++.+++++++..+++.-------------------
---------------------------------------------
---------------.+++++++++++++++++++++++++++++
++++++++++++++++++++++++++.++++++++++++++++++
++++++.+++.------.--------.------------------
---------------------------------------------
----.-----------------------.

code length: 389

share|improve this answer
    
This is a code-golf puzzle, the point is to make the shortest possible program. Part of that involves posting the length of your code. –  zzzzBov Mar 18 '12 at 19:44
    
Thanks for the point. Fixed –  Michael Mar 19 '12 at 3:20

JavaScript, 88

t=!0<<-~-~-~-~!0
r=[]
for(i in{Hello:"O",World:0})r+=i+String.fromCharCode(t++)
alert(r)

99

Many thanks to @Timwi for the suggestions

removed ternary operator:

o={Hello:"O",World:0}
t=!0<<-~-~-~-~!0
c=String.fromCharCode
r=c(0)
for(i in o)r+=i+c(t++)
alert(r)

103
aliased String.fromCharCode

o={Hello:"O",World:0}
t=!0<<-~-~-~-~!0
c=String.fromCharCode
for(i in o)o[i]?r=i+c(t):alert(r+i+c(++t))

117
Switched if-else to ternary operator

o={Hello:"O",World:0},t=!0<<-~-~-~-~!0
for(i in o)o[i]?r=i+String.fromCharCode(t):alert(r+i+String.fromCharCode(++t))

125
I'm keeping the "O" just to have an "O" in the program.

o={Hello:"O",World:0},t=!0<<-~-~-~-~!0
for(i in o)if(o[i])r=i+String.fromCharCode(t)
else alert(r+i+String.fromCharCode(++t))

133

o={Hello:"O",World:0},t=!0<<(!0+!0<<!0)+!0
for(i in o)if(o[i])r=i+String.fromCharCode(t)
else r+=i+String.fromCharCode(t+!0)
alert(r)
share|improve this answer
    
@Joey, that's largely the point. –  zzzzBov Mar 11 '11 at 23:35
    
@Joey, i read on the codegolf meta that one should avoid answering their own question for some time to encourage others to try various different approaches. My plan was to uncomment it in a day or two. –  zzzzBov Mar 11 '11 at 23:43
    
@Joey, wasn't working on my own machine at the time, and I didn't feel like e-mailing myself the answer when I could just post it in a comment. –  zzzzBov Mar 11 '11 at 23:57
    
@Joey: "There are only five of those" is wrong. Anyone can suggest an edit. Just click on "edit" between "link" and "flag" and you can see the code. –  John Mar 12 '11 at 3:57
    
@zzz: "that's largely the point" Anyone can still see it. Anyone can suggest an edit by clicking on "edit" in between "link" and "flag", which will bring up the edit dialog, revealing your code. –  John Mar 12 '11 at 3:59

Haskell - 146

a:b:c:f:g:s:j:z=iterate(\x->x+x)$succ 0
y=[f+j,d+a,c+s+h,l,a+b+l,s,f-s+o,o,a+b+o,l,l-f,a+s]
[h,e,l,_,o,_,w,_,r,_,d,x]=y
main=putStrLn$map toEnum y

I figured pattern matching would give Haskell a huge leg up, in particular because you can initialize powers of two like so:

one:two:four:eight:sixteen:thirty_two:sixty_four:the_rest = iterate (*2) 1

However, as seen in MtnViewMark's Haskell answer (which deserves many many upvotes, by the way) and other answers, better compression can be achieved by using more than just + and -.

share|improve this answer

Clojure - 46 chars

(map print(butlast(rest(str'(Hello World!)))))

Note that Hello and World! are symbols, not literals of any kind.

share|improve this answer

C++

/*
Hello World!
*/
#define CodeGolf(r) #r
#include<iostream>
using namespace std;
int main()
{
    char str[*"O"];
    freopen(__FILE__,CodeGolf(r),stdin);
    gets(str);gets(str);puts(str);
}
share|improve this answer
    
The 100 violates the rule about numbers. Please replace it with something more amusing. –  Joey Adams Mar 12 '11 at 23:03
    
@joey-adams Thanks for pointing out. Changed that. –  fR0DDY Mar 13 '11 at 5:15
    
Line 7: error: ISO C++ forbids declaration of 'main' with no type –  Nathan Osman Mar 15 '11 at 2:31
    
@george-edison Corrected. Though it worked without int on g++ –  fR0DDY Mar 15 '11 at 3:07

J, 250

oo=:#a.
o0=:<.o.^0
o0o=:%:%:%:oo
ooo=:p:^:(-*oo)
o=:<.(^^^0)*(^^0)*(^^0)
o00=:o,~(,[)o0(**(**[))o0o
oo0=:*/p:(!0),>:p:!0
echo u:(o0(**(**]))o0o),(ooo ooo ooo(o.o.^^^0)*oo),o00,(+:%:oo),(oo0-~!p:>:>:0),o,(<.-:o.o.^o.^0),(>:p:(]^])#o00),(*:#0,#:oo),oo0

I had entirely too much fun with this one, and I learned a little bit more J to boot. Also, ooo ooo ooo may be the stupidest code I've ever written.

share|improve this answer

Python, 106

o=-~-~-~0
L=o+o
d,W=-~L,o**o
_=o*W
print str().join(chr(L+W+_-i)for i in[d*L,L+d,L,L,o,-~_,W,o,0,L,d+d,_])
share|improve this answer
    
Use '' (with no preceding space) instead of str() –  aditsu Mar 19 '13 at 11:29
1  
Also, VERY nice! :) Btw, the multiple assignment is not saving any characters –  aditsu Mar 19 '13 at 11:39
    
@aditsu: I can't, that would be a string literal. Thanks for your comments. :) –  flornquake Mar 20 '13 at 14:49
    
Oh oops, somehow the empty string didn't register as a literal in my mind –  aditsu Mar 20 '13 at 15:23
    
+1. Should be easy to port to Perl. –  Steven Rumbalski Mar 20 '13 at 16:05

QR with halfblocks (169)

With QR-Code by using UTF-8 Half-blocks characters:

  ▛▀▀▌▚▝▐▀▌▛▀▀▌
  ▌█▌▌▖▞▚▚▘▌█▌▌
  ▌▀▘▌ ▚▛▞ ▌▀▘▌
  ▀▀▀▘▚▌▙▘▌▀▀▀▘
  ▄▙▛▚▜▀▄▀█▖▝▄▌
  ▖▄▄▘▖▄▄▄▟▗ ▀▘
  ▜Code  golf!▌
  ▚▟▘▘▝▙▛▚▐▀▖▜▘
  ▘▘ ▀▛▗▚▗▛▀▌▄ 
  ▛▀▀▌▟▌▜▖▌▘█▐▘
  ▌█▌▌▘█▌▟█▜▙▐ 
  ▌▀▘▌▚▌▌█▗▝▌▚▘
  ▀▀▀▘ ▝▘▘▀▀▀▀▘

Unfortunely, this won't render nicely there.

You could cut'n paste above sample into a console with utf-8 fonts... Or look my little try on jsfiddle.

Idealy, this could look like:

QR:Hello world

share|improve this answer
    
Use your smart phone to see this or zbar under linux. (sample: xwd | xwdtopnm | zbarimg /dev/stdin ) –  F. Hauri Jan 28 at 21:40
    
Isn't Code golf! a character literal? –  ugoren Feb 5 at 8:10
    
No, because it's only there for cosmetic! You could drop and replace them by anything (like ¢@d3||@01f for sample). The number of required characters won't change! –  F. Hauri Feb 5 at 8:51
    
I actually think this answer, though creative, doesn't qualify because it isn't a program. QR is a way to encode data, not a programming language. –  ugoren Feb 5 at 9:20
    
Also what about languages like postscript, svg or other presentation languages already used there!? A way to encode is a language anyway... I think! –  F. Hauri Feb 5 at 10:07

PHP, 220 202 201 characters

<?$b=++$a+$a;$y=$b+$a;$c=$y+$b;$z=$c*$b;$s=$y*$z+$b;$h=$s*$b+$z-$b;$e=$z*$z+$a;$o=$e+$z;$l=chr($o-$y);echo chr($h).chr($e).$l.$l.chr($o).chr($s).chr($h+$z+$c).chr($o).chr($o+$y).$l.chr(--$e).chr(++$s);

Uses no numbers, string literals, RegEx literals, or pre-built functions that display "Hello World!".

It works: http://codepad.viper-7.com/OhXBkA

share|improve this answer

PHP – 49 chars

<?=Hello.chr(($a=-~-~-~0).~-$a).World.chr($a.$a);

Changelog:

  • (73 -> 86) Forgot to output an exclamation point... sigh
  • (86 -> 57) Uses a single variable with incrementing
  • (57 -> 51) Changed to use bitwise operators on 0
  • (51 -> 49) More bitwise operators
share|improve this answer
    
Pretty sure that the constants would count as string literals because of the conversions. –  Kevin Brown Mar 13 '11 at 18:23
    
@Bass5098 Thanks for your edit! I approved it. –  nyuszika7h Mar 13 '11 at 18:34
    
@Bass5098 that doesn't work, you need to have chars 32 and 33, not 21 and 22. <?$a=-~-~-~0;echo Hello.chr($a.$a-1).World.chr($a.$a); works, but it's 54 chars. –  zzzzBov Mar 16 '11 at 18:07
    
@zzzzBov I could not figure out how to clear the edit initially, and forgot to roll it back once it was approved. –  Kevin Brown Mar 16 '11 at 22:52
    
whoops, i left the 1 in there. There I go breaking my own rules...<?$a=-~-~-~0;echo Hello,chr($a.~-$a),World,chr($a.$a); is what I should have used, still 54 chars. –  zzzzBov Mar 16 '11 at 23:34

C - 125 128 chars

I didn't see a proper C entry so I wrote this one:

main(){char u='O'/'O',t=u+u+u,h=u<<t,s=h<<u+u,d='O'*'O'+t,l=d+h,o='O'|s,
g['O']={'O'-h+u,d+u,l,l,o,s,o+h,o,o+t,l,d,s+u};puts(g);}

(As noted below, redefining u via main(u) can save another 9 bytes.)

share|improve this answer
3  
Your array isn't NULL-terminated, so it prints junk. Pay 2 chars. Earn them back, and more, by main(u) instead of 'O'/'O'. –  ugoren May 8 '12 at 6:58

C, 190 189 204

a,b,c,h,e,l,o;i[4],*j=i;main(_){o=*"O";a=_+_+_;b=_<<a;c=b*a+b;h=(o>>_)^o;o^=c;e=h-a;l=o-h+e;*j++=h-c|e<<b|l<<b+b|l<<c-b;*j++=o|c<<b|o+b-c<<b+b|o<<c-b;*j=o+a|l<<b|e-_<<b+b|c+_<<c-b;puts(i);}
  • Must be run with exactly zero command line arguments since it depends on argc being equal to 1.
  • no preprocessor macros
  • Might be system endian dependant (involves cast from integer array to char array)
  • Not memory safe
a,b,c,h,e,l,o;   // variables
i[],m,*j=i;      // array and pointer

main(_)          // if no arguments are given _ will be 1
{
    o=*"O";      // our one legal string constant
    a=_+_+_;     // a = 3
    b=_<<a;      // b = 8
    c=b*a+b;     // c = 32
    h=(o>>_)^o;  // h = 'h'
    o^=c;        // o = 'o'
    e=h-a;       // e = 'e'
    l=o-h+e;     // l = 'l'
    *j++=h-c|e<<b|l<<b+b|l<<c-b;     // j[0] = 'H' | 'e' << 8 | 'l' << 16 | 'l' << 24
    *j++=o|c<<b|o+b-c<<b+b|o<<c-b;   // j[1] = 'o' | ' ' << 8 | 'W' << 16 | 'o' << 24
    *j=o+a|l<<b|e-_<<b+b|c+_<<c-b;   // j[2] = 'r' | 'l' << 8 | 'd' << 16 | '!' << 24

    puts(i);     // puts("Hello World!\0\0\0\0")
}

Revisions:

a,b,c,h,e,l,o;i[9],*j=i;main(_){o=*"O";a=_<<_|_;b=_<<a;c=_<<b-a;h=(o>>_)^o;o^=c;e=h-a;l=o-h+e;*j++=h^c|e<<b|l<<b<<b|l<<(c-b);*j++=o|c<<b|(o+b^c)<<b<<b|o<<(c-b);*j=o+a|l<<b|e-_<<b<<b|c+_<<(c-b);printf(i);} /* original */
a,b,c,h,e,l,o;i[4],*j=i;main(_){o=*"O";a=_+_+_;b=_<<a;c=b*a+b;h=(o>>_)^o;o^=c;e=h-a;l=o-h+e;*j++=h-c|e<<b|l<<b+b|l<<c-b;*j++=o|c<<b|o+b-c<<b+b|o<<c-b;*j=o+a|l<<b|e-_<<b+b|c+_<<c-b;puts(i);} /* golfed better */
a,b,c,h,e,l,o;i[],m,*j=i;main(_){o=*"O";a=_+_+_;b=_<<a;c=b*a+b;h=(o>>_)^o;o^=c;e=h-a;l=o-h+e;*j++=h-c|e<<b|l<<b+b|l<<c-b;*j++=o|c<<b|o+b-c<<b+b|o<<c-b;*j=o+a|l<<b|e-_<<b+b|c+_<<c-b;puts(i);} /* removed illegal numeric literal in array size */
share|improve this answer
    
i[9] violates the rules. –  ugoren Nov 9 '12 at 18:15
    
shoot, it does doesn't it. –  Wug Nov 9 '12 at 18:26
    
i['O'] should be fine. Or i[-~-~-~-~0]. –  ugoren Nov 9 '12 at 21:05
    
The way that's there works (with the caveat that it starts scribbling over other global variables, but they're not read from again later so I don't care). I could probably reorder them to specifically take advantage of this behavior and make them only overwrite variables that are from-then-on unused. Anyway, at this point it contains no numeric literals and I rather like it that way. –  Wug Nov 9 '12 at 21:57

JavaScript 100 chars (when all in one line)

Borrowing subtly from zzzzBov -- with regard to the -~ trick, not seen that before :) -- this is another way to source those pesky space and exclamation characters.

Relies on the Function.toString() ability:

c=!0<<-~-~!0;a=[];
for(i in{Hello:0,world:"O"}){a+=i+([]+function(){!0}).charAt(c);c+=c>>!0}
alert(a)

JavaScript 89 chars (when all in one line)

Also zzzzBov could slightly improve the first example by using the fact that arrays in JS collapse down to strings when used in a calculation; oh, and not creating needless vars ;)

t=!0<<-~-~-~-~!0;r=[];for(i in{Hello:"O",World:0})r+=i+String.fromCharCode(t++);alert(r)
share|improve this answer

JavaScript 654 chars

O=[[,],[,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,],[,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,],[,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,],[,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,],[],[,,,,,,,,,,,,,,,,],[,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,],[,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,],[,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,],[,,,,,,,,,,,,,,,,,,,,,,,,,,,,,]];for(O_O=0;O.length>O_O;O_O++)document.write(String.fromCharCode((O[O_O].length||-Math.pow([,,].length,[,,,,,].length)-[,,,,,,,].length)+Math.pow([,,].length,[,,,,,,].length)+Math.pow(++[,].length,[,,,].length)-[,].length));document.write(String.fromCharCode(Math.pow([,,].length,[,,,,,].length)+[,].length))

What about abusing array literals just to have unary base. This program has advantage of not using 0.

share|improve this answer
    
While this may be valid, this particular puzzle is a code golf, which means you should be aiming for the shortest code possible. At 600+ chars, you're nowhere near the <100 chars that the existing JS solutions already have. –  zzzzBov Nov 20 '12 at 15:01
    
@zzzzBov: I'm actually not trying to win. –  xfix Nov 20 '12 at 15:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.