Period 2 Reversed Quine

10

2

Your task is to write a program that outputs its own source code in reverse. However, when the reversed code is run, it should output the source code, facing the correct direction.

Example

Say your program is abc. When run, it should output cba. cba, when run, should output abc.

Rules

Your program should follow all the rules of a proper quine (except for outputting the source code). Palindromes are disallowed.

Random note: I am aware of this but I believe that this challenge is different because the transformed code must have the same properties.

internet_user

Posted 2017-05-04T14:28:30.243

Reputation: 314

Related – HyperNeutrino – 2017-05-04T14:35:36.010

Also related. – Martin Ender – 2017-05-04T14:58:48.403

Answers

9

RProgN, 3 bytes

1
2

Thanks to @MartinEnder for reminding me about this answer.

Try it online!

How it works

This exploits a potential flaw in our definition of proper quine:

It must be possible to identify a section of the program which encodes a different part of the program. ("Different" meaning that the two parts appear in different positions.)

Furthermore, a quine must not access its own source, directly or indirectly.

That's obviously the case here, as the output is the reverse of the code and the code is not a palindrome.

RProgN – reverse programmer notation – uses a LIFO stack and prints the items on it in the order they are popped. The two tokens 1 and 2, separated by spaces and/or newlines, get popped in reverse order and are printed separated by a newline.

This prints the reversed program

2
1

which, in turn, prints the original one.

!enilno ti yrT

Dennis

Posted 2017-05-04T14:28:30.243

Reputation: 196 637

I like the URL. – Christopher – 2017-05-04T15:10:47.890

4

We should get this fixed... I'd post a new answer (based on this comment) on the meta question, but it probably wouldn't get enough attention to compete with the existing answers.

– ETHproductions – 2017-05-04T18:03:58.757

2@ETHproductions: Do it. I feel there are several problems with our current proper quine definition. (Arguably, my language 7 is a study in edge cases for proper quineness; in addition to this particular case, it can handle quite a few others.) FWIW, I'm not sure this is a proper reverse-quine even by our current definition; the 1 and 2 are clearly encoded by themselves, so the only potential interest is as to what's encoding the newline. (This is different from a 1\n1, in which the two 1s encode each other; that's possibly a proper quine but not a proper reverse-quine.) – None – 2017-05-05T01:02:35.560

9

Befunge-98, 33 bytes

b3*>1#;-:0g,:#;_@_;#:,g0:-;#1>*b3

Try it online!

IQuick 143

Posted 2017-05-04T14:28:30.243

Reputation: 1 229

Nice first answer. But use the # to make you header. Also go to Tryitonline.net and get a online interpreter – Christopher – 2017-05-04T16:01:21.703

2Also go under the save button and use the codegolf submission one. – Christopher – 2017-05-04T16:02:21.133

I edited it on TIO in the first place I just forgot to add the link but now its added and thanks for the Header tip – IQuick 143 – 2017-05-04T16:02:37.637

Note that the "codegolf submission" option on TIO gives you a starting template, including the header. – Ørjan Johansen – 2017-05-04T16:07:50.543

I thought g was disallowed in Befunge quines, as it reads the program's source directly from memory? – None – 2017-05-05T01:21:17.157

I'm not sure but the Quines on wikipedia use it so I quess its allowed – IQuick 143 – 2017-05-05T12:23:14.990

Wikipedia doesn't care about PPCG's Proper Quine definition.

– Ørjan Johansen – 2017-05-07T02:42:21.103

7

Fission 2, 10 bytes

"L;L'!+!'_

Try it online!

This prints:

_'!+!'L;L"

Try it online!

And vice versa.

Explanation

This is a modification of the reverse quine. It's working to our advantage here that ! is used for printing and is also only one code point away from the the quote ". That makes it easier to make the quote printing section palindromic (the '!+!'). Let's start with the first code:

"L;L'!+!'_

This program has two entry points at the Ls, which each creates a left-going atom. However, the right one immediately hits the ; which destroys it. The left one enters string mode and wraps around to the end, so that it prints the entire code (except the ") from back to front. That already gives us _'!+!'L;L. All that's left is to print the ". _ can be ignored, '! sets the atom's mass to 33 (the code point of !), + increments it to ", and ! prints it. That's all the output done. The 'L sets the atoms mass to the code point of L but that's irrelevant. ; destroys this atom as well and since no atoms are left, the program terminates.

Now the other way round:

_'!+!'L;L"

Again, we have two entry points but one atom is immediately destroyed. This time we move through the !+!' section first, so we start by printing a quote. The '_ is again irrelevant, but we need the _ (or some other useless character) here to avoid ' escaping the ". The atom wraps to the end, traverses the source code once in string mode to print the rest of the program in reverse, the L is then ignored and ; destroys the atom and terminates the program.

Martin Ender

Posted 2017-05-04T14:28:30.243

Reputation: 184 808