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.

In a language of your choice, write a program that exactly outputs the characters Hello world! followed by a newline. The code:

  • should not use any character more than twice (alphanumeric, symbol, whitespace...anything)
  • should not use any external resources
  • should not use any user input
  • should not output anything else

An example of a valid python program:

print("He%so world!"%(2*'l'))

An example of an invalid python program (the character 'r' is used three times):

print("Hel"+chr(108)+'o world!')

Winner is whoever has the most votes after 14 days.

share|improve this question
3  
Is it allowed to use a letter more than twice in different case? E.g. "RRrr" –  Boann yesterday
12  
What about the repeated letter o in the "valid" python example. There also these:- " ' ( ) –  Adam Speight yesterday
8  
@AdamSpeight what do you mean? The letter is only used twice, as per the specs. –  Josh yesterday
4  
@AdamSpeight ... which is literally the same. –  VisioN yesterday
28  
I guess brainfuck is out of the question... –  Jubobs yesterday
show 14 more comments

48 Answers

Ruby (1.9+)

Since this is a popularity contest let's try to not use ANY of the characters from 'Hello world!' while still using other characters only a maximum of two times:

puts("S\107VsbG8gV29ybGQhCg".unpack(?m))

It's 40 chars btw.

Bash

And this one uses unicode magic.

Notes:

  • While the orignal characters appear elsewhere (unlike the ruby example), the printed string contains only non-ascii characters.
  • Two from the three spaces are actually tabs, so there are no utf-8 characters that appear more than 2 times
  • As binary some of the octets do appear more than 2 times, hopefully that's not against the rules. I'm trying to resolve them though.

Code:

echo '๐“—๐ž๐‘™๐’๐“ธโ€Š๐“ฆ๐—ˆ๐–—๐–‘๐˜ฅยก'|iconv -t  asCIi//TRANSLIT

For those who don't have a proper font installed it looks like this:

code as image

Here is the hexdump:

00000000  65 63 68 6f 20 27 f0 9d  93 97 f0 9d 90 9e f0 9d  |echo '..........|
00000010  91 99 f0 9d 92 8d f0 9d  93 b8 e2 80 8a f0 9d 93  |................|
00000020  a6 f0 9d 97 88 f0 9d 96  97 f0 9d 96 91 f0 9d 98  |................|
00000030  a5 c2 a1 27 7c 69 63 6f  6e 76 09 2d 74 09 61 73  |...'|iconv.-t.as|
00000040  43 49 69 2f 2f 54 52 41  4e 53 4c 49 54 0a        |CIi//TRANSLIT.|
0000004e

You have to run it on a machine where the default charset is utf-8. I tried on an OSX10.8 using iTerm2 with the following environment:

bash running in iTerm2

PHP 5.4

This uses zLib: (unfortunately it does uses the characters e and o)

<?=gzuncompress('xโ–’โ–’Hโ–’โ–’โ–’W(โ–’/โ–’IQโ–’!qh');

Hexdump:

