tags:

views:

1002

answers:

17

Write a program that outputs the reverse of its source code as a string. If the source is

abcd
efg

(i.e., the C string "abcd\nefg")

Then the output should be

gfe
dcba

(i.e., the C string "gfe\ndcba")

Bonus points for using esoteric languages such as brainf*ck.


*EDIT:** Removed the unnecessary \0 characters.+

+15  A: 

HQ9+:

In HQ9+ esoteric language this code might be:

Q

Here You can find interpreter for that language.

Ruby:

Reversed quine from here.

eval s=%q(puts "eval s=%q(#{s})".reverse)
Dejw
Damn you, sir! I would +1 for beating me to the punch, but I believe this should be community wiki.
Chris Lutz
Nice answer! Simple is better!
Eduardo León
+1 for HQ9+ solution (duh).
LiraNuna
Warning--the HQ9+ interpreter locked up Firefox on me.
Loren Pechtel
+8  A: 

Here's a two-liner, adapted from NeatQuine.py:

me = 'me = %(me)r\nprint (me %% locals())[::-1]'
print (me % locals())[::-1]
Pēteris Caune
+1 The equivalent of the `putchar(*p--);` trick! :)
Eduardo León
+5  A: 

Bash script

(10 Charecters)

cat $0|rev

This must be saved as a script file to work and executed on the same directory.

Another solution would be in python (or any other scripting languages) a zero byte source code file! it will print nothing in return. There's nothing in the rules saying it can't be 0byte file :).

LiraNuna
+1 for zero-byte Python solution
Ben Blank
Quines aren't supposed to read their own source code from the filesystem.
Adam Crume
Following the idea of zero-byte solution, probably any scripting language allow to do the same. So the point is that is not a quine ;-)
Dejw
+7  A: 

C89

int sprintf(char*,char*,...);char*d=
"int sprintf(char*,char*,...);char*d=%c%c%s%c%c,b[999],*p=b+251;main(){for(sprintf(b+1,d,10,34,d,34,10,10,10);*p;)putchar(*p--);}%c"
,b[999],*p=b+251;main(){for(sprintf(b+1,d,10,34,d,34,10,10,10);*p;)putchar(*p--);}
strager
Famous last words, strager. Testing...
Chris Lutz
Output has one fewer `,10` in the arguments to `sprintf()` than source.
Chris Lutz
Nice use of `putchar(*p--)`. I haven't tested it, but I understood the concept.
Eduardo León
@Lutz, Thanks, I've corrected my answer.
strager
You were still off by a semicolon in the `for()` loop, but that's a trivial fix, so I went ahead and added it. But +1.
Chris Lutz
@Lutz, I would prefer you not mess with my entry next time. It's pretty rude...
strager
Sorry. It was only one character, so I thought it would be easier.
Chris Lutz
@strager: is it actually rude to modify a _community wiki_ answer? Especially when he's not trying to take the credit? Methinks thou dost protest too much.
Adriano Varoli Piazza
@Piazza, He could have modified it if I didn't respond to his comment in a week, or something. I'm capable of modifying my own code, and I found it offensive he did it for me. I know this is CW, but that's how I felt.
strager
this site is built for being edited by others.
Ikke
I certainly feel free to edit others posts and allow the same privilege in return, even without the Community Wiki thing. *shrug* Not like there's any hard rules about that here.
ephemient
A: 

PHP

Snip - removed incorrect answer

And now to redeem myself;

<?php echo strrev(file_get_contents(__FILE__)); ?>
Christian
It's not asking to reverse the input. It's asking to print out the source program in reverse.
strager
The goal is to print the program's source code in reverse, not arbitrary user input. Read the Wikipedia article on quines: http://en.wikipedia.org/wiki/Quine_%28computing%29
Chris Lutz
bugger. my first attempt at code golf and i misunderstood. :(
Christian
:) Don't worry, this is not a serious contest, there's no limit to how many entries you may post!
Eduardo León
Ok, updated my answer with new solution.
Christian
It's generally considered cheating to read from the source file, but I'm glad you've got the objective now.
Chris Lutz
Wow ok, so yeah this is a lot trickier than I originally anticipated.
Christian
@Christian - Same here. Good luck.
Chris Lutz
+4  A: 

I'm going to lose at golf, but it taught me an important lesson about the subtleties of reverse(). Perl in way too many (142) characters:

#!/usr/bin/perl
$_='#!/usr/bin/perlc%$_=c%s%c%;print sprintf~~reverse,10,39,~~reverse,39,10;c%';print sprintf~~reverse,10,39,~~reverse,39,10;

This proves that the sprintf()/reverse() combination is not the way to approach this problem. A better Perl solution will undoubtedly use eval().


Vast improvement: 45 characters:

print~~reverse <<''x2
print~~reverse <<''x2

Note that the source file should end in a blank line. The blank line is counted in the character count - how else do you think we got an odd character count out of two identical lines of code?

Chris Lutz
`reverse<<''` works fine, at least on my Perl 5.8.8. However, isn't this missing a newline at the start of the output? `print$/.reverse<<''x2` fixes that.
ephemient
+3  A: 

F# (659 chars)

