146
\$\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\$
15
  • 3
    \$\begingroup\$ Can there be whitespace around the output? \$\endgroup\$ Commented Jun 26, 2017 at 20:20
  • 24
    \$\begingroup\$ Amazing first post! \$\endgroup\$
    – Adám
    Commented Jun 26, 2017 at 22:49
  • 13
    \$\begingroup\$ Seeing all the answers with "HHeelllloo" reminded me of speaking Whale. \$\endgroup\$ Commented Jun 27, 2017 at 4:19
  • 36
    \$\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\$ Commented Jun 27, 2017 at 7:15
  • 7
    \$\begingroup\$ "No solutions with fewer than 2 characters." Amazing. \$\endgroup\$
    – Rob Grant
    Commented Jun 28, 2017 at 9:10

92 Answers 92

5
\$\begingroup\$

Javascript, 73 bytes

a ='a0l0e0r0t0`0H0e0l0l0o0,0 0W0o0r0l0d0!0`0/0/';eval(a.replace(/0/g,''))

Constructs a string a with the content a0l0e0r0t0`0H0e0l0l0o0,0 0W0o0r0l0d0!0`0/0/, then removes all 0's to give alert`Hello, World!`//, which is eval'd.

Taking every other character of the program gives

a=alert`Hello, World!`//;vlarpae//,')

which alerts Hello, World using template string syntax the same way as what was eval'd in the full program, then stores the result of the call in a and includes the insightful comment //;vlarpae//,').

\$\endgroup\$
5
\$\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\$

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\$
4
\$\begingroup\$

Jelly, 32 bytes

 2“HHeelllloo,,  WWoorrlldd!! ”m

Try it online!

-2 bytes thanks to @totallyhuman's answer

Now mine is a dupe >.<

Go upvote that one too :D

\$\endgroup\$
0
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\$

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\$
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\$
0
4
\$\begingroup\$

C (MSVC), 60 bytes

As Stack Exchange doesn't like me posting null bytes, here is an xxd -g1:

00000000: 6d 00 61 00 69 00 6e 00 28 00 29 00 7b 00 70 00  m.a.i.n.(.).{.p.
00000010: 75 00 74 00 73 00 28 00 22 00 48 00 65 00 6c 00  u.t.s.(.".H.e.l.
00000020: 6c 00 6f 00 2c 00 20 00 57 00 6f 00 72 00 6c 00  l.o.,. .W.o.r.l.
00000030: 64 00 21 00 22 00 29 00 3b 00 7d 00              d.!.".).;.}.

For an infinitely less readable textual version, with 0 representing a raw 0x00 byte:

m0a0i0n0(0)0{0p0u0t0s0(0"0H0e0l0l0o0,0 0W0o0r0l0d0!0"0)0;0}0

Explanation

I'll see your small brain comment abuse and raise you one cosmic brain text encoding abuse. 😏

Unlike GCC and Clang, MSVC supports files encoded in UTF-16.

That is what we abuse. MSVC will """incorrectly""" auto-detect this file as-is as UTF-16LE, causing it to """incorrectly""" read it as this, encoded in UTF-16LE:

main(){puts("Hello, World!");}

If we remove every other character (a.k.a. removing every 00 byte), it treats it as UTF-8 (well, technically, the current SBCS/MBCS codepage), and it is compiled as you would probably expect, as the codepoints are now the same as they were in UTF-16LE:

main(){puts("Hello, World!");}

You may say "but this is UTF-16, it should be mi({us"el,Wrd";", but I say that U+0000 is a valid UTF-8 codepoint, and it just looks like UTF-16 by """coincidence""".

\$\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\$

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\$
0
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\$
0
3
\$\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\$
3
\$\begingroup\$

Bash, 36 bytes

e\c\h\o  "H"e"l"l"o"," "W"o"r"l"d"!"

With every other character removed:

echo Hello, World!

Invalid escape sequences will be ignored, so \c\h\o makes no difference from cho.

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

Rust, 85 bytes

| | /**/print!("Hello, World!")//___*//_p_r_i_n_t_!_(_"_H_e_l_l_o_,_ _W_o_r_l_d_!_"_)

Try it online

Every other character:

||/*pit(Hlo ol!)/_*/print!("Hello, World!")

Try it online

Full program, 113 bytes

/**/fn main(){print!("Hello, World!")}//__*//_f_n_ _m_a_i_n_(_)_{_p_r_i_n_t_!_(_"_H_e_l_l_o_,_ _W_o_r_l_d_!_"_)_}

Try it online

Every other character:

/*f an)pit(Hlo ol!)/_*/fn main(){print!("Hello, World!")}

Try it online

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

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, 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\$
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\$

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\$
0
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\$
3
  • \$\begingroup\$ Actually, the even characters are removed, not the odd ones. \$\endgroup\$
    – Okx
    Commented Jun 27, 2017 at 9:58
  • \$\begingroup\$ Just remove the first space. Then everything works out and you can save a byte in the process :) \$\endgroup\$
    – Datboi
    Commented Jun 27, 2017 at 10:05
  • \$\begingroup\$ My bad, fixing... \$\endgroup\$
    – Oliver Ni
    Commented Jun 27, 2017 at 15:47
2
\$\begingroup\$

Ly, 38 bytes

"""!!ddllrrooWW  ,,oolllleeHH" [po ]

Try it online!

Explanation:

"""HHeelllloo,,  WWoorrlldd!!" [po ]

"""           # open a string literal, close it, then open one again
   ...        # push "HHeelloo,,  WWoorrlldd!!" backwards
      "       # close string literal
         [    # until the stack is empty
          po  # pop off the stack, then output
            ] # end loop

Every other character:

"!dlroW ,olleH"[o]

Try it online!

Explanation:

"!dlroW ,olleH"[o]

"!dlroW ,olleH"    # push "Hello, World!" backwards
               [o] # output until the stack is empty
\$\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\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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