00000000  3c 3f 3d 67 7a 75 6e 63  6f 6d 70 72 65 73 73 28  |<?=gzuncompress(|
00000010  27 78 9c f3 48 cd c9 c9  57 28 cf 2f ca 49 51 e4  |'x..H...W(./.IQ.|
00000020  02 00 21 71 04 68 27 29  3b                       |..!q.h');|
00000029

+1

Here is the ruby 2.0 code I used to test for duplicates:

d=ARGF.read
p [d.split(//),d.unpack('C*')].map{|x|x.inject(Hash.new(0)){|i,s|i[s]+=1;i}.select{|k,v|v>2}}
share|improve this answer
1  
While I always appreciate Unicode cleverness, your bash attempt doesn't seem to satisfy the question's specification. "write a program that exactly outputs the characters Hello world! followed by a newline" –  FireFly yesterday
6  
@FireFly: it does satisfy it. iconv with the target encoding ascii//translit will transliterate the unicode characters to basic ascii. And of course echo will add the newline, so this one fits the spec (if we don't consider the similarity of the octets) –  SztupY yesterday
2  
On my system I get Hello World? instead of Hello World! –  marinus yesterday
 
@SztupY oh, you're right, my bad. Very clever, I like it! –  FireFly yesterday
1  
@MrLister '\x9D` is an octet that is part of a single Unicode character. The question did specify repetition of characters, not bytes, so I'd consider this legal. Anyway, he could encode it in UTF-32, in which case going by (32-bit) characters would make it satisfy the rules. –  FireFly 12 hours ago
show 5 more comments

Vim command (18 keystrokes)

iHe<Esc>3al<Esc>io Wor<Right>d!<Enter>

Doesn't repeat any keystroke more than twice.

It kinda violates the "user input" rule since it's still the user that needs to input that sequence, but I suppose if you store it as a macro or an nnoremap beforehand it would count, since you're just running it without explicitly doing any input to make it happen.

share|improve this answer
 
That's awesome! –  Josh yesterday
 
you have an 'e' and 3 'E'; 3 'i' –  bolov yesterday
11  
If you're talking about the characters in Right, Esc, and Enter, they're not literally those characters. Vim deals in keystrokes, which I am deeming equivalent to characters for the purposes of this contest (and the "don't repeat more than twice" rule). I only use the i keystroke twice, and the <Right> keystroke once. As for E, I never use it at all - but I do use <Esc> twice and <Enter> once. –  Joe Z. yesterday
4  
Now I'm disappointed that Emacs doesn't have a M-x hello-world command... –  RemcoGerlich yesterday
3  
Even if it did, that's three ls. –  Joe Z. yesterday
add comment

HQ9+, 1 char

H

keeping it simple :)

share|improve this answer
1  
HQ9+ outputs a comma though ;) –  Josh yesterday
5  
I'd still keep your answer out...it is a popularity contest after all, rules are for the wind! –  Josh yesterday
2  
For any challenge like this, HQ9+ should be outlawed just because it's so obvious to begin with. –  Iszi yesterday
9  
@Josh This is one of the rare events where I miss the downvote button on comments. If we throw away rules there is nothing left to justify the contest at all. –  Howard yesterday
2  
@Howard I feel dirty. I upvoted yours and Josh's comment –  Cruncher yesterday
show 3 more comments

C, 194 chars

#
#
/*$$@``[[]]..*/ATION_=94;main(BBCDDEEFFGGHJJKKLLMMPPQQRRSSUUVVWWXXYYZZabbdefgghhjjkkmpqqsstuuvvwxyyzz) {{printf("He%clo \
world!%c\
",2^7&!88&~1|~-1?4|5?0x6C:0:6<3>2>=3<++ATION_,'@'^79-5);}}

Since this isn't golf, I decided to have some fun and try to use every character exactly twice (while putting as few as possible in a comment, because that's boring). I realise that I didn't do terribly well, since my code contains a ton of boring "dead" code too (not in the literal sense, but in the sense of placeholder characters just used in order to fullfil the requirement). Anyway, this was surprisingly hard (but fun) with the two-character limitation, so unfortunately I couldn't come up with anything more interesting. It did give me an idea for a new problem though...

share|improve this answer
2  
Creatively impressive. I like it. –  Josh 23 hours ago
 
