Golf an Anagram Quine

25

3

In this question I asked you to guess an anagram quine based on its output. However it looks like we don't have a question asking to golf an anagram quine yet. So your task will be to make the shortest anagram quine that you can.

What is an anagram quine?

An anagram quine is a non empty program that prints an anagram of its source code, other than its original source.

Here's an example of an anagram quine in Python 2:

print`'`print`*2'*2`

You should not read your own source for this challenge.

Post Rock Garf Hunter

Posted 8 years ago

Reputation: 55 382

Does this have to be a proper quine? – Leaky Nun – 8 years ago

2@LeakyNun No, It can't be a quine. – Post Rock Garf Hunter – 8 years ago

2Does the outputted code have to be a valid program? – MD XF – 8 years ago

Is code that doesn't actually meet our PPCG definition of a quine (minus the printing its source code) valid? – Okx – 8 years ago

I could've sworn we had this exact challenge before, but I guess not... – ETHproductions – 8 years ago

1@MDXF No it does not. – Post Rock Garf Hunter – 8 years ago

2@Okx It should follow our standard requirements for a quine (other than it being a quine). – Post Rock Garf Hunter – 8 years ago

1@LeakyNun Mind you, I don't think 0-byte programs have anagrams that differ from the original either! – Neil – 8 years ago

Answers

31

Pyth, 2 bytes

)(

Outputs

()

Try it online!

)  # Ends statement, does nothing in this program
 ( # Create an empty tuple
   # Implicitly print the empty tuple

Rod

Posted 8 years ago

Reputation: 17 588

3

Does that mean that you discovered that () is a quine much shorter than the well known jN*2]"jN*2]?

– Jim – 8 years ago

1@Jim no, because this isn't payload-capable :c – Rod – 8 years ago

9

><>, 9 8 7 bytes

Golfed 1 byte thanks to @WheatWizard by using ! and incrementing it to get "

Golfed 1 byte thanks to @ConorO'Brien by using # instead of <!

":1->o#

Try it online!

Outputs "#o>-1:.

Explanation

":1->o#"        Push this string (note that the IP wraps around)
:               Duplicate the top value of the stack (35 from the "#")
1-              Subtract one from it to get 34 ('"')
>o#             Print every character on the stack until the program cannot pop any more and still tries to pop a value from the stack afterwards
                The program exits with an error from not being able to pop a value from an empty stack

user41805

Posted 8 years ago

Reputation: 16 320

":1+>o<! is a bit shorter. – Post Rock Garf Hunter – 8 years ago

@WheatWizard Thanks, that was a neat golf :) – user41805 – 8 years ago

1

You can get 7 bytes: ":1->o#

– Conor O'Brien – 8 years ago

@ConorO'Brien Thanks for the tip, that is really clever. – user41805 – 8 years ago

Thanks :) I'm pleasantly surprised that it works better here than in the actual quine

– Conor O'Brien – 8 years ago

Every forgets about the single quote, 'd3*>o< :(

– Jo King – 8 years ago

9

V, 4 bytes

2ii2

Outputs:

i2i2

Try it online!

Riley

Posted 8 years ago

Reputation: 11 345

2x insert i2? – CalculatorFeline – 8 years ago

1Yep. 2 -> do the next thing twice i -> insert the following – Riley – 8 years ago

Ah I see, I thought this was Vim. – Post Rock Garf Hunter – 8 years ago

@WheatWizard Nope, this is a trivial modification of the standard V quine. – Riley – 8 years ago

This works in Vim too, right? – isaacg – 8 years ago

1@isaacg: no, in Vim you'd need to press ESC once you were finished, which is a problem because there's no ESC character in the output. – None – 8 years ago

@ais523 I see, I didn't realize Vim programs needed to leave insert mode. – isaacg – 8 years ago

@isaacg You have to press escape for the 2 part to take effect. – Riley – 8 years ago

9

Brainfuck, 158 bytes

