13
\$\begingroup\$

Write a program that will output its own source code when run, and nothing else. Seems easy, right?

The catch is that when the source code is reversed, your program must output "Hello, World!" exactly, without the quotes.

This is code-golf, so lowest byte count wins.

Edit: Your quine must be a proper quine.

\$\endgroup\$
  • 2
    \$\begingroup\$ This is one of the things to avoid when writing challenges \$\endgroup\$ – Mego Apr 4 '16 at 5:24
  • 4
    \$\begingroup\$ @Mego to be fair, this isn't the kind of generalised quine I'm talking about there, since this isn't "print function X of your source code". That said it probably won't play out very differently from the other "when you reverse the program..." challenges we have. \$\endgroup\$ – Martin Ender Apr 4 '16 at 6:44
  • 1
    \$\begingroup\$ @MartinBüttner It's similar enough to the challenges you described that I feel the same reasoning applies. \$\endgroup\$ – Mego Apr 4 '16 at 6:48

11 Answers 11

8
\$\begingroup\$

Y, 19 bytes

Upxp"!dlroW ,olleH"

U captures a string with U at the beginning until the next U is met, in this case, the source code. p prints the item, and x is a termination link. When reversed, this looks like:

"Hello, World!"pxpU

This captures the string and prints it with p, again terminating the program with x.

Try it here!

\$\endgroup\$
  • 3
    \$\begingroup\$ U pushes U implicitly? ಠ_ಠ \$\endgroup\$ – Sp3000 Apr 4 '16 at 14:30
  • 1
    \$\begingroup\$ @Sp3000 It's made so that the program can modify itself. More technically, the U command is the "chain link capture command". It's not made for quining, but that won't stop me from using it for quining. :P \$\endgroup\$ – Conor O'Brien Apr 4 '16 at 14:31
  • 1
    \$\begingroup\$ I'm not sure this counts as a proper quine. Our definition requires a section of the program which encodes a different part of the program, but U... just encodes U.... \$\endgroup\$ – Dennis Oct 16 '16 at 18:30
  • \$\begingroup\$ @Dennis define encodes? I thought the p bit did the output, which was part of the encoding. \$\endgroup\$ – Conor O'Brien Oct 16 '16 at 18:36
  • 1
    \$\begingroup\$ I took encode as something that generates the output, so p and x wouldn't count. For example, the shortest SMBF quine is still considered a cheating quine, even though the code has to print it. Might be worth asking Martin for clarification though. \$\endgroup\$ – Dennis Oct 16 '16 at 18:43
20
\$\begingroup\$

JavaScript (ES6), 42 38 bytes

f=_=>/\//g&&"f="+f||"!dlroW ,olleH">=_

Reversed

_=>"Hello, World!"||f+"=f"&&g//\/>=_=f

Explanation

When reversed, it becomes an anonymous function that returns the string Hello, World!.

The regex /\//g becomes a comment when it is reversed, which allows the syntactically invalid >=_=f to be commented out in the reversed code.

\$\endgroup\$
  • \$\begingroup\$ This is very smart. :) I wouldn't have thought of this. \$\endgroup\$ – ericw31415 Apr 4 '16 at 10:21
  • 3
    \$\begingroup\$ +1 just for the shifty-eyes: =_=. \$\endgroup\$ – Darrel Hoffman Apr 4 '16 at 16:45
6
\$\begingroup\$

JavaScript (ES6), 71 bytes

trela=a=>alert("trela="+trela+"\ntrela\n``")//
`!dlroW ,olleH`
trela
``

How it Works:

Line 1 defines function trela that when run outputs the program's source code. Line 2 is an unassigned string, does nothing. Lines 3 and 4 call trela abusing the template string syntax.

Reversed:

``
alert
`Hello, World!`
//)"``n\alertn\"+alert+"=alert"(trela>=a=alert

How it Works:

Line 1 is an unassigned string, does nothing. Lines 2 and 3 abuse the template string syntax to print Hello, World!. Line 4 is a comment.

\$\endgroup\$
  • \$\begingroup\$ I see that JavaScript seems to have some leeway with its syntax. Well, at least more than Python does. That's nice. \$\endgroup\$ – R. Kap Apr 4 '16 at 2:27
  • \$\begingroup\$ Is the empty `` unnecessary? \$\endgroup\$ – Rɪᴋᴇʀ Apr 4 '16 at 3:00
  • \$\begingroup\$ @EasterlyIrk No, it is necessary to call the trela function (which prints the quine) in the forwards version \$\endgroup\$ – jrich Apr 4 '16 at 3:03
  • \$\begingroup\$ @jrich oh, I didn't realize that the `` was the arguments. Cool. \$\endgroup\$ – Rɪᴋᴇʀ Apr 4 '16 at 3:56
  • 2
    \$\begingroup\$ I feel like I should be getting used to see trela around here... >_> \$\endgroup\$ – Conor O'Brien Apr 4 '16 at 13:23
