16
\$\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\$
3
  • 3
    \$\begingroup\$ This is one of the things to avoid when writing challenges \$\endgroup\$
    – user45941
    Commented Apr 4, 2016 at 5:24
  • 6
    \$\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\$ Commented Apr 4, 2016 at 6:44
  • 1
    \$\begingroup\$ @MartinBüttner It's similar enough to the challenges you described that I feel the same reasoning applies. \$\endgroup\$
    – user45941
    Commented Apr 4, 2016 at 6:48

13 Answers 13

22
\$\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\$
2
  • \$\begingroup\$ This is very smart. :) I wouldn't have thought of this. \$\endgroup\$
    – ericw31415
    Commented Apr 4, 2016 at 10:21
  • 5
    \$\begingroup\$ +1 just for the shifty-eyes: =_=. \$\endgroup\$ Commented Apr 4, 2016 at 16:45
9
\$\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\$
5
  • 4
    \$\begingroup\$ U pushes U implicitly? ಠ_ಠ \$\endgroup\$
    – Sp3000
    Commented Apr 4, 2016 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\$ Commented Apr 4, 2016 at 14:31
  • 2
    \$\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
    Commented Oct 16, 2016 at 18:30
  • \$\begingroup\$ @Dennis define encodes? I thought the p bit did the output, which was part of the encoding. \$\endgroup\$ Commented Oct 16, 2016 at 18:36
  • 2
    \$\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
    Commented Oct 16, 2016 at 18:43
7
\$\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\$
5
  • \$\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
    Commented Apr 4, 2016 at 2:27
  • \$\begingroup\$ Is the empty `` unnecessary? \$\endgroup\$
    – Riker
    Commented Apr 4, 2016 at 3:00
  • \$\begingroup\$ @EasterlyIrk No, it is necessary to call the trela function (which prints the quine) in the forwards version \$\endgroup\$
    – jrich
    Commented Apr 4, 2016 at 3:03
  • \$\begingroup\$ @jrich oh, I didn't realize that the `` was the arguments. Cool. \$\endgroup\$
    – Riker
    Commented Apr 4, 2016 at 3:56
  • 2
    \$\begingroup\$ I feel like I should be getting used to see trela around here... >_> \$\endgroup\$ Commented Apr 4, 2016 at 13:23
4
\$\begingroup\$

GolfScript, 33 bytes

Forwards

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

Try it online!

Backwards


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

Try it online!

\$\endgroup\$
4
\$\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\$
2
  • \$\begingroup\$ I can't tell if this is really crazy RETURN code, or just normal RETURN code ._. \$\endgroup\$
    – Downgoat
    Commented Apr 5, 2016 at 5:43
  • \$\begingroup\$ Both ;_; I cry everytiem \$\endgroup\$ Commented Apr 5, 2016 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\$
1
\$\begingroup\$

Python 2, 60 bytes / Python 3.8 (pre-release), 64 bytes

Python 2: (60 bytes)

  • Normal :
a="print'a=%r;exec a#\\42'%a#'!dlroW ,olleH'tnirp;";exec a#"
  • Reversed:
"#a cexe;";print'Hello, World!'#a%'24\\#a cexe;r%=a'tnirp"=a

Try it online!

Python 3: (64 bytes)

  • Normal:
exec(a:="print('exec(a:=%r)#\\42'%a)#)'!dlroW ,olleH'(tnirp;")#"
  • Reversed:
"#)";print('Hello, World!')#)a%'24\\#)r%=:a(cexe'(tnirp"=:a(cexe

Try it online!

Explanation :

  • I used a modification of the quine a="print'a=%r;exec a'%a";exec a adding some comments # to isolate the "hello world" part.
  • I also used \\42 to get the char " without breaking my strings or having some slashes \ at the printing

  • Thanks to the previous comments and a few well placed quotes, the reversed program is very straight forward
\$\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\$
1
  • \$\begingroup\$ Adapting this Python quine gives _='_=%r;print _%%_#"!dlroW ,olleH"tnirp';print _%_#"!dlroW ,olleH"tnirp \$\endgroup\$
    – Sp3000
    Commented Apr 4, 2016 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\$
1
  • \$\begingroup\$ C99 I assume? \$\endgroup\$
    – Neil
    Commented Apr 27, 2016 at 13:05
0
\$\begingroup\$

Python 2, 70 bytes

_='_=%r;print _%%_#"dlroW ,olleH"tnirp';print _%_#"!dlroW ,olleH"tnirp
\$\endgroup\$
0
\$\begingroup\$

><> (Fish), 25 bytes

"r00g>o<o>'Hello, World!'

Try it

Reversed

'!dlroW ,olleH'>o<o>g00r"

Try it

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