>>--<<-[[<+>->+>->+++<<<]>-]<<<<<<[--->>.<<]>>++<<<[->>>.<<<]>>-<<<[--->>>.<<<]>>>--<<<<[++>>>>.<<<<]>>>>+++>--.[---<.>]>+.......++.......<<<>>>>>>>>>-----+++

Try it online!

It may not be the shortest version, but at least it works.

Fun fact, the output code can actually be executed (and it does terminate).

Output

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>--------------------------++++++++++++++++++<....................[[[[[[[]]]]]]]

Explanation

>>--<<-[[<+>->+>->+++<<<]>-]    Initializes the tape with the
                                help of a recurrence relation.
<<<<<<[--->>.<<]>>++<<<[->>>
.<<<]>>-<<<[--->>>.<<<]>>>--    Prints the characters using
<<<<[++>>>>.<<<<]>>>>+++>--.    classic loops.
[---<.>]>+.......++.......

<<<>>>>>>>>>-----+++            Junk to complete the anagram.

6infinity8

Posted 8 years ago

Reputation: 371

8

Python 3, 32 bytes

print("""p"r"i"n"t"2(")*"""*2)

Prints p"r"i"n"t"2(")*p"r"i"n"t"2(")*\n, sorted output: \n""""""""""""(())**22iinnpprrtt

CalculatorFeline

Posted 8 years ago

Reputation: 2 608

Oops, old version :( – CalculatorFeline – 8 years ago

1@Downvoter Please undownvote. – CalculatorFeline – 8 years ago

7

Ruby, 8 bytes

p"p*2"*2

Try it online!

This prints

"p*2p*2"

Explanation

This works similar to the python answer in the question. It will make the string p*2p*2 then using Ruby's p will print the representation of the string.

Post Rock Garf Hunter

Posted 8 years ago

Reputation: 55 382

You can use p instead of puts?! So much wasted time... – Magic Octopus Urn – 8 years ago

4@carusocomputing It's not precisely the same thing as puts. p x is equivalent to puts x.inspect – Conor O'Brien – 8 years ago

6

JavaScript (ES6), 40 32 bytes

f=($=`($)=>{$=$+"${"``"}$"}`)=>$+$

No messing around with Function.toString either. As a bonus, the code inside the string almost looks legal. Edit: Saved 8 bytes by using + instead of repeat(2).

Neil

Posted 8 years ago

Reputation: 95 035

1Why not use *2 instead of .repeat(2), does that not work? – Magic Octopus Urn – 8 years ago

1@carusocomputing JavaScript does not overload * for strings – Conor O'Brien – 8 years ago

1@carusocomputing That gave me an idea, thanks! – Neil – 8 years ago

4

Klein, 7 + 6 = 13 bytes

Here's an answer based on the ><> answer.

":1+@!

Try it online!

This outputs

:1+@!"

Post Rock Garf Hunter

Posted 8 years ago

Reputation: 55 382

4

Japt, 10 9 bytes

Found a better way :-)

Q+2ç"Q+2ç

Outputs "Q+2çQ+2ç. Test it online!

Explanation

Q+2ç"Q+2ç    // Implicit: Q = quotation mark
    "Q+2ç    // Take this string.     Q+2ç
  2ç         // Repeat it twice.      Q+2çQ+2ç
Q+           // Prepend a quote.      "Q+2çQ+2ç
             // Implicit: output result of last expression

Could also be Qi2ç"Qi2ç, which prints Qi2çQi2ç". This one is closer to the standard Japt quine:

"iQ ²"iQ ²

But I do not believe there is any easy way to wedge the quotation mark in the middle of the string for a 9-byte quine.

ETHproductions

Posted 8 years ago

Reputation: 47 880

Another 10-byter: Q+"+Q² " ² – Conor O'Brien – 8 years ago

@ConorO'Brien Yep, and you can always arrange the 4 chars inside the string in each of the 24 possible permutations. Also I believe you can always replace both spaces with newlines – ETHproductions – 8 years ago

