137
\$\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
  • 2
    \$\begingroup\$ Can there be whitespace around the output? \$\endgroup\$ – vroomfondel Jun 26 '17 at 20:20
  • 21
    \$\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
  • 34
    \$\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\$ – Rob Grant Jun 28 '17 at 9:10

81 Answers 81

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

Java 8, 123 122 bytes

Here is the full code

/**/()->System.out.print("Hello, World!")//_*_/_(_)_-_>_S_y_s_t_e_m_._o_u_t_._p_r_i_n_t_(_"_H_e_l_l_o_,_ _W_o_r_l_d_!_"_)_

Try it online here


Here is the 'every other' code

/*(-Sse.u.rn(Hlo ol!)/*/()->System.out.print("Hello, World!")

Try it online here

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

LOGO, 63 bytes

 ;p r [ H e l l o ,   W o r l d ! ] e r [
 pr[Hello, World!] ;]

With characters at even index removed

 pr[Hello, World!]er[ rHlo ol! ]

Explanation:

; starts an inline comment in Logo. Therefore the first program is equivalent to

 pr[Hello, World!] 

which obviously print "Hello, World!".

In the second program, a function er is invoked. er stands for erase, receive a list in a suitable format as input, and erase some contents specified in the list (the details does not matter). The key point is that er does not return anything, so it is a shorter alternative to ignore, which ignores its contents.

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

x86 machine code, 73 bytes

Inspired by Евгений Новиков's solution, I thought it should be doable with less tricks, i.e., just jumping around to otherwise "disjoint" codes for all three variants. I'm still trying with a smart variant that uses lodsb; lodsb as central point (so only one string constant is needed for all variants)

EB 14 00 00 8A 8A 17 16 01 01 B4 B4 09 09 CD CD
21 21 CD CD 20 20 8A 1f 01 B4 09 CD 21 CD 20 48
65 6c 6c 6f 2c 20 57 6f 72 6c 64 21 00 48 48 65
65 6c 6c 6c 6c 6f 6f 2c 2c 20 20 57 57 6f 6f 72
72 6c 6c 64 64 21 21 00 00

If I recall correctly from my childhood days, COM's tiny model starts with DS=CS=SS and the code is loaded beginning from CS:0100h. I do not assume it is guaranteed that the code is loaded into a zeroed memory block (if it were guaranteed, I could drop two bytes).

Disassembly of the long code should be

  JMP *+14h
  ; 20 irrelevant bytes
  MOV DX,message
  MOV AH,09h
  INT 21h;  print string pointed to by DS:DX
  INT 20h;  exit program
message:
  DB "Hello, World!\0"
  DB "HHeelllloo,,  WWoorrlldd!!\0\0"

Disassembly of odd code

  JMP *+00h
  MOV DX,message
  MOV AH,09h
  INT 21h;  print string pointed to by DS:DX
  INT 20h;  exit program
  ; some irrelevant bytes
message:
  DB "Hello, World!\0"

Disassembly of even code:

  ADC AL,00h
  MOV DX,message
  MOV AH,09h
  INT 21h;  print string pointed to by DS:DX
  INT 20h;  exit program
  ; some irrelevant bytes
message:
  DB "Hello, World!\0"
\$\endgroup\$
1
\$\begingroup\$

Powershell, 46 bytes

Full version:

#
"Hello, World"
#" H e l l o ,   W o r l d " 

Try it online here

Every other character

#"el,Wrd
"Hello, World"

Try it online here

\$\endgroup\$
1
\$\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\$
1
\$\begingroup\$

Cubically, 155 bytes

R U + 4 3 2 @ 6 + 5 0 - 4 @ 6 + 3 - 4 @ 6 @ 6 + 1 - 0 0 @ 6 - 3 3 1 @ 6 - 0 0 @ 6 + 4 1 1 0 @ 6 + 0 0 0 0 @ 6 + 1 - 0 0 @ 6 - 0 @ 6 - 2 + 4 @ 6 - 3 3 1 @ 6

The new parser strips out all spaces ¯\_(ツ)_/¯ so either way the code is interpreted like this:

RU+432@6+50-4@6+3-4@6@6+1-00@6-331@6-00@6+4110@6+0000@6+1-00@6-0@6-2+4@6-331@6

Try it online! (spaced)

Try it online! (every other character)

\$\endgroup\$
1
\$\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\$
1
\$\begingroup\$

MAWP, 169 bytes

9/8/W/;/5/5/W/4/W/1/M/;/9/3/W/4/W/;/9/3/W/4/W/;/9/4/W/1/M/3/W/;/5/8/W/4/M/;/8/4/W/;/9/8/M/5/W/2/M/;/9/4/W/1/M/3/W/;/9/9/M/1/M/6/W/;/9/3/W/4/W/;/5/5/W/4/W/;/9/2/M/3/W/;/.

Try it!

