Reputation: 19742
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
Reputation:
#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
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
Reputation: 11060
At the Python interactive prompt:
xatnys dilavni :rorrExatnyS
27 characters.
Upvotes: 3
Reputation: 70402
#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
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
Reputation: 175766
Javascript - IE/Chrome/FF
(function(){alert(("("+arguments.callee+")()").split("").reverse().join(""))})()
Upvotes: 3
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
Reputation: 7153
|.(,~,2#{:)'|.(,~,2#{:)'''
Produces the output:
'''):{#2,~,(.|'):{#2,~,(.|
Upvotes: 5
Reputation: 3988
Snip - removed incorrect answer
And now to redeem myself;
<?php echo strrev(file_get_contents(__FILE__)); ?>
Upvotes: 0
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
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
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
Reputation: 204718
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
Reputation: 204718
a=";main=putStr.reverse$\"a=\"++show a++a";main=putStr.reverse$"a="++show a++a
Standard trick.
Upvotes: 1
Reputation: 400274
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
Reputation: 22719
Powershell FTW (1 character):
1
Put it directly on the command line, or inside a script.
Upvotes: 18
Reputation: 34120
73 characters.
#! /opt/perl/bin/perl
seek DATA,0,0;$/=\1;print reverse <DATA>;
__DATA__
__DATA__
at the end for the DATA
file-handle to start out opened.$/
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.#!
...)__DATA__
requires a newline after it, or it isn't valid Perl.Upvotes: 1
Reputation: 118865
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
Reputation: 67262
(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
Reputation: 90012
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
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
Reputation: 4062
In HQ9+ esoteric language this code might be:
Q
Here You can find interpreter for that language.
Reversed quine from here.
eval s=%q(puts "eval s=%q(#{s})".reverse)
Upvotes: 24