3

Ruby, 20 bytes

$><<%q($><<%q()*2)*2

This outputs

$><<%q()*2$><<%q()*2

Taking advantage of Ruby's %q(...) string syntax, which supports nested parentheses.

Doorknob

Posted 8 years ago

Reputation: 68 138

3

Retina, 8 bytes


_

$nn$

Try it online!

Prints


n$_
n$

Both contain one _, two n, two $ and three linefeeds.

Note that a linefeed followed by almost any other character is a trivial solution, but it's questionable whether it's valid, because the second character only encodes itself and the linefeed doesn't really encode either output character.

Explanation


_

Replace the empty input with a _.


$nn$

Match an empty string, which happens both before or after the _ and insert a linefeed ($n), an n, and a $. Since we first inserted that _, this adds each of those characters twice, so the n and $ account for the $n, and we get two of the three linefeeds we need in the output. The third linefeed is printed because Retina prints a trailing linefeed by default.

We could also use n$n$ in this stage, which would then print:

n
$_n
$

Martin Ender

Posted 8 years ago

Reputation: 184 808

3

Python Repl, 4 bytes

This is my first Code Golf solution, so I hope it meets the rules. In the Python 2 or 3 interactive interpreter:

>>> (1),
(1,)

The output is an anagram of the input.


Another:

>>> 2*'2*'
'2*2*'

In Python 2:

>>> type('rst <>'),
(<type 'str'>,)

In Python 3:

>> {1, 0}
{0, 1}

Update 2017-06-15: Yet another:

>>> 01.
1.0

Colonel Panic

Posted 8 years ago

Reputation: 133

1I think the last answer here violates the quine rules (all the characters in the output serve the same purpose as the matching character in the input). The others are fine, though. – None – 8 years ago

2

Haskell, 38 39 bytes

main=print$[0,0]>>"main=print$[0,0]>>"

Try it online! Output:

"main=print$[0,0]>>main=print$[0,0]>>"

Edit: +1 byte because I previously forgot about the implicit trailing newline of print.


Alternative: (Same byte count but does not contain ASCII-owl)

main=print$e++e;e="main=print$e++e;e="

Try it online!

Output:

"main=print$e++e;e=main=print$e++e;e="

Laikoni

Posted 8 years ago

Reputation: 23 676

Just barely an anagram quine rather than a true quine.... – Feathercrown – 8 years ago

2

05AB1E, 10 bytes

'∞∞''∞'JJ∞

Try it online!

Output:

∞∞''JJ''∞∞

Explanation:

Code       | Explanation                | Stack
-----------+----------------------------+-------------------
'∞         | Push literal '∞'.          | ["∞"]
  ∞        | Mirror.                    | ["∞∞"]
   ''      | Push literal "'".          | ["∞∞","'"]
     ∞     | Mirror.                    | ["∞∞","''"]
      'J   | Push literal 'J'.          | ["∞∞","''","J"]
        J  | Join it all together.      | ["∞∞''J"]
         ∞ | Mirror.                    | ["∞∞''JJ''∞∞"]
-----------+----------------------------+-------------------
           | Implicit print.            | ∞∞''JJ''∞∞

Magic Octopus Urn

Posted 8 years ago

Reputation: 19 422

2

Groovy, 24 20 bytes

{"""{""*""2""}"""*2}

-4 thanks to CalculatorFeline, the whitespace wasn't needed after all!

Output:

{""*""2""}{""*""2""}

Explanation:

Anonymous closure that, when called, returns {""*""2""} two times (concatenated).

Magic Octopus Urn

Posted 8 years ago

Reputation: 19 422

1Explanation please. Also, bytes can (probably) be saved by using the *2 as padding between quotes: {"""{""*""2""}"""*2} – CalculatorFeline – 8 years ago