Code with every other character removed:

98W;55W4W1M;93W4W;93W4W;94W1M3W;58W4M;84W;98M5W2M;94W1M3W;99M1M6W;93W4W;55W4W;92M3W;.

Try It!

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

Java (JDK), 123 bytes

/*/ */System.out.print("Hello, world!"); ///\ u 0 0 0 d S y s t e m . o u t . p r i n t ( " H e l l o_,_ _w o r l d ! " ) ;

Try it online!

This isn't a full program - you'll have to paste it into a method somewhere.


Every other character, 62 bytes

//*Sse.u.rn(Hlo ol!) /\u000dSystem.out.print("Hello, world!");

Try it online!

It takes advantage of the way the compiler turns unicode escapes into their corresponding characters.

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

Julia, 59 bytes

".H.e.l.l.o. .W.o.r.l.d.!\" | > p r i n t #"[2:2:24]|>print

Shortened version :

"Hello World!"|>print#[::4|pit

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Stacked, 60 bytes

'Hello, World!'  '''H e l l o ,   W o r l d !''' @oouutt out

Try it online!

I generated the second code with this snippet. Second code is:

'el,Wrd' 'Hello, World!' out u

The first one generates two strings, Hello, World! and 'H e l l o , W o r l d !'. The latter is saved to variable oouutt, and the former is printed.

The second one generates two strings, outputs the second, then errors after encountering u.

\$\endgroup\$
0
\$\begingroup\$

dc, 52 bytes

0#[#H#e#l#l#o#,# #W#o#r#l#d#!#]#P#q
[Hello, World!]P

Try it online!

The first line puts a zero on the stack, and then the rest of the line is commented out. The second line prints 'Hello, World!'

With every even character removed, all the #s in the first line disappear, and the line is no longer commented out. It then prints 'Hello, World!' and quits before hitting the second line.

\$\endgroup\$
0
\$\begingroup\$

Brainfuck 156 bytes

-
-
<
-
<
<
+
[
+
[
<
+
>
-
-
-
>
-
>
-
>
-
<
<
<
]
>
]
<
<
-
-
.
<
+
+
+
+
+
+
.
<
<
-
.
.
<
<
.
<
+
.
>
>
.
>
>
.
<
<
<
.
+
+
+
.
>
>
.
>
>
-
.
<
<
<
+
.
\$\endgroup\$
0
\$\begingroup\$

BotEngine, 91 bytes

 vv 
 v < 
 eHH 
 eee 
 ell 
 ell 
 eoo 
 e,, 
 e   
 eWW 
 eoo 
 err 
 ell 
 edd 
 e!! 
 P
\$\endgroup\$
0
\$\begingroup\$

Keg, 47 39 35 bytes

`1H1%1;1,1 1c1¡1;1!1`_`H%;, c¡;!` $

Try it online!

And, unless I've somehow misread the challenge, the program with alternating characters:

-hd, 18 bytes

`H%;, c¡;!``%,c;`$

Try it online!

\$\endgroup\$
0
\$\begingroup\$

05AB1E, 25 bytes

”Ÿ™,‚ï!”,””q”
Ÿ
™
,
‚
ï
!

Try it online!

”Ÿ™,‚ï!”,””q”Ÿ™,‚ï!  # full program (newlines aren't important here)
        ,            # output...
”Ÿ™,‚ï!”             # "Hello, World!"
         ””          # push empty string
           q         # exit program
            ”Ÿ™,‚ï!  # push "\nŸ\n™\n,\n‚\nï\n!" (never executed)

Every other character, 13 bytes

”™‚!,””Ÿ™,‚ï!

Try it online!

”™‚!,””Ÿ™,‚ï!  # full program
”™‚!,”         # push "Situation!,"
      ”Ÿ™,‚ï!  # push "Hello, World!"
               # implicit output
\$\endgroup\$
0
\$\begingroup\$

Zsh, 46 bytes

<<< Hello,\ World!
#H#e#l#l#o#,# #W#o#r#l#d#!

Try it online!

Tyi nie

Optimal (I think) would be e\c\h\o \H\e\l\l\o\,\ \W\o\r\l\d\!, but this is more interesting.

Explanation: both <<<string and <<anything(newline)string will print string

\$\endgroup\$
0
\$\begingroup\$

AWK, 87 bytes

BEGIN {print"Hello, World!" }
B#E#G#I#N# #{#p#r#i#n#t#"#H#e#l#l#o#,# #W#o#r#l#d#!#"# #}

Try it online!

The resulting code is:

BGN{rn"el,Wrd"}BEGIN {print"Hello, World!" }

Because the BGN variable is false (not assigned), the first part of the code has no effect.

dc, 49 bytes

[Hello, World!]p #[[HHeelllloo,,  WWoorrlldd!!]]p

Try it online!

\$\endgroup\$
1 2
3

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.