The slash `\` ending lines three and four make it a line continuation, not newline. And line four is terminated by a EOF, making it legal. –  Josh 16 hours ago
add comment

Python 3 [38 bytes]

exec('import '+chr(95)*2+"h\x65llo__")

I wouldn't consider import __hello__ as an external resource.

share|improve this answer
add comment

PHP, 33 chars

I just love how much PHP is forgiving and understanding!

<?=Hel.str_rot13("yb jbe").'ld!';

Before it was deleted (or if it's still there, I'm totally blind), I saw a comment saying "No brainf*ck? :D". Well, it is pretty much impossible to write a solution in BrainF*ck, as you know. But I managed to code this, just for the lulz.

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

If you don't have a BF interpreter, the code above just prints the PHP one :P

share|improve this answer
 
I didn't know even a code like this is valid! Php tag isn't closed and string Hel isn't surrounded by quotes. Also I've never heard about str_rot13 before. –  Aycan YaลŸฤฑt yesterday
 
@AycanYaลŸฤฑt the closing php tag is not required on EOF, and if you put a string without quotes it assumes it is an undeclared constant with the same content as its name and gives a warning –  Einacio yesterday
 
You have two ls... –  Manishearth yesterday
 
@Manishearth Ooh, you should see the comment thread on the question. –  Mr Lister yesterday
2  
@PaลญloEbermann I would say that using that is like cheating, since it's not pure PHP anymore. –  Vereos 21 hours ago
show 4 more comments

Powershell, 20

"He$('l'*2)o world!"
share|improve this answer
 
"l" is repeated 2 times, and so are the quote characters –  MistressDavid 16 hours ago
4  
@MistressDavid That makes this answer valid. Quote: "should not use any character more than twice (alphanumeric, symbol, whitespace...anything)". So single repetitions are okay. Also, since you saw so many answers with that "problem", instead of posting a comment on each one, you should stop and reconsider, asking yourself, "Why does everyone repeat characters? Did I misunderstand something or are there really so many people who would misunderstand the question?" –  Quincunx 16 hours ago
 
@Quincunx My bad. I really should have read the question more carefully. –  MistressDavid 16 hours ago
add comment

Ruby: 27 characters

puts [:Hel,'o wor',"d!"]*?l

Sample run:

bash-4.1# ruby <<ENDOFSCRIPT
> puts [:Hel,'o wor',"d!"]*?l
> ENDOFSCRIPT
Hello world!

Ruby: 25 characters

(Based on Vache's comment.)

puts 'He'+?l*2+"o world!"

Ruby: 23 characters

(Copy of Danko Durbiฤ‡'s Powershell answer.)

puts"He#{?l*2}o world!"
share|improve this answer
 
puts 'He'+'l'*2+'o world!' is one character shorter! –  Vache yesterday
2  
But has 6 โ€œ'โ€'sโ€ฆ –  manatwork yesterday
 
haha I was so focused on letter characters that I never noticed that. never mind! –  Vache yesterday
 
@Vache, your comment is actually good. Thank you for it. –  manatwork yesterday
1  
Remove the space after puts and make it 23. -- puts"He#{?l*2}o world!" –  Sampriti Panda yesterday
show 2 more comments

Perl: 34 characters

$_="He12o wor3d!
";s{\d}{l}g;print

Sample run:

bash-4.1# perl -e '$_="He12o wor3d!
> ";s{\d}{l}g;print'
Hello world!

(Not a big deal. Posted just to use at least once in my life s/// with those fancy delimiters.)

share|improve this answer
add comment

C - 43 Characters

main(){printf("Hel%co World!%c",'k'+1,10);}

Output

Hello World!

Character Counts

' ' Count: 1    '!' Count: 1    '"' Count: 2    '%' Count: 2    ''' Count: 2
'(' Count: 2    ')' Count: 2    '+' Count: 1    ',' Count: 2    '0' Count: 1
'1' Count: 2    ';' Count: 1    'H' Count: 1    'W' Count: 1    'a' Count: 1
'c' Count: 2    'd' Count: 1    'e' Count: 1    'f' Count: 1    'i' Count: 2
'k' Count: 1    'l' Count: 2    'm' Count: 1    'n' Count: 2    'o' Count: 2
'p' Count: 1    'r' Count: 2    't' Count: 1    '{' Count: 1    '}' Count: 1
share|improve this answer
 
Don't you need main etc? –  FireFly yesterday
 
@FireFly I guess so! It did say to write a program. –  Kirk Backus yesterday
 
That's C but not C++. C++ does not have implicit int (and you can't spare another i). –  Ben Voigt yesterday
 
@BenVoigt Okie dokie! –  Kirk Backus yesterday
add comment

HTML Fiddle - 21 characters

Hel&#108;o World!<br>
share|improve this answer
 
Does that include the trailing newline? –  Josh yesterday
 
Doh...missed that requirement! I'll add a <br> in there. –  Briguy37 yesterday
 
I'm not quite sure that the <br> tag actually outputs a \n character. –  netinept 23 hours ago
6  
@netinept :It asked for a newline, not a \n so I'd say <br> counts –  Chris 23 hours ago
1  
But the character r is repeated in the solution. Did I misinterpret the requirements? –  JAnderton 12 hours ago
show 1 more comment

Scala: 34 29 characters

I'm proud of myself for this one:

printf("He%c%co world!\n",108,108)

Had a really hard time overcoming duplicate 'l's, 'r's, quotation marks and brackets. Then I discovered the old Java printf function, which will happily convert numbers to letters when given the %c format specifier.

Update

MrWonderful did a wonderful thing by pointing out that a whole bunch of characters can be saved by using up my second 'l' manually in the string!

printf("Hel%co world!\n",108)
share|improve this answer
1  
@KCaloux, Since you are allowed up to 2 'l's, wouldn't printf("Hel%co world\n",108) at 28 be even better? –  MrWonderful yesterday
 
@MrWonderful I think you're absolutely correct! (Also I just realized that I forgot to include the '!') –  KChaloux yesterday
 
From what I understand, this isn't a valid entry, though a good attempt at it. printf contains a r as does world. Same goes for the letter o which is used more than once. This is based on my interpretation of the following statement from the OP "An example of an invalid python program (the character 'r' is used three times): print("Hel"+chr(108)+'o world!')" –  JAnderton 12 hours ago
 
@JAnderton I had a ruby program parse out my script to make sure there were no characters included more than twice. Read it again. There are 2 rs, not 3. One in "printf" and one in "world". The reason the python one is invalid is because it includes chr –  KChaloux 6 hours ago
add comment

Actually I don't like cheating :P

Python

print("!dlrow o%seH"%('l'*2))[::-1]
share|improve this answer
1  
You should specify that it's for python2. In python3 the you get the reversed string as output and then a TypeError. –  Bakuriu 15 hours ago
add comment

Sclipting

ไธŸ๋‚†๋…ฌ๋‹†๋ฌฌ๊ธ…๋ฏ๋Œฆ๋กค๊ธ๋คŠ

I saw this beautiful HelloWorld program on esolang's Hello World program list.

share|improve this answer
add comment

Emacs Command (14 keystrokes)

He<Ctrl-3>l<Left>o wor<End>d<Enter>

If that vim answers is legal then this must be too:)

Joking aside, macro it can become too:)

More nonsense aside, I can probably squeeze some more, but this seems to be good enough for the time being (since it beats vim).

P.S., please ignore all my nonsense (I (rarely, but)use vim too!).

share|improve this answer
2  
I still think vim > emacs. –  Joe Z. 17 hours ago
add comment

JavaScript [27 bytes]

alert('Hel\x6co wor\x6cd!')

Too primitive isn't it?

share|improve this answer
5  
Whereโ€™s the newline? –  Christopher Creutzig yesterday
add comment

Hmm.

In C, given these rules, we can only have one #define (because of i and n) and at most two function calls OR definitions (( and )).

I presume there's pretty much only one way to do it (though I'm probably wrong):

main(){puts("Hello w\x6fr\154d!");}
share|improve this answer
 
You still need to output the trailing newline. Doing this challenge in C is difficult... –  Josh yesterday
1  
@Josh I just have to use puts() instead of printf(). –  Oberon yesterday
2  
But what's wrong with o in world? –  VisioN yesterday
 
@VisioN In an old draft of the same code (before I realized how hard it actually was to write such a program) I used another 'o' elsewhere. But this isn't code-golf, so I guess it doesn't have to be fixed. –  Oberon yesterday
4  
In fact, I'm not sure why every answer is displaying a character count. –  Oberon yesterday
add comment

BASH

printf 'Hel\x6co world!\n'

Cred @manatwork

echo $'Hel\x6c\x6f world!'
share|improve this answer
 
The first one has 3 โ€œeโ€'s and 3 โ€œoโ€'s. –  manatwork yesterday
 
@manatwork: Gag; thanx ;) –  Sukminder yesterday
 
In Bash you may skip -e by using the special $'โ€ฆ' syntax: echo $'Hel\x6c\x6f world!'. –  manatwork yesterday
add comment

C - 46 Characters

main(){printf("He%clo wor%cd!\x0d",'l',108);}

Prints out:

Hello world!

share|improve this answer
 
If you're aiming for golf, main(){printf("He%clo world!%c",108,10);} should work, and saves you a few chars. –  FireFly yesterday
 
@FireFly you're right, you'd save me 3 characters. Your suggestion works perfectly, too. –  Phillip Kinkade yesterday
add comment

nginx.conf

return  202 "He&#x6c;lo world!\n";

In action:

%curl -6 http://localhost/ | lynx -dump -stdin
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    18  100    18    0     0  16635      0 --:--:-- --:--:-- --:--:-- 18000

   Hello world!

%
share|improve this answer
 
There are 4 spaces in this solution... –  Josh yesterday
1  
@Josh, actually, there are 5. But couldn't you consider that two of the first four are tabs, and then the last one is a non-breakable space? :-) –  cnst yesterday
 
That works for me! –  Josh yesterday
 
@Josh, actually, now that we're using HTML, no more need for set. This only has 3 spaces now, any one of which could be a tab! –  cnst yesterday
 
Very nicely done. –  Josh yesterday
add comment

Fugue

From the esolang wiki:

enter image description here

And you can listen to the source code.

share|improve this answer
6  
I'd say this is invalid. I can see more than 4 middle c's in the right hand. –  Quincunx yesterday
3  
Not to mention the string of four consecutive G's in the 9th measure. –  Joe Z. yesterday
 
Did you write this, or only render it? Given that you link to the esolang archive's MIDI. –  FireFly yesterday
2  
-1 for plagiarism. This is from esolang wiki. You do not cite it. Edit: reverted, now the esolang wiki is cited. –  Quincunx yesterday
add comment

GolfScript

'He

o world!'n/"l"*

Substitutes two newlines (fortunately the third one, needed for the substitution, is provided by the built-in n), using both types of string literal to avoid quadruplicate copies of a quote mark. Since l is the only character which occurs more than twice in the original string, it's not too hard.

share|improve this answer
 
You forgot the "!". –  Howard yesterday
 
@Howard, missed it. Oops. –  Peter Taylor yesterday
 
The letter o is repeated also –  Adam Speight yesterday
1  
@AdamSpeight: There is only twice the letter o. The rules say that it's not allowed to have a character more than twice. –  ProgramFOX yesterday
add comment

AWK,34

BEGIN{printf"Hel%co world!\n",108}
share|improve this answer
add comment

nginx.conf

set $i l;
return 202 "Hel${i}o world!\n";

In action:

opti# curl -6v "http://localhost/"
* About to connect() to localhost port 80 (#0)
*   Trying ::1...
* connected
* Connected to localhost (::1) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.26.0
> Host: localhost
> Accept: */*
>
< HTTP/1.1 202 Accepted
< Server: nginx/1.4.1
< Date: Fri, 17 Jan 2014 18:02:08 GMT
< Content-Type: application/octet-stream
< Content-Length: 13
< Connection: keep-alive
<
Hello world!
* Connection #0 to host localhost left intact
* Closing connection #0
share|improve this answer
 
Unfortunately your code has three e's and three l's! –  Josh yesterday
 
One of the duplicate l's can be removed easily. e is harder, unless nginx is case-insensitive –  SztupY yesterday
2  
Don't forget the spaces! –  Vereos yesterday
add comment

XQuery (19 chars)

"Hello Wor&#x6C;d!"
share|improve this answer
add comment

Piet-- No characters whatsoever!

Hello world in Piet

share|improve this answer
3  
I would consider the codels as "characters". –  Paลญlo Ebermann yesterday
 
@PaลญloEbermann Working on a version w/ no repeated codels. –  ValekHalfHeart yesterday
add comment

HTML — 17 characters

He&#x6c;lo world!

In action:

%echo -n "He&#x6c;lo world!" | wc
       0       2      17
%echo -n "He&#x6c;lo world!" | lynx -dump -stdin

   Hello world!

%
share|improve this answer
add comment

JavaScript, 66 characters

alert('Hel'+window.atob("\x62G8gd29ybGQhCg=="));//rH+in.\x689yQhC;

Inspired by FireFly, every character used by this code is used exactly twice.

share|improve this answer
 
I think you are allowed to use some characters just once, and you can drop the comment. –  Jan Dvorak 19 hours ago
add comment

Clojure (35)

(printf "He%c%co world!\n",108,108)

The whitespace almost got me, but Clojure allows the use of commas to separate parameters.

share|improve this answer
 
You could always have used different types of whitespace--space, newline, tab, vertical tab, form feed. I'm not completely sure of Clojure's grammar off the top of my head, but the first tree ones would likely work at least. –  FireFly 12 hours ago
1  
But it was interesting to try out this use of commas in Clojure. I thought I'd read somewhere that they could be used to substitute for whitespace but hadn't messed with it. –  Bob Jarvis 10 hours ago
 
As MrWonderful pointed out for my Scala version, you're only using a single 'l'. You could save a few characters by using your second, for (printf "Hel%co world!\n",108) –  KChaloux 6 hours ago
add comment

Befunge 98

a"!dlrow ol":'e'Hck,@

Here is a version where every character appears twice.

bka!e:dw"H@!dlrow  ol":'e'Hbk,a,@

Leaves a bunch of junk on the stack.

As a bonus, every single character has something done with it by the IP.

share|improve this answer
add comment

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.