10
\$\begingroup\$

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.

\$\endgroup\$
8
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ I like the URL. \$\endgroup\$ – Christopher May 4 '17 at 15:10
  • 3
    \$\begingroup\$ 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. \$\endgroup\$ – ETHproductions May 4 '17 at 18:03
  • 1
    \$\begingroup\$ @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.) \$\endgroup\$ – user62131 May 5 '17 at 1:02
9
\$\begingroup\$

Befunge-98, 33 bytes

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

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Nice first answer. But use the # to make you header. Also go to Tryitonline.net and get a online interpreter \$\endgroup\$ – Christopher May 4 '17 at 16:01
  • 2
    \$\begingroup\$ Also go under the save button and use the codegolf submission one. \$\endgroup\$ – Christopher May 4 '17 at 16:02
  • \$\begingroup\$ 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 \$\endgroup\$ – IQuick 143 May 4 '17 at 16:02
  • \$\begingroup\$ Note that the "codegolf submission" option on TIO gives you a starting template, including the header. \$\endgroup\$ – Ørjan Johansen May 4 '17 at 16:07
  • \$\begingroup\$ I thought g was disallowed in Befunge quines, as it reads the program's source directly from memory? \$\endgroup\$ – user62131 May 5 '17 at 1:21
6
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.