Double-duty quine: quine!dlroW ,olleH

13

1

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.

ericw31415

Posted 10 years ago

Reputation: 2 229

4@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. – Martin Ender – 10 years ago

1@MartinBüttner It's similar enough to the challenges you described that I feel the same reasoning applies. – Mego – 10 years ago

Answers

8

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!

Conor O'Brien

Posted 10 years ago

Reputation: 36 228

4U pushes U implicitly? ಠ_ಠ – Sp3000 – 10 years ago

1@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 – Conor O'Brien – 10 years ago

1

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

– Dennis – 9 years ago

@Dennis define encodes? I thought the p bit did the output, which was part of the encoding. – Conor O'Brien – 9 years ago

1I 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. – Dennis – 9 years ago

20

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.

user81655

Posted 10 years ago

Reputation: 10 181

This is very smart. :) I wouldn't have thought of this. – ericw31415 – 10 years ago

3+1 just for the shifty-eyes: =_=. – Darrel Hoffman – 10 years ago

6

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.

jrich

Posted 10 years ago

Reputation: 3 898

I see that JavaScript seems to have some leeway with its syntax. Well, at least more than Python does. That's nice. – R. Kap – 10 years ago

Is the empty \`` unnecessary? – Rɪᴋᴇʀ – 10 years ago

@EasterlyIrk No, it is necessary to call the trela function (which prints the quine) in the forwards version – jrich – 10 years ago

@jrich oh, I didn't realize that the \`` was the arguments. Cool. – Rɪᴋᴇʀ – 10 years ago

2I feel like I should be getting used to see trela around here... >_> – Conor O'Brien – 10 years ago

3

GolfScript, 33 bytes

Forwards

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

Try it online!

Backwards


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

Try it online!

Dennis

Posted 10 years ago

Reputation: 196 637

3

GolfScript, 29 28 bytes

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

It has one trailing newline. Try it here.

Reversed:

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

Try it here.

jimmy23013

Posted 10 years ago

Reputation: 34 042

2

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).

Mama Fun Roll

Posted 10 years ago

Reputation: 7 234

I can't tell if this is really crazy RETURN code, or just normal RETURN code ._. – Downgoat – 10 years ago

Both ;_; I cry everytiem – Mama Fun Roll – 10 years ago

2

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.

MickyT

Posted 10 years ago

Reputation: 11 735

1

Javascript ES6, 55 bytes

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

Quite simple, really.

Mama Fun Roll

Posted 10 years ago

Reputation: 7 234

0

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.

csvoss

Posted 10 years ago

Reputation: 9

Adapting this Python quine gives _='_=%r;print _%%_#"!dlroW ,olleH"tnirp';print _%_#"!dlroW ,olleH"tnirp

– Sp3000 – 10 years ago

0

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

mIllIbyte

Posted 10 years ago

Reputation: 1 129

C99 I assume? – Neil – 10 years ago

0

Python 2, 70 bytes

_='_=%r;print _%%_#"dlroW ,olleH"tnirp';print _%_#"!dlroW ,olleH"tnirp

Erik the Outgolfer

Posted 10 years ago

Reputation: 38 134