129
\$\begingroup\$

Write a program that prints "Hello, World!". But also, if you take only the first, third, fifth, etc. characters of your program, the resulting program should still print "Hello, World!".

If your program is:

abc
def

It should output "Hello, World!", but so should

acdf

No solutions with fewer than 2 characters.

\$\endgroup\$
  • 2
    \$\begingroup\$ Can there be whitespace around the output? \$\endgroup\$ – vroomfondel Jun 26 '17 at 20:20
  • 18
    \$\begingroup\$ Amazing first post! \$\endgroup\$ – Adám Jun 26 '17 at 22:49
  • 11
    \$\begingroup\$ Seeing all the answers with "HHeelllloo" reminded me of speaking Whale. \$\endgroup\$ – Brian Minton Jun 27 '17 at 4:19
  • 32
    \$\begingroup\$ Pro tip for avoiding "Hello, World!" built-ins in challenges like this: use a slightly different string of similar complexity like "Greetings, Earthlings!" \$\endgroup\$ – Martin Ender Jun 27 '17 at 7:15
  • 6
    \$\begingroup\$ "No solutions with fewer than 2 characters." Amazing. \$\endgroup\$ – Robert Grant Jun 28 '17 at 9:10

71 Answers 71

4
\$\begingroup\$

My naive solution

C/C++, 111 105 byes 97 bytes

(6 bytes saved using "puts", inspired by Hawkings)

///
f(){puts("Hello, World!");}
#define x\
_f_(_)_{_p_u_t_s_(_"_H_e_l_l_o_,_ _W_o_r_l_d_!_"_)_;_}

After takeaway

//f)pt(Hlo ol!)}#eiex 
f(){puts("Hello, World!");}
\$\endgroup\$
4
\$\begingroup\$

dc, 112

 6   C * P
A d * 1 + d P
7 + d P
d P
3 + d P
B   4 * d P
C - P
F   6 * 3 - P
d P
3 + d P
6 - d P
8 - P
B   3 * P

All odd-indexed characters are whitespace that don't affect the output of the program. Character values are built arithmetically from single digits

Try it online.

\$\endgroup\$
4
\$\begingroup\$

Befunge-93, 43 41 bytes

"#!>d l r o:W$ #,#o l l e H#"#>#:###, _ @

Try It Online

Second version

"!dlroW ,olleH">:#,_@

is the shortest possible "Hello World!". This therefore is literally the shortest answer to this question for Befunge-93.

Try It Online

\$\endgroup\$
  • \$\begingroup\$ That's brilliant! Much better than my Befunge answer. Can't believe you haven't got any votes for this. \$\endgroup\$ – James Holderness Dec 20 '17 at 0:06
4
\$\begingroup\$

Java 8, 245 243 241 239 238 237 bytes (full program)

/**/interface M{static void main(String[]a){System.out.print("Hello, World!");}}//**/ iinntteerrffaaccee  MM{{ssttaattiicc  vvooiidd  mmaaiinn((SSttrriinngg[[]]aa)){{SSyysstteemm..oouutt..pprriinntt((""HHeelllloo,,  WWoorrlldd!!""));;}}}

Try it online.

After removing every other character:

/*itraeMsai odmi(tig])Sse.u.rn(Hlo ol!)}/*/interface M{static void main(String[]a){System.out.print("Hello, World!");}}

Try it online.

-2 bytes (243 → 241) thanks to @OlivierGrégoire.
-2 bytes (241 → 239) by creating a port of @SuperStormer's JavaScript answer.


Java 8, 63 bytes (lambda function)

/**/v->"Hello, World!"//**/vv-->>""HHeelllloo,,  WWoorrlldd!!""

Try it online.

After removing every other character:

/*v>Hlo ol!/*/v->"Hello, World!"

Try it online.

Explanation:

Utilizes //abc single-line comments and /*abc*/ multi-line comments.
See the Java-highlighting of the two programs/functions to see how these type of comments are used.

\$\endgroup\$
3
\$\begingroup\$

Befunge-98, 39 bytes

"" '!'d'l'r'o'W' ','o'l'l'e'H ""c k , @

