isekaijin
isekaijin

Reputation: 19742

Code golf: Reverse quine

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.+

Upvotes: 13

Views: 3712

Answers (25)

user1849298
user1849298

Reputation:

C/C++:

#include<stdio.h>
int main() {
    char c;
    freopen("thisfilename.c","r",stdin); //or thisfilename.cpp
    while(scanf("%c",&c)!=-1)
        printf("%c",c);
return 0; }

Upvotes: 0

user2953222
user2953222

Reputation:

JavaScript: How many characters do you want?
SECOND SIMPLEST VALID PROGRAM (using my method): 240 Chars! (including spaces but not new lines)

<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<script>
var string = "<!DOCTYPE HTML>\n<html>\n" + document.documentElement.innerHTML + "\n</html>";
var string2 = string.split("").reverse().join("");
document.write(string2);
</script>
</body>
</html>

Upvotes: 0

rlms
rlms

Reputation: 11060

At the Python interactive prompt:

xatnys dilavni :rorrExatnyS

27 characters.

Upvotes: 3

jxh
jxh

Reputation: 70402

C89 (155 bytes)

#define q(k)r(char*s){if(*s)r(s+1);putchar(*s);}main(){r(#k"\nq("#k")\n");}
q(#define q(k)r(char*s){if(*s)r(s+1);putchar(*s);}main(){r(#k"\nq("#k")\n");})

Upvotes: 1

user848654
user848654

Reputation:

UNIX shell:

z=\' a='z=\\$z a=$z$a$z\;eval echo \$a\|rev';eval echo $a|rev

I tend to repeat my self: these programs ought to be referred to as `Goedels' because the idea behind a majority of such things was first used (in modern times) by Kurt Goedel in the proof of his First Incompleteness Theorem (Kurt Goedel's Collected Works I, p.175).

Upvotes: 2

Alex K.
Alex K.

Reputation: 175766

Javascript - IE/Chrome/FF

(function(){alert(("("+arguments.callee+")()").split("").reverse().join(""))})()

Upvotes: 3

Priyank Bhatnagar
Priyank Bhatnagar

Reputation: 814

In C , 217 chars :

a="};)01(rahctup;)--p*(rahctup);p*;43=p*(rof;)a(ftnirp;))a,b=p(tacrts(nelrts=+p{)p*rahc(niam;}7393422{=]99[b;";b[99]={2243937};main(char*p){p+=strlen(strcat(p=b,a));printf(a);for(*p=34;*p;)putchar(*p--);putchar(10);}

Upvotes: 3

user581149
user581149

Reputation:

PHP 36 bytest

<?echo strrev(join(file(__FILE__)));

Upvotes: 1

David
David

Reputation: 7153

J, 26 characters.

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

Produces the output:

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

Upvotes: 5

Christian
Christian

Reputation: 3988

PHP

Snip - removed incorrect answer

And now to redeem myself;

<?php echo strrev(file_get_contents(__FILE__)); ?>

Upvotes: 0

Matthew Flaschen
Matthew Flaschen

Reputation: 284796

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));}}

Upvotes: 1

kennytm
kennytm

Reputation: 523274

C : 0 chars

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

Upvotes: 32

David X
David X

Reputation: 4166

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

Upvotes: 4

Chris Lutz
Chris Lutz

Reputation: 75399

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?

Upvotes: 4

ephemient
ephemient

Reputation: 204718

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.

Upvotes: 2

ephemient
ephemient

Reputation: 204718

Haskell, 79 characters

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

Standard trick.

Upvotes: 1

Adam Rosenfield
Adam Rosenfield

Reputation: 400274

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);}

Upvotes: 2

John Fisher
John Fisher

Reputation: 22719

Powershell FTW (1 character):

1

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

Upvotes: 18

SilentGhost
SilentGhost

Reputation: 319601

python (34 chars):

print(open(__file__).read()[::-1])

Upvotes: 0

Brad Gilbert
Brad Gilbert

Reputation: 34120

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.

Upvotes: 1

Brian
Brian

Reputation: 118865

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)

Upvotes: 3

LiraNuna
LiraNuna

Reputation: 67262

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 :).

Upvotes: 5

strager
strager

Reputation: 90012

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--);}

Upvotes: 7

Pēteris Caune
Pēteris Caune

Reputation: 45112

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

me = 'me = %(me)r\nprint (me %% locals())[::-1]'
print (me % locals())[::-1]

Upvotes: 8

Dawid
Dawid

Reputation: 4062

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)

Upvotes: 24

Related Questions