# Quine / Reverse Quine

Write a complete program that takes a boolean or non-negative integer input. It must:

• Output its own source code if the input value is falsy
• Output its own source code in reverse if the input value is truthy

Your program cannot be palindromic, nor can it read its own source code by any means.

This is code golf - shortest code in bytes wins.

• So... if my language doesn't have booleans. But 0 is falsy and positive integers are truthy. Can I assume that the input will be only 0 or 1 (as stand-ins for booleans - the language will in fact always yield one of those two as the result of a conditional operator)? Or do I have to support any integers, since I can't use "actual" booleans? – Martin Ender Oct 30 '15 at 14:47

# Gol><>, 9 bytes

'rd3*I?rH


I feel a little awkward posting this, since we already have ><>, Vitsy and Minkolang answers. The only additions to the standard quine are I (read integer input), ? (execute next if truthy) and r (reverse stack).

## CJam, 17 16 bytes

{"_~"+Wq~g#%}_~


Test it here.

A fairly straight-forward modification of the standard quine. Other solutions for 17 bytes:

{"_~"+q~{W%}&}_~
{"_~"+q~!2*(%}_~


If I can assume that the input is only 0 or 1 (as a stand-in for a boolean, which there is no dedicated type for in CJam), I get 15 by omitting the g:

{"_~"+Wq~#%}_~


### Explanation

{"_~"+  e# Standard generalised quine framework. Leaves the source code on the stack.
W      e# Push a -1.
q~     e# Read and evaluate input.
g      e# signum, turning truthy values into 1 (leaving 0 unchanged).
#      e# Power. -1^0 == 1, -1^1 == -1.
%      e# Select every Nth element: a no-op for 1 and reverses the string for -1.
}_~

• Since 0 and 1 are CJam's Booleans, I don't think you need the g. – Dennis Oct 30 '15 at 14:45
• @Dennis Yeah I wasn't really sure about that. – Martin Ender Oct 30 '15 at 14:46
• Surprisingly, I somehow beat this. o-o I won't be surprised if you golf it down more though. :D – Addison Crump Oct 30 '15 at 21:53

# Pyth, 17 bytes

_WQjN*2]"_WQjN*2]


A straightforward modification of the standard Pyth quine.

• Damn, I'd been waiting to post this since an hour after it was sandboxed. – lirtosiast Oct 30 '15 at 15:50

## ><>, 17 Bytes

Requires the -v flag (+1 byte) for pushing input onto the stack (or for you to put input on the stack beforehand on the online interpreter).

'rd3*$?rol?!;70.  You could do the below for the same amount of bytes (without the flag) if integer inputs only were allowed (i.e. 0 for falsy, 1 for truthy). 'rd3*ic%?rol?!;80.  Try it online Truthy/falsy for ><> are anything not 0 and 0, respectively. # Vitsy, 15 Bytes ...I'm... I'm beating CJam! (shouts over) Mom! Look, ma, I deed it! 'rd3*i86*-)rl\O Explanation: 'rd3*i86*-(rl\O Standard quine, but with a twist: ' Capture the source code as a string rd3* Create the ' character in ASCII i86*- Get the input character as ASCII then subtract 48 from it. If it's zero, it'll the if statement will skip the next instruction. (r If the top item of the stack is zero, do not do the next item. The next item here is reverse. l\O Print out the stack.  ## Newer Version of Vitsy, 11 Bytes v'rd3*}v)rZ v Capture the input as a variable. ' Capture the source until encountering another ' r Reverse the stack. d3* Push ' to the stack. } Rotate the stack to the right one. v) Push the variable (input) to the stack and test if it is not zero. r If so, reverse the stack. Z Output everything in the stack. # Javascript (ES6), 42 bytes $=_=>'$='+(_?$:[...''+$].reverse().join)  This is a modification of my Bling Quine. It's twice as long, too. • Does this actually output anything? Answers that only work in a REPL are not allowed. – feersum Oct 30 '15 at 14:10 • @ETHproductions That would call the function, but it still wouldn't print anything. Also, it wouldn't be a quine anymore. – Dennis Oct 30 '15 at 14:51 • @Dennis Right. Well, I guess that means prompt() is necessary, unless we switch to Node.JS. I believe $=_=>(A=$=${$};$(prompt()),alert(_?A:[...A].reverse().join));$(prompt()) works properly, although maybe it could be golfed down more. – ETHproductions Oct 30 '15 at 15:19 • "Your program cannot [...] read its own source code by any means." Does this solution fall into this category? – ETHproductions Oct 30 '15 at 21:57 ## Burlesque, 40 bytes ri#Q2 SH ~- "ri#Q" \/ .+ j "<-" ps if sh  Explanation: Burlesque has advanced stack and code manipulation built-ins. In fact, you can't access the source code of the program but you can access the remaining code that is to be executed in the future. This means #Q will return all the code that follows it which is why we have to add everything up to #Q to that code which is what we're doing with ri#Q. blsq ) #Q1 2++ 12 -- this is the result of 1 2++ {1 2 ++} -- this is the result of #Q  ++1 2 is technically illegal code since it's stack based. But we can manipulate the code to make it execute as 1 2++: blsq ) #Q<-#q++1 2 12  Working with these built-ins is incredibly tricky and nobody has yet used them for anything productive except for quine related things. If you reverse ++1 2 you get 2 1++ which would produce 21 and not 12. The reason the code above produces 12 is because #Q also includes the <- so in the end we end up executing a lot more than just 2 1++ :p. We end up executing 2 1++#q<- which produces 12. We can actually replace things in our code for example this code replaces all occurences of ?+ in itself with ?* blsq ) #Q(?+)(?*)r~5.-#q5 5?+ 25  Usage: $ echo "1" | blsq --stdin 'ri#Q2 SH ~- "ri#Q" \/ .+ j "<-" ps if sh'
hs fi sp "-<" j +. /\ "Q#ir" -~ HS 2Q#ir
$echo "0" | blsq --stdin 'ri#Q2 SH ~- "ri#Q" \/ .+ j "<-" ps if sh' ri#Q2 SH ~- "ri#Q" \/ .+ j "<-" ps if sh  ## Haskell, 126118 108 bytes main=readLn>>=putStr.([t,reverse t]!!);t=s++show s;s="main=readLn>>=putStr.([t,reverse t]!!);t=s++show s;s="  Expects 0 or 1 as input. ## Minkolang 0.10, 13 bytes "66*2-n,?r$O.


Try it here.

This is exactly like the standard quine except for these four characters: n,?r. n takes an integer from input, , inverts it, so ? skips r if the input is truthy. Otherwise, r reverses the stack so that it is output in reverse order.

• My thunder is gone. :c – Addison Crump Oct 30 '15 at 21:58

## Python 2, 51 bytes

s="print('s=%r;exec s'%s)[::1-2*0**input()]";exec s


interface N{static void main(String[]a){var s="interface N{static void main(String[]a){var s=%c%s%1$c;s=s.format(s,34,s);System.out.print(new Boolean(a[0])?new StringBuffer(s).reverse():s);}}";s=s.format(s,34,s);System.out.print(new Boolean(a[0])?new StringBuffer(s).reverse():s);}}  Try it online. # Java 10 (as lambda function), 154 bytes b->{var s="b->{var s=%c%s%1$c;s=s.format(s,34,s);return b?new StringBuffer(s).reverse():s;}";s=s.format(s,34,s);return b?new StringBuffer(s).reverse():s;}


Try it online.

Explanation:

interface N{                          // Class
static void main(String[]a){        //  Mandatory main-method
var s="interface N{static void main(String[]a){var s=%c%s%1$c;s=s.format(s,34,s);System.out.print(new Boolean(a[0])?new StringBuffer(s).reverse():s);}}"; // Unformatted source code s=s.format(s,34,s); // Format the source code (the quine-String) System.out.print( // Print: new Boolean(a[0])? // If the input is true: new StringBuffer(s).reverse() // Print the quine-String reversed : // Else: s);}} // Print the quine-String as is  explanation: • The var s contains the unformatted source code • %s is used to put this String into itself with s.format(...) • %c, %1$c, and 34 are used to format the double-quotes
• s.format(s,34,s) puts it all together

And then new StringBuffer(s).reverse() is used to reverse the quine-String if necessary based on the input-boolean.

# 05AB1E, 21 bytes

0"D34çýÂ‚sè"D34çýÂ‚sè


Try it online.

Modification of the default 0"D34çý"D34çý by adding Â‚sè.

Explanation:

0                        # Push 0 to the stack
#  STACK: [0]
"D34çýÂ‚sè"             # Push the string 'D34çýÂ‚sè' to the stack
#  STACK: [0, 'D34çýÂ‚sè']
D            # Duplicate this string
#  STACK: [0, 'D34çýÂ‚sè', 'D34çýÂ‚sè']
34ç         # Push '"' to the stack
#  STACK: [0, 'D34çýÂ‚sè', 'D34çýÂ‚sè', '"']
ý        # Join the stack with '"' delimiter
#  STACK: ['0"D34çýÂ‚sè"D34çýÂ‚sè']
Â       # Bifurcate (short for Duplicate & Reverse)
#  STACK: ['0"D34çýÂ‚sè"D34çýÂ‚sè', 'ès‚Âýç43D"ès‚Âýç43D"0']
‚      # Pair them up:
#  STACK: [['0"D34çýÂ‚sè"D34çýÂ‚sè','ès‚Âýç43D"ès‚Âýç43D"0']]
s     # Swap to get the boolean input (0 or 1)
#  STACK: [['0"D34çýÂ‚sè"D34çýÂ‚sè','ès‚Âýç43D"ès‚Âýç43D"0'], 1]
è    # Index the input into the list
#  STACK: ['ès‚Âýç43D"ès‚Âýç43D"0']
# (Output the top of the stack implicitly)


PS: Automatically prints a trailing newline. If this should be reversed as well, it's 23 bytes instead:

0"D34çýÂ‚sè?"D34çýÂ‚sè?


Try it online. (?` is an explicit Print without new-line)