Try it online!

Every second character removed:

" !dlroW ,olleH"ck,@

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Standard ML (MLton), 75 bytes

(*p r i n t " H e l l o ,   w o r l d ! " ( *)print"Hello, world!"(*  * )*)

Try it online!

After removing every other character, we have:

(print"Hello, world!"(*pitHlo ol!( *))

Try it online!

Just exploits the fact that comments are a digraph.

\$\endgroup\$
3
\$\begingroup\$

Pip, 33 bytes

"0H0e0l0l0o0,0 0W0o0r0l0d0!0"RM 0

Removes the 0s from the string, leaving just Hello, World!. Try it online!

The every-other-character version:

"Hello, World!"M0

Uses the map operator to replace each character in 0 with the string "Hello, World!". Try it online!

\$\endgroup\$
3
\$\begingroup\$

TeX, 54 bytes

Full version:

 %H e l l o ,   W o r l d ! \ e n d%
Hello, World!\end

Every other character:

 Hello, World!\end
el,Wrd\n

A bit boring, but for completeness.

\$\endgroup\$
3
\$\begingroup\$

Alice, 37 bytes

"_!_d_l_r_o_W_ _,_o_l_l_e_H_"_d_&_o_@

Try it online!

Removing every other character gives:

"!dlroW ,olleH"d&o@

Try it online!

The way this works is that Alice's string mode doesn't push all cell values directly to the stack. Some characters have a special meaning. In particular, _ is a wall which retains it's control flow meaning even when in string mode. But a horizontal wall is a no-op when the IP moves horizontally, so none of those _ do anything. Hence, they can be safely removed.

\$\endgroup\$
3
\$\begingroup\$

Self-modifying Brainfuck, 39 bytes

< - [ . <<- ] " e m s p X ! - p m m f I

Try it online!

Explanation

For anyone that is not familir with self-modifying brainfuck: The program itself gets put into registers directly left to where you start out from. This allows for some cool stuff you can't do with regular brainfuck.

This program will move back two registers (one with every second char removed) increment it and output in until the loop stops. The loop will stop once you increment the char that ends the loop itself thus making brainfuck ignore it.

Version without every second character:

 <-[.<-]"emspX!-pmmfI     
\$\endgroup\$
3
\$\begingroup\$

Japt, 25 23 bytes

Saved 2 bytes thanks to @Shaggy

`\H\e\¥\o\,\ \W\Ž\l\d\!

Try it online!

This seems to be the optimal compressed string that would output Hello, World! while ignoring every other \.

\$\endgroup\$
2
\$\begingroup\$

Braingolf, 42 40 bytes

#"#H#e#l#l#o#,# #W#o#r#l#d#!#" $_ <$_& @

Try it online!

After removing odd characters:

"Hello, World!"$ $&@

Try it online!

Explanation

Full program:

#"#H#e#l#l#o#,# #w#o#r#l#d#!#" pushes "Hello, World!" (including quotes)

$ adds the silent modifier to the next operator
space does nothing
_ Pops and prints the last item on the stack, but due to silent mode, does not print
< moves the first item on the stack to the end of the stack

This means that $_ <$_ will remove both quotes from the stack

& adds the greedy modifier to the next operator
@ pops and prints the last item on the stack as an ASCII character
  greedy modifier means it prints the entire stack.

With characters removed:

"Hello, World!" Pushes Hello, World!
Spaces are no-ops
&@ Prints entire stack as ASCII
@ is not affected by the silent modifier (apparently)

It feels really cheap using no-ops for this, but it's better than the Java/Python comment solutions imo

\$\endgroup\$
2
\$\begingroup\$

PHP, 63 bytes

#
echo"Hello, World!";
#eecchhoo""HHeelllloo,,  WWoorrlldd!!"";

Try it online!

PHP, 32 bytes

#eh"el,Wrd"
echo"Hello, World!";

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Noodel, 25 bytes

Hðeðlðlðoð,ð¤ðWðoðrðlðdð!

Try it:)


How it works

Hðeðlðlðoð,ð¤ðWðoðrðlðdð! # The ð character breaks the string into an array and pushes the array ["H", "e", "l", "l", "o", ",", "¤", "W", "o", "r", "l", "d", "!"].
                          # Implicitly printed to the screen (¤ is the space).

Without every other character gives you :

Hello,¤World!

<div id="noodel" code="Hðeðlðlðoð,ð¤ðWðoðrðlðdð" input="" cols="12" rows="2"></div>

<script src="https://tkellehe.github.io/noodel/noodel-latest.js"></script>
<script src="https://tkellehe.github.io/noodel/ppcg.min.js"></script>

\$\endgroup\$
2
\$\begingroup\$

Lua, 85 bytes

I decided to try making a function copying Lua answer. It's slightly shorter than the comment abuse submission.

paints= p_r_i_n_t----
print("Hello, World!")

paints( " H e l l o ,   W o r l d ! " )

Try it online!

Alternated:

pit=print--pit"el,Wrd"
pit("Hello, World!")

Try it online!

Alternator script is in my other answer.

\$\endgroup\$
2
\$\begingroup\$

shell (ksh), 58 bytes

 #e c h o   H e l l o ,   W o r l d ! #
echo Hello World!

(in interactive bash, you'd need to add '\' before the '!')

\$\endgroup\$
2
\$\begingroup\$

JavaScript (ES6), 66 bytes

/ /;alert`Hello, World!`
'a l e r t ` H e l l o ,   W o r l d ! `'

//aetHlo ol!
alert`Hello, World!`

\$\endgroup\$
2
\$\begingroup\$

Python 2, 73 bytes

print"=#0##Hello, World!"[5:] 
#portionate ""IHeeelallof,A TWookralade!?"

Try it online!

Python 2, unfortunately doesn't have that nifty trick the Python 3 answer used :( So I did the obvious comment trickery. "Try it online" code provides the halved version and execs it to make checking easier.

Halved version:

pit=0#el,Wrd"5] 
print "Hello, World!"
\$\endgroup\$
2
\$\begingroup\$

JavaScript (ES6), 65 bytes

Latecomer to the game...

01?alert`Hello, World!` : a_l_e_r_t ` H e l l o ,   W o r l d ! `

Becomes:

0?lr`el,Wrd`:alert`Hello, World!`

Obviously, neither the a_l_e_r_t function nor the lr function does exist. That's OK, because their respective code paths are never executed.

Demo

let code0 = "01?alert`Hello, World!` : a_l_e_r_t ` H e l l o ,   W o r l d ! `";
let code1 = code0.replace(/../g, s => s[0]);

console.log(code0);
eval(code0);
console.log(code1);
eval(code1);

\$\endgroup\$
2
\$\begingroup\$

C, 111 101 bytes

10 bytes saved thanks to Jasen

Full version:

//*
main(){puts("Hello, World!");}
//**// m a i n ( ) { p u t s ( " H e l l o ,   W o r l d ! " ) ; }

Every other character

/*mi({us"el,Wrd";
/*/main(){puts("Hello, World!");}
\$\endgroup\$
2
\$\begingroup\$

05AB1E, 32 bytes

"
H
e
l
l
o
,

W
o
r
l
d
!
"¶ K

Basically the "s make a string, and the ¶ K removes all newlines.

Try it online!

Version with even characters removed, 16 bytes.

"Hello, World!" 

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Actually, the even characters are removed, not the odd ones. \$\endgroup\$ – Okx Jun 27 '17 at 9:58
  • \$\begingroup\$ Just remove the first space. Then everything works out and you can save a byte in the process :) \$\endgroup\$ – Datboi Jun 27 '17 at 10:05
  • \$\begingroup\$ My bad, fixing... \$\endgroup\$ – Oliver Ni Jun 27 '17 at 15:47
2
\$\begingroup\$

Ruby, 58 bytes

$/>%< <> > ;puts a= %w( H e l l o ,\  W o r l d ! )* "#{}"

Which becomes:

$><<  pt =%(Hello, World!) #}

I borrowed the puts/assignment trick from @G B's solution

Explanation:

The first version compares the ruby global $/ which evaluates to \n to the string " <> " and does nothing with it. Then it creates an array of all the characters in "Hello, World!" and joins them by multiplying by empty string

The second pushes the return value of assignment of the string "Hello, World!" (which happens to be "Hello, World!" directly to $> which is the global for standard out.

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Befunge-93, 51 bytes

Inspired by Martin's Cardinal solution, I thought it might be fun to try and do something vertical in Befunge as well. It's a little more complicated in Befunge though.

?
"
!
d
l
r
o
W

,
o
l
l
e
H
" :
>>:v$
#^,__@@
v >

Try it online!

We start with a ?(random direction) command, which can potentially send the instruction pointer in any direction. Left and right will just wrap around back to the start, and going up will hit the v at the bottom of the program and reflect back to the start as well. So while we may bounce around for a while, we'll eventually be forced down at some point, pushing the "Hello, World!" string (in reverse) onto the stack. Which brings us to this sequence:

>>:v
 ^,__@

This is just a simple output loop that writes out the string on the stack. The only thing out of the ordinary is the extra _ (branch) when the loop terminates. But because there are only zeros on the stack at that point, the second test will safely continue on to to the right, finishing with the @ (exit) command.


Once we remove every second character, the code looks like this:

?"!dlroW ,olleH":>:$#,_@v>

Try it online!

Again we start with a ?(random direction) command, and again most directions will just end up wrapping back to the start. So eventually we're forced to go to the right, pushing "Hello, World!" (in reverse) onto the stack. Which then brings us to this sequence:

:>:$#,_@

Again this is just a loop outputting the string on the stack. It's very similar to the classic inline print loop, but we've got an extra : (dup) command, and thus require a $ (drop) to cancel it out.

\$\endgroup\$
2
\$\begingroup\$

Stax, 33 bytes

"zHzezlzlzoz,z zWzozrzlzdz!z"'z -

Run and debug it

Every other character, 17 bytes

"Hello, World!"z-

Run and debug it

In the first version, 'z - remove all zs from the string. In the second version, z by itself is empty set, and z- is noop.

\$\endgroup\$
1
\$\begingroup\$

Pyth, 31 bytes

%y1t"pHEeGlGlhoz,e QwjolrDlBdJ!

eh

\$\endgroup\$
1
\$\begingroup\$

Forth, 61 bytes

This uses the fact that \ makes the rest of the line a comment.

 \  . (   H e l l o ,   W o r l d ! )   \
 .( Hello, World! )

Full program

Remove every other byte to get:

  .( Hello, World!) \ (Hlo ol!)

Every other

\$\endgroup\$
1
\$\begingroup\$

Ruby, 63 bytes

puts a= 'Hello, World!'#; p u t s ' H e l l o ,   W o r l d ! '

Which becomes:

pt ='el,Wrd';puts'Hello, World!'                    

(Abusing comments like a lot of people do)

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Very cool, I couldn't think about any way to write it in Ruby. Here's another way to Try it online \$\endgroup\$ – Eric Duminil Jun 28 '17 at 14:42
  • \$\begingroup\$ 61 bytes (Includes Eric Duminil's test suite) \$\endgroup\$ – Value Ink Jun 30 '17 at 2:51
1
\$\begingroup\$

Japt, 32 28 bytes

Wrote this up and then noticed Oliver beat me to it with the same method in 05AB1E.

Note: The empty line after the comma contains a space character and the empty line after the "W" contains an unprintable.

`
H
e
¥
o
,

W

l
d
!
` rR

Test it

With every second character removed, it becomes (with the unprintable still present between the "W" and the "l"):

`He¥o, Wld!`r

Test it

\$\endgroup\$
1
\$\begingroup\$

Swift, 77 bytes

/**/print("Hello, World!")//  * / p r i n t ( " H e l l o ,   W o r l d ! " )

Every other character:

/*pit"el,Wrd"/ */print("Hello, World!")
\$\endgroup\$
1
\$\begingroup\$

Python 2, 63 bytes

#
print'Hello, World!'
#p r i n t ' H e l l o ,   W o r l d ! '

Try it online!

After transformation, this becomes:

#pitHlo ol!
print'Hello, World!'
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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