As a kind of part 2 to Hello, World! (Every other character), write a program such that all three of these programs print "Hello, World!": the entire program, the 1st, 3rd, 5th, etc. characters of your program, and the 2nd, 4th, 6th, etc.
If your program is:
abc
def
It should output "Hello, World!", but so should
acdf
And
b
e
No solutions with built-in "Hello, World!"s.
-
6\$\begingroup\$ Why not just change the text from "Hello, World!" to something else? \$\endgroup\$Conor O'Brien– Conor O'Brien2017年07月02日 03:32:48 +00:00Commented Jul 2, 2017 at 3:32
-
1\$\begingroup\$ I think that avoids loopholes enough and "Hello, World!" is canonical. \$\endgroup\$pommicket– pommicket2017年07月02日 03:35:03 +00:00Commented Jul 2, 2017 at 3:35
-
24\$\begingroup\$ IMO it'd be nice to see a different string than "Hello, World!" once in a while (that's just my opinion) \$\endgroup\$Conor O'Brien– Conor O'Brien2017年07月02日 03:36:03 +00:00Commented Jul 2, 2017 at 3:36
-
1\$\begingroup\$ Are whitespaces and newlines counted as characters? \$\endgroup\$0xffcourse– 0xffcourse2017年07月02日 03:58:10 +00:00Commented Jul 2, 2017 at 3:58
-
6\$\begingroup\$ @ConorO'Brien With the pleasant side effect that very few languages will have built-ins for literally every other string. \$\endgroup\$Dennis– Dennis2017年07月02日 18:14:52 +00:00Commented Jul 2, 2017 at 18:14
22 Answers 22
x86 machine code, 378 bytes
demo
PROG.COM Download and run it in MS-DOS emulator, DOSBox for example.
B3 B4 B3 02 90 B3 B3 B4 02 B3 B4 B3 02 90 B3 B2
B3 48 90 B3 B3 B2 48 B3 B2 B3 48 90 B3 CD B3 21
90 B3 B3 CD 21 B3 CD B3 21 90 B3 B2 B3 65 90 B3
B3 B2 65 B3 B2 B3 65 90 B3 CD B3 21 90 B3 B3 CD
21 B3 CD B3 21 90 B3 B2 B3 6C 90 B3 B3 B2 6C B3
B2 B3 6C 90 B3 CD B3 21 90 B3 B3 CD 21 B3 CD B3
21 90 B3 CD B3 21 90 B3 B3 CD 21 B3 CD B3 21 90
B3 B2 B3 6F 90 B3 B3 B2 6F B3 B2 B3 6F 90 B3 CD
B3 21 90 B3 B3 CD 21 B3 CD B3 21 90 B3 B2 B3 2C
90 B3 B3 B2 2C B3 B2 B3 2C 90 B3 CD B3 21 90 B3
B3 CD 21 B3 CD B3 21 90 B3 B2 B3 20 90 B3 B3 B2
20 B3 B2 B3 20 90 B3 CD B3 21 90 B3 B3 CD 21 B3
CD B3 21 90 B3 B2 B3 77 90 B3 B3 B2 77 B3 B2 B3
77 90 B3 CD B3 21 90 B3 B3 CD 21 B3 CD B3 21 90
B3 B2 B3 6F 90 B3 B3 B2 6F B3 B2 B3 6F 90 B3 CD
B3 21 90 B3 B3 CD 21 B3 CD B3 21 90 B3 B2 B3 72
90 B3 B3 B2 72 B3 B2 B3 72 90 B3 CD B3 21 90 B3
B3 CD 21 B3 CD B3 21 90 B3 B2 B3 6C 90 B3 B3 B2
6C B3 B2 B3 6C 90 B3 CD B3 21 90 B3 B3 CD 21 B3
CD B3 21 90 B3 B2 B3 64 90 B3 B3 B2 64 B3 B2 B3
64 90 B3 CD B3 21 90 B3 B3 CD 21 B3 CD B3 21 90
B3 B2 B3 21 90 B3 B3 B2 21 B3 B2 B3 21 90 B3 CD
B3 21 90 B3 B3 CD 21 B3 CD B3 21 90 B3 CD B3 20
90 B3 B3 CD 20 B3 CD B3 20 90
Also you can download LEFT.COM and RIGHT.COM
How it works and how to run
Magic
If you want to execute 0xAB opcode command with one parameter 0xCD, you write
Magic
Generator
Run code, get hex. Convert in to binary
cat prog.hex | xxd -r -p > PROG.COM
function byte2hex(byte){
var ret=byte.toString(16).toUpperCase();
return ret.length==1 ? "0"+ret : ret;
}
function str2hex(str){
var ret = [];
for(var i=0;i<str.length;i++){
ret.push(byte2hex(str.charCodeAt(i)));
}
return ret;
}
function genCode(hexArr){
var ret = [["B4","02"]];
for(var i=0;i<hexArr.length;i++){
if(hexArr[i]!=hexArr[i-1]){
ret.push(["B2",hexArr[i]]);
}
ret.push(["CD","21"]);
}
ret.push(["CD","20"]);
return ret;
}
function format(arr){
var ret=[],tmp=[];
for(var i=0;i<arr.length;i++){
tmp.push(arr[i]);
if(i%16==15 || i==arr.length-1){ret.push(tmp.join(" "));tmp=[];}
}
return ret.join("\n");
}
function magicCode(str,mode){
var ret=[];
var code=genCode(str2hex(str));
for(var i=0;i<code.length;i++){
var ab=code[i][0],cd=code[i][1],tmp;
tmp=["B3",ab,"B3",cd,"90","B3","B3",ab,cd,"B3",ab,"B3",cd,"90"];
for(var j=0;j<tmp.length;j++){
if((mode&1 && j%2==0) || (mode&2 && j%2==1)){ret.push(tmp[j])}
}
}
return ret;
}
const LEFT=1,RIGHT=2,ALL=3;
var str=prompt("string","Hello, world!");
var out = ["PROG.COM\n",format(magicCode(str,ALL)),"\n\nLEFT.COM\n",format(magicCode(str,LEFT)),"\n\nRIGHT.COM\n",format(magicCode(str,RIGHT))].join("\n");
document.write(out.replace(/\n/ig,"<br>"));
Python 3, 115 bytes
print=="partisn't";
"ran" >="partisn't" ;
print((""" "HHeelllloo,, wwoorrlldd!! """[" ) #2">"1":: 3- 1] [ 1:-1 ]))
Every odd character
pit=print;"a">"ats'";pit(" Hello, world! "[ 2>1: -1 :1])
Every even character
rn="ats'"
rn =print
rn("""Hello, world!""")#""":3 ][1- )
This could probably get a lot shorter, but I'm happy I managed to get it to work in Python. Similar to vroomfondel's answer on the first part.
Boring 93 byte answer
""""""
print("Hello, world!")
""""""#
""""
pprriinntt((""HHeelllloo,, wwoorrlldd!!""))
#"""
Every odd character
"""pit"el,wrd"
"""
""
print("Hello, world!")#"
Every even character
"""
rn(Hlo ol!)"""#""
print("Hello, world!")
""
><>, 45 bytes
! """!!ddllrrooWW oolllleeHH"!!"" >~o <> o <
Or just even characters:
"!dlroW olleH"!">o<
Or just odd characters:
!""!dlroW olleH!" ~ >o<
Try them online: original, evens, odds.
The original version pushes "!!ddllrrooWW oolllleeHH" to the stack, then the fish bounces between >~o <, which deletes a letter, prints two, deletes two, prints two, deletes two, etc. The two half-programs are fairly standard string printing programs. The tricky part was combining " and ! to toggle in and out of string mode in all three programs correctly.
Befunge-98, 43 bytes
120 x""!!ddllrrooWW ,,oolllleeHH""cckk,,@@
Only the odd ones:
10x"!dlroW ,olleH"ck,@
Only the even ones:
2 "!dlroW ,olleH"ck,@
Explanation
The full program:
120 Push 1, push 2, push 0.
x Pop 0 (y) and 2 (x) and set the instruction pointer's movement
delta to (x,y). That is, run the remainder of the code
by skipping every other cell.
"!dlroW ,olleH" Push the code points of the output.
ck, Print 13 characters from the top of the stack.
@ Terminate the program.
In the odd program, the 2 is gone, so that 10x really does nothing at all (it sets the delta to (1,0) which is the default anyway). The remainder of the program is then the same.
In the even program, we just push the 2 at the beginning which we can ignore entirely. The remainder of the program is the same as before.
Gammaplex, 46 bytes
//
EERRrrXXXX""HHeelllloo,, WWoorrlldd!!""XX
Interpreter. It may need some changes to work in modern compilers.
brainfuck, (削除) 3593 448 (削除ここまで) 299 bytes
++<>[[>>>>>>+[[-->-[[>>+>-----<<]]<--<---]]>-.>>>+.>>..+++[[.>]]<<<<.+++.------.<<-.>>>>+.>>]]<<---------------------------------[[>>>>>>>>>>>>>>>>++[[---->>--[[>>>>++>>----------<<<<]]<<----<<------]]>>--..>>>>>>++..>>>>....++++++[[..>>]]<<<<<<<<..++++++..------------..<<<<--..>>>>>>>>++..[[>>]]]]
evens:
+>[>>>[->[>+---<]-<-]>.>+>.++[>]<<++---.<.>>.>]<----------------[>>>>>>>>+[-->-[>>+>-----<<]<--<---]>-.>>>+.>>..+++[.>]<<<<.+++.------.<<-.>>>>+.[>]]
odds:
+<[>>>+[--[>>--<]<---]->>.>.+[.]<<.+.---<->>+>]<-----------------[>>>>>>>>+[-->-[>>+>-----<<]<--<---]>-.>>>+.>>..+++[.>]<<<<.+++.------.<<-.>>>>+.[>]]
UPDATE:
For my first attempt at this question, I used home-grown code for the actual Hello World section - this yielded a solution at 3593 bytes. For my second and third attempts I used pre-baked solutions for Hello World that I found on the esloangs wiki, which brought the byte-count down to 299 bytes.
The following is an un-golfed/commented version of my code:
In the following code B=Both E=Even O=Odd:
++<> current cell is now B:2 E/O:0
[[ B:step into loop E/O: No op
>>>>>> B: Move to the right to find empty space on either side E/O: No op
Hello World: (Note that brackets are doubled to prevent errors from E/O versions
+[[-->-[[>>+>-----<<]]<--<---]]>-.>>>+.>>..+++[[.>]]<<<<.+++.------.<<-.>>>>+.
>> B: Move to zero cell E/O: No op
]] B: Close Loop E/O: No op
<< B: Go back to cell with ! E/O: move to empty cell
--------------------------------- B: current cell is now zero E/O: current cell is now negative 16 or 17
[[ B: No op E/O: Step into loop
>>>>>>>>>>>>>>>> B: No op E/O: Move to the right to find empty space on either side
Hello World(Doubled):
++[[---->>--[[>>>>++>>----------<<<<]]<<----<<------]]>>--..>>>>>>++..>>>>....++++++[[..>>]]<<<<<<<<..++++++..------------..<<<<--..>>>>>>>>++..
[[>>]] B: No op E/O: Move right to first zero
]] B: No op E/O: Close loop
Mathematica, 65 bytes
PPrriinntt[[""HHeelllloo,, WWoorrlldd!!""]]Print@"Hello, World!"
It throws some warnings, prints Hello, World!, and returns Null PPrriinntt[["" HHeelllloo, Null, "" WWoorrlldd!!]]. When run as a program (not in the REPL), the return value will not be printed.
After removing the even characters:
Print["Hello, World!"]Pit"el,Wrd"
It prints Hello, World!, and returns "el,Wrd" Null Pit.
After removing the odd characters:
Print["Hello, World!"]rn@Hlo ol!
It prints Hello, World!, and returns Null ol! rn[Hlo].
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円"
-
\$\begingroup\$ Good job! In DOSBox program is not working. I don't know why com file with this code freeze system at start. \$\endgroup\$Евгений Новиков– Евгений Новиков2017年07月02日 12:28:36 +00:00Commented Jul 2, 2017 at 12:28
-
\$\begingroup\$ Test your code next time. The
8Aat 0116 should beBAinstead, and strings are terminated by$, not NULL. \$\endgroup\$Maya– Maya2018年12月22日 19:54:54 +00:00Commented Dec 22, 2018 at 19:54
Retina, 48 bytes
G |`
HHeelllloo,, WWoorrlldd!!
$_&
(.)1円t?
1ドル
Odd positions:
G|
Hello, World!
_
()1?$
Even positions:
`
Hello, World!$&
.\t
1
Explanation
The full program:
G |`
This does nothing at all. The | is not an existing configuration option. The G makes this a grep stage, but there is really nothing to be grepped and the regex is empty any, so this doesn't do anything. The purpose of this stage is to have two linefeeds in front of the main "Hello, World!" line so that one of them always survives the reduction. The reason for making this a grep stag is that we need to offset the parity of the lines, and grep stages only require a single line.
HHeelllloo,, WWoorrlldd!!
This turns the (empty) working string into the required output with each character doubled.
$_&
This does nothing. The regex tries to match a _ and a & after the end of the string which is of course impossible. We'll need those characters in the reduced version though, again to deal with vanishing linefeeds.
(.)1円t?
1ドル
Finally, we remove the duplicate characters by replacing (.)1円 with 1ドル. The t? is never used but will again be necessary in the reduced versions.
The odd program:
G|
Hello, World!
The G can't match the empty input, but that's why we have the | to allow an alternative empty match. This turns the empty working string into the desired output.
_
()1?$
This replaces underscores with ()1?$, but there are no underscores in the string, so this does nothing.
The even program:
`
Hello, World!$&
The ` just denotes an empty configuration string, so we again use the empty regex to replace the working string with the output. This time we also insert $& but that's the match itself, which is empty, of course, so it doesn't do anything.
.\t
1
This would replace any character followed by a tab with a 1, but we don't have any tabs, so this is also a no-op.
Lua, 106 bytes
----[[[[
print("Hello, World!")
--[[
---- ] ]
---- ] ]
pprriinntt((""HHeelllloo,, WWoorrlldd!!""))
----]]
Alternate 1:
--[[
rn(Hlo ol!)-[
--]]--
print("Hello, World!")--]
Alternate #2:
--[[pit"el,Wrd"
-[--
--]]print("Hello, World!")
--]
Alternator script: Try it online!
Zsh, 54 bytes
<<<Hello\ world!||eecchhoo HHeelllloo\\ wwoorrlldd!!
The main program executes the first statement successfully, so the statement after the boolean || is ignored.
For odd/even, the <<<Hello\ world! becomes either an unterminated <<heredoc or a <file provided on stdin. Either way, the || becomes |, and so whatever is output by the first command is piped into and ignored by echo.
Backhand, 36 bytes
vv^^vv""!!ddllrrooWW ,,oolllleeHH""
Every other character (in both ways):
v^v"!dlroW ,olleH"
How it works
Backhand's IP starts at the first character and bounces back and forth, skipping certain number of steps at once. The initial step size is 3, which can be adjusted with ^ (increase) and v (decrease) commands.
vv^^vv""!!ddllrrooWW ,,oolllleeHH""
v decrease step size to 2
^ increase step size to 3
v decrease step size to 2
" ! d l r o W , o l l e H " push the string
H halt and print the string
v^v"!dlroW ,olleH"
v decrease step size to 2
v decrease step size to 1
"!dlroW ,olleH" push the string
H halt and print the string
PHP, 70 bytes
"";echo'Hello, World!'.""//pprriinntt''HHeelllloo,, WWoorrlldd!!''
;;
Odd characters:
";coHlo ol!."/print'Hello, World!';
Even characters:
"eh'el,Wrd'"/print'Hello, World!'
;
Haskell, 92 bytes
{---- }
mmaaiinn==ppuuttSSttrr""HHeelllloo,, WWoorrlldd!!""----}main=putStr"Hello, World!"
Try it online! It looks like the comment abuse is too much for the syntax highlighting to handle. {- ... -} is an inline or multi-line comment, whereas -- starts a line comment.
Odd characters:
{--}
main=putStr"Hello, World!"--mi=uSrHlo ol!
Even characters:
--
main=putStr"Hello, World!"--}anptt"el,Wrd"
Perl 5 + -p0513, 56 bytes
$ _ =$ _ =q";HHeelllloo,, WWoorrlldd!!";;;s/;|.\K.//g;;
Every odd byte
$_= q;Hello, World!;;/|\./;
Every even byte
$_="Hello, World!";s;.K/g;
LOGO, 71 bytes
;;\\
pr[Hello, World!]er "|
pprr[[HHeelllloo,, WWoorrlldd!!]]
;;\\
|
(the program have a trailing newline)
Two removed versions:
;\p[el,Wrd]r"
pr[Hello, World!]
;\|
and
;\
rHlo ol!e |
pr[Hello, World!];\
(the program have a trailing newline)
For an explanation what pr and er do, see this post. In this case, er is feed with a word determine procedure name.
The \ is escape character in Logo, which will escape the newline after the end of the comment, therefore make the second line (rHlo ol!e |) of second removed program a comment.
Javascript,68 bytes
//**
alert`Hello, World`//**//aalleerrtt``HHeelllloo,, WWoorrlldd``
Odd Characters:
/*aetHlo ol`/*/alert`Hello, World`
Even Characters:
/*
lr`el,Wrd/*/alert`Hello, World`
Modified version of my answer on the other one.
-
1\$\begingroup\$ Why do you need the space at the start? \$\endgroup\$CalculatorFeline– CalculatorFeline2017年07月02日 16:08:16 +00:00Commented Jul 2, 2017 at 16:08
-
\$\begingroup\$ This doesn't seem to be syntactically valid when you take the 1st, 3rd, etc. characters. Also, it's missing a
linHHeelllloo. \$\endgroup\$Arnauld– Arnauld2017年07月03日 00:16:21 +00:00Commented Jul 3, 2017 at 0:16 -
\$\begingroup\$ @CalculatorFeline,@Arnauld fixed \$\endgroup\$SuperStormer– SuperStormer2017年07月04日 22:42:11 +00:00Commented Jul 4, 2017 at 22:42
BotEngine, (削除) 180 (削除ここまで) 178 bytes
Based on my answer to this question.
vv
v <
eHH
eee
ell
ell
eoo
e,,
e
eWW
eoo
err
ell
edd
e!!
>>P e e e e e e e e e e e e e P
Odd characters (note the multiple trailing spaces on the last line):
v
v<
eH
ee
el
el
eo
e,
e
eW
eo
er
el
ed
e!
>P
Even characters:
v H e l l o , W o r l d !
> e e e e e e e e e e e e eP
Python 3.8 (pre-release), 71 bytes
Original code :
#
exec(a:= """
pprriinntt((''HHeelllloo,, WWoorrlldd!!''))##"""[::2])
Every Other char (Odd chars) :
#ee(: "
print('Hello, World!')#"":2)
Other every other char (Even chars)
xca=""
print('Hello, World!')#"[:]
Runic Enchantments, 52 bytes
L @""H!edlllroo,W W,oorlllde!H"" ~@"!dlroW ,olleH"
Runic is not usually very good at dealing with radiation as having flow control characters removed at random makes tracing execution a huge pain, but predictable radiation like every other character? Easy, we just encode two programs that are reversed of each other and interleaved, then tack on a third copy for the base execution and control which one is executed with a single character. In program 2, the third copy is garbage that's never seen, and in program 3 it retains the quotes, allowing it to be popped without printing it.
Program 1 only executes this part:
L @"!dlroW ,olleH"
Program 2 only executes this part:
" H e l l o , W o r l d ! " @
Like this:
"Hello, World!" @!lo olH
Program 3 only executes this part:
L @ " ! d l r o W , o l l e H " ~ " d r W , l e "
Like this:
L@"!dlroW ,olleH"~"drW,le"
The "drW,le" portion is executed, but the ~ immediately pops it off the stack, preserving the desired output.
Naively it would appear that a conversion of the><> answer would result in a shorter program, weighing in at 45 bytes:
! ```!!ddllrrooWW oolllleeHH`!!`` R~$ LR $ L
However, Runic has one limitation that><> does not have: a maximum stack size of 10 + IP's mana (which is initially 10). And !!ddllrrooWW oolllleeHH contains 24 characters, causing the IP to bleed mana until it expires just before executing the R command, resulting in no output for the base program.
Vyxal, 33 bytes
\` ð`HHeelllloo,, WWoorrlldd!!`√
`ðHello, World!`
\ `Hello, World!√
These all exploit the bug/feature that single special characters in strings are ignored during parsing.
\` # Push a backtick (useless)
ð # Push a space (useless)
`HHeelllloo,, WWoorrlldd!!` # String literal
√ # Get every second character
` Hello, World!` # String literal
ð # Single non-ascii character (ignored)
\ # Push a space
`Hello, World! # String literal (unterminated)
√ # Single non-ascii character - ignored
Explore related questions
See similar questions with these tags.