3
\$\begingroup\$

GolfScript, 33 bytes

Forwards

{`".;1$~]"}"!dlroW ,olleH".;1$~]

Try it online!

Backwards


]~$1;."Hello, World!"}"]~$1;."`{

Try it online!

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

GolfScript, 29 28 bytes

"`'.~]'#\"!dlroW ,olleH".~]

It has one trailing newline. Try it here.

Reversed:


]~."Hello, World!"\#']~.'`"

Try it here.

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

RETURN, 94 bytes

"34¤¤,,,,,,,,,,,,,% 'H'e'l'l'o',' 'w'o'r'l'd'!'"34¤¤,,,,,,,,,,,,,% 'H'e'l'l'o',' 'w'o'r'l'd'!'

Reversed:

'!'d'l'r'o'w' ','o'l'l'e'H' %,,,,,,,,,,,,,¤¤43"'!'d'l'r'o'w' ','o'l'l'e'H' %,,,,,,,,,,,,,¤¤43"

Try it here.

Outputs to STDOUT. Until I find a better quine framework, this will have to do for now.

Explanation

"34¤¤,,,,,,,,,,,,,% 'H'e'l'l'o',' 'w'o'r'l'd'!'"

This contains the quine string. In reverse, this is pushed to the stack but not outputted.

34¤¤,,,,,,,,,,,,,

This pushes a quote char to the stack and outputs the result twice until there is nothing left to output. In reverse, this will print the charcodes already on the stack.

% 'H'e'l'l'o',' 'w'o'r'l'd'!'

This one pops the top stack item (in reverse, this would pop a space char) and pushes a series of charcodes to the stack (in reverse, these charcodes would later be printed by the series of ,'s).

\$\endgroup\$
  • \$\begingroup\$ I can't tell if this is really crazy RETURN code, or just normal RETURN code ._. \$\endgroup\$ – Downgoat Apr 5 '16 at 5:43
  • \$\begingroup\$ Both ;_; I cry everytiem \$\endgroup\$ – Mama Fun Roll Apr 5 '16 at 5:44
2
\$\begingroup\$

Fission 2, 42 bytes

Shameless adaptation of a excellent quine by @MartinBüttner in this answer

'!+O!'!d'!l'!r'!o'!W'! '!,'!o'!!l'!e'!H'R"

Try it online

And reversed

"R'H!'e!'l!!'o!',!' !'W!'o!'r!'l!'d!'!O+!'

Try it online

In the quine version the atom starts at the R heading right. The "starts print mode which wraps to the next " (itself). This prints everything out except the ". '!+ set the atom to char ". O prints it out and destroys the atom ending the program.

The reversed version starts at the R again and for each character in Hello, World set the atom and prints ! it out without destroying the atom. For the final character ! the print O destroys the atom.

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

Javascript ES6, 55 bytes

$=_=>`$=${$};$()//"!dlroW ,olleH"`;$()//"!dlroW ,olleH"

Quite simple, really.

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

Python 2, 131 bytes

Forward:

print(lambda x:x+repr(x)+")#'!dlroW ,olleH' tnirp")('print(lambda x:x+repr(x)+")#\'!dlroW ,olleH\' tnirp")(')#'!dlroW ,olleH' tnirp

Reverse:

print 'Hello, World!'#)'()"print '\Hello, World!'\#)"+)x(rper+x:x adbmal(tnirp'()"print 'Hello, World!'#)"+)x(rper+x:x adbmal(tnirp

The first half is a one-line quine, followed by a # to form a comment separating the first half from the simpler second half.

\$\endgroup\$
  • \$\begingroup\$ Adapting this Python quine gives _='_=%r;print _%%_#"!dlroW ,olleH"tnirp';print _%_#"!dlroW ,olleH"tnirp \$\endgroup\$ – Sp3000 Apr 4 '16 at 10:42
0
\$\begingroup\$

C, 108 bytes

char*s="char*s=%c%s%c;main(){printf(s,34,s,34);}";main(){printf(s,34,s,34);}//};)"!dlroW ,olleH"(stup{)(niam
\$\endgroup\$
  • \$\begingroup\$ C99 I assume? \$\endgroup\$ – Neil Apr 27 '16 at 13:05
0
\$\begingroup\$

Python 2, 70 bytes

_='_=%r;print _%%_#"dlroW ,olleH"tnirp';print _%_#"!dlroW ,olleH"tnirp
\$\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.