@CalculatorFeline it's pretty self explanatory. But yes, that is a <s>100%</s> 9% better idea than mine. – Magic Octopus Urn – 8 years ago

3Actually, it's only 9% better :P – CalculatorFeline – 8 years ago

2

CJam, 6 bytes

"_`"_`

Try it online!

Prints

_`"_`"

Explanation

"_`"   e# Push this string.
_      e# Duplicate.
`      e# Stringify it, which wraps it in quotes.
       e# Implicitly print stack contents.

Martin Ender

Posted 8 years ago

Reputation: 184 808

I was about to post this... "`"` also works – Luis Mendo – 8 years ago

1

PHP, 44 bytes

<?=str_repeat('<?=\str_\repeat(\'\',2);',2);

Try it online!

Jörg Hülsermann

Posted 8 years ago

Reputation: 13 026

1

Fission 2, 9 8 6 bytes

R"'!+O

Try it online!

Explanation

An atom is created at R, which moves right. This atom then comes across a ", which starts printing mode. In printing mode, all characters (until the matching ") are printed. This means it prints '!+OR in this case. Then, all that is left is printing ", which is done by the remaining characters. '! sets the atom's mass to the character code of !, and + increments it to the character code of ". Then, the character code is output by O and the atom is destroyed, ending the program.

(Actually, this is just a rotation of the shortest quine)

Luke

Posted 8 years ago

Reputation: 4 675