open System;let R(s:String)=new System.String(s|>List.of_seq|>List.rev|>Array.of_list) in let q=char 34 in let Q(s:String)=s.Replace(new String([|q|]),new String([|char 92;q|])) in let Quine s=String.Format("let s={0}{1}{2} in printf {3}%s%s{4} (R(Quine s)) (R s)",[|box q;box(Q s);box q;box q;box q|]) in let s="open System;let R(s:String)=new System.String(s|>List.of_seq|>List.rev|>Array.of_list) in let q=char 34 in let Q(s:String)=s.Replace(new String([|q|]),new String([|char 92;q|])) in let Quine s=String.Format(\"let s={0}{1}{2} in printf {3}%s%s{4} (R(Quine s)) (R s)\",[|box q;box(Q s);box q;box q;box q|]) in " in printf "%s%s" (R(Quine s)) (R s)

Inserting line breaks (that break the program, but make it more readable here):

open System;
let R(s:String)=new System.String(s|>List.of_seq|>List.rev|>Array.of_list) in 
let q=char 34 in 
let Q(s:String)=s.Replace(new String([|q|]),new String([|char 92;q|])) in 
let Quine s=String.Format("let s={0}{1}{2} in printf {3}%s%s{4} (R(Quine s)) (R s)",
    [|box q;box(Q s);box q;box q;box q|]) in 
let s="open System;
       let R(s:String)=new System.String(s|>List.of_seq|>List.rev|>Array.of_list) in 
       let q=char 34 in 
       let Q(s:String)=s.Replace(new String([|q|]),new String([|char 92;q|])) in 
       let Quine s=String.Format(\"let s={0}{1}{2} in printf {3}%s%s{4} (R(Quine s)) (R s)\",
           [|box q;box(Q s);box q;box q;box q|]) in " in
printf "%s%s" (R(Quine s)) (R s)
Brian
+1  A: 

Perl

73 characters.

#! /opt/perl/bin/perl
seek DATA,0,0;$/=\1;print reverse <DATA>;
__DATA__
​
  • You have to have __DATA__ at the end for the DATA file-handle to start out opened.
  • Setting $/ to a reference of a number, causes readline() to read that many bytes at a time.
  • seek(DATA,0,0) is required to set the pointer to the beginning of the file, instead of at the beginning of the __DATA__ section.
  • Could remove, or shorten the shebang line (#! ...)
  • __DATA__ requires a newline after it, or it isn't valid Perl.
Brad Gilbert
A: 

python (34 chars):

print(open(__file__).read()[::-1])
SilentGhost
The rules for writing a quine don't allow reading its source in from a file, including its own source. That misses the point.
caf
half of all the answers do exactly the same thing.
SilentGhost
That doesn't make them correct.
Iceman
+13  A: 

Powershell FTW (1 character):

1

Put it directly on the command line, or inside a script.

John Fisher
+3  A: 

Python 2 (55 char):

x='x=%s;print(x%%repr(x))[::-1]';print(x%repr(x))[::-1]

A better golfer might be able to shorten this somewhat, so any improvements are welcome.

Edit (43 char):

x='x=%r;print(x%%x)[::-1]';print(x%x)[::-1]

also thanks to @stephan202 for catching the whitespace on prints

David X
fails in python 3. print doesn't return a string that you can reverse. here's the correct solution `x='x=%r;print((x%%x)[::-1])';print((x%x)[::-1])`
Tor Valamo
@tor valamo, this isnt written in python 3, and the parenthisis are optional for the print __statement__.
David X
+2  A: 

C89, 119 characters

Unfortunately, this requires use of the highly non-standard function strrev():

main(){char*a="};)43,)b(verrts,43,a(ftnirp;)a(pudrts=b*,%c%s%c=a*rahc{)(niam",*b=strdup(a);printf(a,34,strrev(b),34);}
Adam Rosenfield
+1  A: 

Haskell, 79 characters

a=";main=putStr.reverse$\"a=\"++show a++a";main=putStr.reverse$"a="++show a++a

Standard trick.

ephemient
+1  A: 

Bash, 75 chars

Well, of course an empty shell script will output nothing, but the next best thing I can think of:

a=\;printf\ \"a=%q%s\"\ \"\$a\"\ \"\$a\"\|rev;printf "a=%q%s" "$a" "$a"|rev

Same ol' trick.

ephemient
+8  A: 

C : 0 chars

Ref: http://www0.us.ioccc.org/years.html#1994_smr

KennyTM
THIS IS MADE OF PURE WIN
M28
+1  A: 

Java:

Definitely not the shortest possible, but what the heck (one line):

public class R{public static void main(String[] a){StringBuffer s = new StringBuffer("public class R{public static void main(String[] a){StringBuffer s = new StringBuffer();s.reverse();System.out.print(s.substring(0,152));System.out.write(34);System.out.print(s);System.out.write(34);System.out.println(s.substring(152));}}");s.reverse();System.out.print(s.substring(0,152));System.out.write(34);System.out.print(s);System.out.write(34);System.out.println(s.substring(152));}}

Matthew Flaschen
+2  A: 

J, 26 characters.

|.(,~,2#{:)'|.(,~,2#{:)'''

Produces the output:

'''):{#2,~,(.|'):{#2,~,(.|
David