9
\$\begingroup\$

You must make a polyglot that outputs its source code in one language and its source code backward in another. Unlike the normal rules, you are allowed to read the current file or use a builtin to get the source code of your submission and reverse that in one language. Your source code cannot be a palindrome.

For example, if your source code is abcxyz, it must output abcxyz in one language and zyxcba in another. If your code is abcxyzyxbca, it's invalid because it's a palindrome.

Good luck!

\$\endgroup\$
  • 3
    \$\begingroup\$ Normal rules are there for a reason. Allowing quine built-ins will likely make this challenge too broad, and allowing palindrome source codes allows answers which are forward quines for both languages. \$\endgroup\$ – Erik the Outgolfer Apr 24 '17 at 11:43
  • \$\begingroup\$ @EriktheOutgolfer palindromes are now not allowed. \$\endgroup\$ – programmer5000 Apr 24 '17 at 11:44
  • 1
    \$\begingroup\$ @EriktheOutgolfer Ah, so the source code cannot be a palindrome? \$\endgroup\$ – Luis Mendo Apr 24 '17 at 11:52
  • 2
    \$\begingroup\$ I think you should offer a bounty for the first person to complete this challenge without breaking any of the normal quine rules. (maybe 50 rep?) \$\endgroup\$ – Qwerp-Derp Apr 24 '17 at 12:13
  • 1
    \$\begingroup\$ I read "in one language" as clearly disallowing the case where both languages read the source code, but the current top-voted answer does exactly that. Can you edit to make it clear whether that's meant to be allowed? \$\endgroup\$ – hvd Apr 24 '17 at 21:44
5
\$\begingroup\$

PHP & GolfScript, 2 bytes


1

i.e. a newline and the digit 1.

This is a reverse quine in GolfScript, contributed on this site by Justin. PHP hasn't triggered that it's a programming language so it prints its input.

\$\endgroup\$
6
\$\begingroup\$

Python 2 / Python 3, 71 bytes

lambda _='lambda _=%r:(_%%_)[::int(1-(1/2)*4)]':(_%_)[::int(1-(1/2)*4)]

Does not use any quine builtins.

Thanks to ovs for generally awakening me.

\$\endgroup\$
  • \$\begingroup\$ lambda _='lambda _=%r:(_%%_)[::int(1-(1/2)*4)]':(_%_)[::int(1-(1/2)*4)] for 71 bytes \$\endgroup\$ – ovs Apr 24 '17 at 12:48
  • \$\begingroup\$ @ovs Oh of course, how I didn't think of that. \$\endgroup\$ – Erik the Outgolfer Apr 24 '17 at 12:49
6
\$\begingroup\$

Batch / bash + tac, 39 bytes

:;tac -r -s '.\|'$'\n' $0;exit
@type %0

Outputs forwards in Batch. Explanation: Batch sees the first line as a label and ignores it, while the second line simply copies the source file to STDOUT. (Note that you need to invoke the file including extension, or change %0 to %~f0.) bash sees four commands:

  • : does nothing (same as true)
  • tac -r -s '.\|'$'\n' $0
    • -r puts tac into regex mode
    • -s specifies a regex
    • '.\|'$'\n' is the regex, composed of
      • . any character except newline
      • \| or
      • $'\n' a newline
    • The upshot is that tac splits the file into characters rather than lines.
  • exit stops the script, ignoring the fourth command
  • @type %0 (ignored)
\$\endgroup\$
  • \$\begingroup\$ I think that you can replace the first line with :;rev $0|tac;exit. Also, the header should say Batch / sh + util-linux + coreutils instead. \$\endgroup\$ – Erik the Outgolfer Apr 24 '17 at 12:01
  • \$\begingroup\$ @EriktheOutgolfer $'\n' is a Bashism, rev $0|tac doesn't reverse newlines correctly, and does it really matter that tac comes in a package? \$\endgroup\$ – Neil Apr 24 '17 at 12:23
  • \$\begingroup\$ $'\n' doesn't exist in the replacement I've suggested, and rev $0|tac works for me. And I think it's better to specify the packages instead of the individual utilities, because sometimes confusion might arise (e.g. which package's tac?). \$\endgroup\$ – Erik the Outgolfer Apr 24 '17 at 12:29
  • \$\begingroup\$ @EriktheOutgolfer Try it online! is wrong. So is Try it online! (but more subtly). \$\endgroup\$ – Neil Apr 24 '17 at 12:31
  • \$\begingroup\$ Could you please give an explanation? \$\endgroup\$ – programmer5000 Apr 24 '17 at 12:34
3
\$\begingroup\$

PHP & Retina, 2 bytes


1

The same as Gille's PHP & GolfScript answer.

PHP just reads the code and outputs it directly, Retina will replace matches of an empty string in the input and replace it with 1, and output that with a newline.

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

JS (ES5), JS (ES6), 94 bytes

function f(){try{eval("x=(f+'f()').split``.reverse().join``")}catch(e){x=f+"f()"};return x}f()

Does not use any quine built-ins. Just uses the fact that JS functions stringify to their code. Probably can be golfed more.

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

If you allow reading the source code, it isn't really a quine.

PHP & sh+util-linux, 6 bytes

rev $0

I'm sure that the usual golfing languages can do it in 2 bytes.

\$\endgroup\$
  • \$\begingroup\$ They infact can. \$\endgroup\$ – ATaco Apr 26 '17 at 0:16

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.