I believe you can just use the standard quine and shift it cyclically (so something like R"'!+O, untested). – Martin Ender – 8 years ago

@MartinEnder: you're right. Updated. Thanks for helping. – Luke – 8 years ago

1

Bash, 36 bytes

tee f<<<'tee f<<<""cat f'"''"
cat f

This outputs

tee f<<<""cat f''
tee f<<<""cat f''

(and creates the file f as a side effect, but that's allowed per meta.)

Both the program and output have a trailing newline.

Thought process: I figured that the easiest way to output a string two times, aside from assigning it to a variable, was to do

tee f<<<string
cat f

The string needs to be quoted because it will contain spaces and < characters, so then I had

tee f<<<'tee f<<<cat f'
cat f

which almost works, except it doesn't output the quotes. Fortunately, Bash supports string literal concatenation by simply placing them next to each other, so appending "''" to the herestring and inserting "" inside the single quote part yields this solution.

Doorknob

Posted 8 years ago

Reputation: 68 138

1

Befunge, 11 bytes

' 2+">:#,_@

Prints:

+2 '@_,#:>"

Explanation:

' 2+"        Put a " on the stack (32 + 2)
    "        Put the rest of the code on stack (wrap-around string)
     >:#,_   Print stack
          @  End

MegaTom

Posted 8 years ago

Reputation: 3 787

Yes. but so does ". What is the restriction on that exactly? – MegaTom – 8 years ago

I don't think that " is considered reading ones own source, its just a string literal. g however is pretty blatantly reading its own source. – Post Rock Garf Hunter – 8 years ago

@WheatWizard okay. I will change it. – MegaTom – 8 years ago

":1+>:#,_@! also works but its not shorter. – Post Rock Garf Hunter – 8 years ago

' 2+"8k,@ for 9 bytes – ovs – 8 years ago

" in Befunge forms a "topological quine" (in which the program is inside and outside a string literal at the same time). The written version of our rules is really unclear on whether that counts as reading your own source or not, but we've historically allowed it. – None – 8 years ago

1

CJam, 8 bytes

"2*`"2*`

Try it online!

Explanation

Similar to the Python example in the question

"2*`"     e# Push the string "2*`"
     2*   e# Repeat it twice
       `  e# Get its string representation (wrap in quotes)

The output is "2*`2*`".

Business Cat

Posted 8 years ago

Reputation: 8 927

1

QBIC, 8 bytes

?A+@?A@+

I just figured out how to do a proper quine in QBIC. Making an anagram out of it is done by simply switching around the characters in the string literal. There are 24 possible anagrams this way.

steenbergh

Posted 8 years ago

Reputation: 7 772

1

Befunge-98, 8 bytes

"'$<@,k7

Try it online!

ovs

Posted 8 years ago

Reputation: 21 408

1

Ohm, 14 bytes

"æ3M.Cæ"æ3M."C

Try it online!

Output:

æ3M.CæC.M3æ"""

Explanation

"æ3M.Cæ"æ3M."C
"æ3M.Cæ"       # Pushes "æ3M.Cæ"
        æ      # Palindrone of that string
         3M    # 3 times...
           ."   # Push " on the stack
             C  # Concatenate with the string above

Datboi

Posted 8 years ago

Reputation: 1 213

1

JavaScript (ES6), 15 bytes

f=(s='f=')=>f+s

Outputs:

(s='f=')=>f+sf= 

Snippet:

f=(s='f=')=>f+s

console.log(f());

Rick Hitchcock

Posted 8 years ago

Reputation: 2 461

1

Mathematica, 2 bytes

.0

Output:

0.

A number starting with a decimal point such as .123 is interpreted as 0.123, so .0 is interpreted as 0.0. Since the part of the number after the decimal point is zero, Mathematica does not print it.

Ben

Posted 8 years ago

Reputation: 159

1I don't think this is valid. Our site definition requires that quines have an "encoder" and a "decoder", this bans literal only type quines. – Post Rock Garf Hunter – 8 years ago

All the rules for quines carry over to this challenge. – Post Rock Garf Hunter – 8 years ago

@WheatWizard: The . encodes itself, but I don't think the 0 does? A 0 beyond the decimal point can't sanely be seen as encoding a leading zero before the decimal point, the latter's a side effect of printing a float. So under the old quine rules, there's no problem here. (I'm not sure whether the new rules have come into force yet.) – None – 8 years ago

@ais523 I don't know. Perhaps this should be addressed by a meta question. – Post Rock Garf Hunter – 8 years ago

1

JavaScript (yet another one), 11 bytes

f=_=>'=f'+f

Called with f(), outputs

=f_=>'=f'+f

f=_=>'=f'+f

console.log(f());

GOTO 0

Posted 8 years ago

Reputation: 752

1

Python 3, 31 bytes

a='a=%r;pritn(a%%a)';print(a%a)

aaay aaay

Posted 8 years ago

Reputation: 71

1

Stax, 8 4 bytes

.S.S

Run and debug online!

A direct port of this answer.

Old version, 8 bytes

..b..LbL

Run and debug online!

Alternative version with a pretty cheap trick that can be applied to proper quines in almost any language.

"43bL"34bL

Run and debug online!

Because "34bL"34bL is a proper quine in Stax.

Yet another version, using only single-char string literals.

''c'Lc'cccLcLL

Run and debug online!

Explanation

.S.S        Generates powerset ["","S","S.","."]
            Implicit flatten and output

..b         Push string ".b"
   ..L      Push string ".L"
      b     Duplicate both strings
       L    Concatenate all 4 strings to a single one.

Weijun Zhou

Posted 8 years ago

Reputation: 3 396

1

W, 2 bytes

1-

Explanation

a   % (Implicit) argument of the input
    % The input is automatically set to 0
    % if it isn't specified
 1- % Minus 1 over the input (i.e. 0-1 = -1)

Output:

-1

a'_'

Posted 8 years ago

Reputation: 1 099

0

05AB1E, 13 bytes

"34çJ∞"34çJ∞

Outputs:

34çJ∞""∞Jç43

Try it online!

"34çJ∞"      # Push this string                 | [ 34çJ∞ ]
       34ç   # Push a quote (")                 | [ 34çJ∞, " ]
          J  # Join                             | [ 34çJ∞" ]
           ∞ # Mirror                           | [ 34çJ∞""∞Jç43 ]
             # Implicitly output with a newline

Riley

Posted 8 years ago

Reputation: 11 345

0

C, 60 bytes

main(s){printf(s="main(s){s=printf(%c%s%1$c,34,s);}",34,s);}

Just the shortest C quine with trivial modifications.

Output:

main(s){s=printf("main(s){s=printf(%c%s%1$c,34,s);}",34,s);}

Try it online!

MD XF

Posted 8 years ago

Reputation: 11 605

0

shortC, 34 bytes

Bs){Rs="Bs){s=R%c%s%1$c,34,s",34,s

Output:

Bs){s=R"Bs){s=R%c%s%1$c,34,s",34,s

Try it online!

MD XF

Posted 8 years ago

Reputation: 11 605

0

str, 10 bytes

`2xr;`2xr;

Try it online! Outputs `2xr;2xr;`

`2xr;`2xr;
.........;   preamble
`2xr;`       a string containing those character
      2x     repeat twice
        r    print the representation of that doubled string
         ;   end preamble (no program)

Conor O'Brien

Posted 8 years ago

Reputation: 36 228

0

Jelly, 7 bytes

“ØvṚ”Ṙv

Try it online!

Dennis

Posted 8 years ago

Reputation: 196 637

0

Braingolf, 36 32 bytes

"V#!1+!@@R!&@&@;"V#!1+!@@R!&@&@;

Try it online!

Outputs ""V#!1+!@@R!&@&@;V#!1+!@@R!&@&@;, which is the source, but with both " at the start, the ; is required at the end to prevent a trailing newline

Explanation:

"V#!1+!@@R!&@&@;"V#!1+!@@R!&@&@;  
"V#!1+!@@R!&@&@;"                 Pushes the charcode of each char in the string
                 V                Create stack2 and switch to it
                  #!              Push charcode of !
                    1+            Increment to charcode of "
                      !@@         Print twice, popping the 2nd time
                         R        Return to stack1
                          !&@&@   Print entire stack twice, popping the 2nd time
                               ;  Suppress implicit output to prevent trailing newline

Alternatively (not sure this one actually meets the requirement of it being a proper quine):

Braingolf, 2 bytes


1

\n1, outputs 1\n

Explanation:

\n1
\n   No-op, does nothing
  1  Pushes 1
     Implicit output of last item on stack plus trailing newline

Skidsdev

Posted 8 years ago

Reputation: 9 656

0

Java 8, 88 bytes

v->{String s="-v>{String s=%c%s%1$c;return s.format(s,34,s);}";return s.format(s,34,s);}

Outputs with both v and - swapped as anagram:

-v>{String s="-v>{String s=%c%s%1$c;return s.format(s,34,s);}";return s.format(s,34,s);}

Explanation:

Try it online.

  • The String s contains the unformatted source code (with - and v swapped to make it an anagram.
  • %s is used to input this String into itself with the s.format(...).
  • %c, %1$c and the 34 are used to format the double-quotes.
  • s.format(s,34,s) puts it all together.

Kevin Cruijssen

Posted 8 years ago

Reputation: 67 575

0

Microscript II, 8 bytes

"phq"pqh

Microscript, 11 bytes (also a reverse quine):

0"Caxq"Caxq

SuperJedi224

Posted 8 years ago

Reputation: 11 342

0

Jelly, 6 bytes

00,”ṘṘ

Try it online!

Output is

0,”Ṙ0Ṙ

dylnan

Posted 8 years ago

Reputation: 4 993

0

GolfScript, 8 bytes

{}*{.*}.

Output is {.*}{.*}

Works because the first three characters are garbage to find a way to get another set of braces in the program that don't show up.

If you don't care about whitespace, then {+}{.}+. outputs {+ .}{+ .}, which was my first attempt.

Try it online!

Mathgeek

Posted 8 years ago

Reputation: 408

0

Keg, 8 bytes

`.,:`:,.

Try it online!

A rearrangement of the usual quine.

Lyxal

Posted 8 years ago

Reputation: 5 253