Write a program that prints its own source code out backwards, in other words an eniuq.
Scoring:
- +50 if you use pull data from the Internet.
- +25 if you read your own source code.
- +1 point per character
- Lowest score wins.
Rules:
- No using other files (e.g.
reverse.txt) - Minimum code length is two characters.
- Your program cannot be a palindrome.
-
1\$\begingroup\$ Looks like this has been done before, just without the "no palindromes" rule. \$\endgroup\$Iszi– Iszi2013年12月18日 18:59:10 +00:00Commented Dec 18, 2013 at 18:59
-
1\$\begingroup\$ They are penalties. \$\endgroup\$ike– ike2013年12月18日 20:33:11 +00:00Commented Dec 18, 2013 at 20:33
-
10\$\begingroup\$ The one question I have, then, is why is pulling data from the internet given a larger penalty than reading the source file? \$\endgroup\$AJMansfield– AJMansfield2013年12月18日 20:35:36 +00:00Commented Dec 18, 2013 at 20:35
-
\$\begingroup\$ I posted the challenge codegolf.stackexchange.com/questions/16043/… which has tighter rules than this one, so every answer there would qualify as an answer here, but would not score as well. My current answer there is over 6000 characters. \$\endgroup\$hildred– hildred2013年12月19日 02:13:28 +00:00Commented Dec 19, 2013 at 2:13
-
1\$\begingroup\$ @lebatsnok You could move your comment to an answer now. :) \$\endgroup\$mbomb007– mbomb0072016年04月22日 16:07:24 +00:00Commented Apr 22, 2016 at 16:07
78 Answers 78
huh?, 5 characters
!hcuO
(削除) I actually have NO idea how it works, but (削除ここまで) If you download the interpreter, and if you write !hcuO, then you get Ouch!
To run this, you need to execute the program like this:
huh.exe !hcuO
It will actually look for a file called !hcuO, but it doesn't exist, so it outputs Ouch!
-
36\$\begingroup\$ Is it not just a little ironic that you don't understand how your own code works, in an esolang that by design isn't supposed to understand your code either? \$\endgroup\$Iszi– Iszi2013年12月18日 18:08:18 +00:00Commented Dec 18, 2013 at 18:08
-
\$\begingroup\$ @Iszi That's the joke. \$\endgroup\$AJMansfield– AJMansfield2013年12月18日 20:17:46 +00:00Commented Dec 18, 2013 at 20:17
-
10\$\begingroup\$ I'm not sure you're using
huh?right, though I'm having a hard time finding proper documentation. It seems the usage is intended to behuh.exe <path to source code>andOuch!is returned for an invalid path. Try putting your code into an actual file, and feeding that file as an argument tohuh?and see what happens. It's also interesting to see that it generates aNotes.txtfile with some commentary. \$\endgroup\$Iszi– Iszi2013年12月18日 21:59:53 +00:00Commented Dec 18, 2013 at 21:59 -
8\$\begingroup\$ Ok, this thing just told me it thinks it understands but I didn't see anything happen. Maybe I should stop toying with it on my primary system. \$\endgroup\$Iszi– Iszi2013年12月18日 22:04:16 +00:00Commented Dec 18, 2013 at 22:04
-
1\$\begingroup\$ What if there IS a file name !hcuO? \$\endgroup\$MilkyWay90– MilkyWay902019年05月15日 22:51:39 +00:00Commented May 15, 2019 at 22:51
Mathematica, 3 chars
a 2
a 2 means a times 2. So the answer is 2 a.
-
2\$\begingroup\$ This will probably win unless someone can get a two char solution. \$\endgroup\$ike– ike2013年12月20日 01:54:45 +00:00Commented Dec 20, 2013 at 1:54
-
34\$\begingroup\$ In fact I know a two char solution. Also in Mathematica:
1#. The output is#1. \$\endgroup\$alephalpha– alephalpha2013年12月20日 11:24:39 +00:00Commented Dec 20, 2013 at 11:24 -
8\$\begingroup\$ @alephalpha: Then you should post that as an answer! \$\endgroup\$user3094403– user30944032013年12月20日 12:07:21 +00:00Commented Dec 20, 2013 at 12:07
GolfScript - 2
1
(ie \n1 where \n is the newline character)
Output:
1
(ie 1\n)
To quote Ilmari:
GolfScript automatically appends a newline to the end of the output
Thus a newline followed by a number will print the number followed by a newline.
-
16\$\begingroup\$ +1 I didn't think anything could beat the Mathematica solution. \$\endgroup\$Kaya– Kaya2013年12月21日 19:55:54 +00:00Commented Dec 21, 2013 at 19:55
-
28\$\begingroup\$ Pretty boring solution... \$\endgroup\$user36219– user362192015年04月13日 17:26:55 +00:00Commented Apr 13, 2015 at 17:26
-
1\$\begingroup\$ @theonlygusti It's golfscript, whaddya expect... \$\endgroup\$2019年02月27日 14:39:06 +00:00Commented Feb 27, 2019 at 14:39
H9+, 13 characters
!dlrow ,olleH
As the web page says, all characters that are not H, 9 or + are ignored, so my program will print Hello, world!
-
32\$\begingroup\$ The one and only practical benefit of crazy esoteric languages is to answer these crazy questions. \$\endgroup\$totymedli– totymedli2013年12月19日 08:11:30 +00:00Commented Dec 19, 2013 at 8:11
-
\$\begingroup\$ It'd be more practical if these questions were so. \$\endgroup\$kojiro– kojiro2013年12月22日 18:50:31 +00:00Commented Dec 22, 2013 at 18:50
-
1\$\begingroup\$ That's pretty darned clever! \$\endgroup\$user36219– user362192015年04月13日 17:28:23 +00:00Commented Apr 13, 2015 at 17:28
-
\$\begingroup\$ According to the JS implementation,
Hseems to outputhello, world\n, therefore your code should be\ndlrow ,olleh, with no bytes added. \$\endgroup\$Makonede– Makonede2021年02月26日 21:56:02 +00:00Commented Feb 26, 2021 at 21:56
Javascript: 34 characters
reifitnedi detcepxenU :rorrExatnyS
outputs SyntaxError: Unexpected identifier, at least in the Chrome console
-
11\$\begingroup\$ Clever use of abusing the system through error abuse \$\endgroup\$Eliseo D'Annunzio– Eliseo D'Annunzio2013年12月19日 01:46:12 +00:00Commented Dec 19, 2013 at 1:46
-
2\$\begingroup\$ You sir, have made my day :D \$\endgroup\$major-mann– major-mann2013年12月19日 09:54:17 +00:00Commented Dec 19, 2013 at 9:54
-
4
-
1\$\begingroup\$ @Lohoris Sorry, I didn't see that one \$\endgroup\$scrblnrd3– scrblnrd32013年12月20日 14:58:59 +00:00Commented Dec 20, 2013 at 14:58
-
1\$\begingroup\$ Firebug console in Mozilla Firefox:
tnemetats erofeb ; gnissim :rorrExatnyS=>SyntaxError: missing ; before statement. \$\endgroup\$kenorb– kenorb2014年07月21日 13:09:42 +00:00Commented Jul 21, 2014 at 13:09
Python, (削除) 43 (削除ここまで) 41
_=']0~::[_%%_ tnirp;%r=_';print _%_[::~0]
Mathematica, 2 bytes
1#
Outputs:
#1
TI-BASIC, 2
i2
Where i is the imaginary number.
Outputs 2i
-
1\$\begingroup\$ This only works in the home screen, not inside a PRGM. \$\endgroup\$kernigh– kernigh2014年06月28日 03:27:42 +00:00Commented Jun 28, 2014 at 3:27
-
11\$\begingroup\$ @kernigh Did you try it? It works fine. (If the last statement of a TI-BASIC program evaluates as an expression, its result is printed instead of 'Done' when the program terminates) \$\endgroup\$AJMansfield– AJMansfield2015年04月13日 18:04:53 +00:00Commented Apr 13, 2015 at 18:04
BASIC, (削除) 22 (削除ここまで) (削除) 12 (削除ここまで) 7 characters
:-)
1 enil ni rorre xatnyS
EDIT: If you're allowed to enter the program in immediate mode, then this could be reduced to rorre xatnyS (12 characters).
In BBC BASIC, you only need 7 characters:
ekatsiM
ksh (21 chars)
$ dnuof ton :found :hsk
ksh: dnuof: not found
bash (31 chars)
$ dnuof ton dnammoc :found :hsab-
-bash: dnuof: command not found
sh (29 chars)
$ dnuof ton dnammoc :found :hs-
sh: dnuof: command not found
This one could not work on some Linux distributions, but works on OSX.
tcsh (26 chars)
$ .dnuof ton dnammoC :found.
.dnuof: Command not found.
csh (26 chars)
% .dnuof ton dnammoC :found.
.dnuof: Command not found.
Above should work on all *unix based OS.
Assumptions:
- You don't have
dnuofcommand or alias present.
bash (2-4 chars)
This one most likely doesn't qualify, but I'll share it as curiosity.
Assuming the previous shell command in Bash was $!. The following command:
!$
will produce: $!.
-
33\$\begingroup\$ Doesn't work for me, i have a command named
dnuof\$\endgroup\$Kroltan– Kroltan2013年12月19日 14:23:51 +00:00Commented Dec 19, 2013 at 14:23 -
8\$\begingroup\$ I forgot to add, it doesn't work on distributions which has
dnuofinstalled (whatever it is). Damn hackers. \$\endgroup\$kenorb– kenorb2013年12月19日 15:40:13 +00:00Commented Dec 19, 2013 at 15:40 -
6\$\begingroup\$ Hahaha, there's a command named
dnuof? :D what does it do? \$\endgroup\$Doorknob– Doorknob2013年12月19日 23:51:50 +00:00Commented Dec 19, 2013 at 23:51 -
2\$\begingroup\$ From the name I can tell
dnuof=(d)aily (n)ew (u)ser (of). This outputs how many users have been created on this computer today. Its arguments filter which computer(s) on the Internet to show for the daily new users. (The computer names are decoded by the program; each computer is given a different name.) (.dnuofis an alias fordnuof.) \$\endgroup\$user85052– user850522019年11月08日 06:57:13 +00:00Commented Nov 8, 2019 at 6:57 -
\$\begingroup\$ ha ha this is funny :)) \$\endgroup\$Tomas– Tomas2022年01月10日 15:11:10 +00:00Commented Jan 10, 2022 at 15:11
C++ 472 characters
A lot of characters but I cant think of a simpler way in a c-based language.
#include<iostream>
#include<string.h>
#define p(t) std::cout<<'}'<<';'<<')'<<strrev(&std::string(#t)[0])<<t;
char* strrev(char*p){char*t=p;char*q=p;while(q&&*q)++q;for(--q;p<q;++p,--q)*p=*p^*q,*q=*p^*q,*p=*p^*q;return t;}
int main(){p("(p{)(niam tni};t nruter;q*^p*=p*,q*^p*=q*,q*^p*=p*)q--,p++;q<p;q--(rof;q++)q*&&q(elihw;p=q*rahc;p=t*rahc{)p*rahc(verrts *rahc;t<<)]0[)t#(gnirts::dts&(verrts<<')'<<';'<<'}'<<tuoc::dts )t(p enifed#>h.gnirts<edulcni#>maertsoi<edulcni#");}
GolfScript, 12 chars
"-1%.`"-1%.`
This code takes the double-quoted string "-1%.`", reverses it (-1%), duplicates it (.) and un-evals (`) the second copy, restoring the double quotes around it.
Previous entry (13 chars):
{`'.~'+-1%}.~
Based on the 8-char quine {'.~'}.~ from this answer; the extra 5 chars are needed to stringify and reverse the output.
Ps. Note that GolfScript automatically appends a newline to the end of the output. If this is counted as part of the output, a corresponding newline can be prepended to either version of the code without affecting the output, for a cost of one extra char.
Befunge 98 - 10 chars
"8k,'!1+,@
This works if your interpreter does not interpret wrapped lines after " as adding an extra space. If your interpreter does interpret wrapped lines like that, then this 11 char solution works (because duplicate spaces in a string literal are interpreted as one):
"9k,'!1+,@
If I can use g without penalty, then these also work (7 and 8 chars respectively):
"5k,g,@
and
"6k,g,@
Fission, 6 bytes
A rare case of a generalised quine that is the same length as the normal quine:
"LO+!'
The idea is the same as that of the normal quine, but we're using a left-going atom (starting at the L) so that print mode traverses the code in the opposite order.
Ruby, 60
puts(2,s=<<2.chop.reverse,s)
puts(2,s=<<2.chop.reverse,s)
2
Based on a classic Ruby quine.
-
\$\begingroup\$ Nice use of heredocs. \$\endgroup\$Jon Purdy– Jon Purdy2013年12月23日 19:25:33 +00:00Commented Dec 23, 2013 at 19:25
Perl, 41
$_=q{print~~reverse"\$_=q{$_};eval"};eval
Old 52 character answer (27+25 penalty)
open+0;print ~~ reverse <0>
Reads its own source, stores the reverse in a scalar, and prints that.
-
1\$\begingroup\$ The
scalaroperator can be replaced by~~. However, you need to add +25 to your score for reading your own source code. \$\endgroup\$breadbox– breadbox2013年12月18日 19:26:58 +00:00Commented Dec 18, 2013 at 19:26 -
\$\begingroup\$ @breadbox noted \$\endgroup\$smcg– smcg2013年12月18日 19:39:09 +00:00Commented Dec 18, 2013 at 19:39
-
\$\begingroup\$ +1. I was about to post something like my shell solution:
$_='say"lave;047円",~~reverse,"047円=_\$"';eval, but your solution is shorter :-) Note that you can golf it down to 39 characters usingsay. Hope you will beat all the esoteric weirdness :-) \$\endgroup\$Tomas– Tomas2014年02月01日 20:26:50 +00:00Commented Feb 1, 2014 at 20:26 -
\$\begingroup\$ @Tomas are you suggesting replacing
printwithsay? That ends up not printing anything. \$\endgroup\$smcg– smcg2014年02月03日 14:58:55 +00:00Commented Feb 3, 2014 at 14:58 -
\$\begingroup\$ Did you run perl with
-Mfeature=sayoption? \$\endgroup\$Tomas– Tomas2014年02月03日 15:19:33 +00:00Commented Feb 3, 2014 at 15:19
J: 26
Standard quining (26 chars): by defining a function and passing it its own definition, in quotes:
|.(,],2#{:)'|.(,],2#{:)'''
Could probably be made shorter.
J-specific (33 chars): by defining a variable and asking what file the variable was defined in, i.e. this one, then printing out the contents of that file:
1!:2&2|.1!:1(4!:4 a=:<'a'){4!:3''
Must be saved & run from a script (i.e. not in the REPL, because then the answer to the question is "your argument wasn't defined in a file", so there's no file to read).
-
2\$\begingroup\$ The second one should get the + 25 bonus to read the own file. \$\endgroup\$Johannes Kuhn– Johannes Kuhn2013年12月18日 20:04:11 +00:00Commented Dec 18, 2013 at 20:04
-
2\$\begingroup\$ @JohannesKuhn: Penalty, but yeah. \$\endgroup\$jazzpi– jazzpi2013年12月19日 10:08:26 +00:00Commented Dec 19, 2013 at 10:08
-
1\$\begingroup\$ "Bonus" sounds nicer. \$\endgroup\$Johannes Kuhn– Johannes Kuhn2013年12月19日 10:15:50 +00:00Commented Dec 19, 2013 at 10:15
><>, 25 bytes
I was surprised to find this hadn't been done yet. :)
...yhsif sllems gnihtemoS
Paste code here and run it.
. is the Jump command, popping x and y off the stack, and moving the IP to (x, y) in the code box. In this case, the stack is empty, so the language's only error message is printed:
Something smells fishy...
-
1\$\begingroup\$ The best answer \$\endgroup\$Insane– Insane2016年04月24日 18:28:33 +00:00Commented Apr 24, 2016 at 18:28
Microscript, 11 bytes
I kind of had to do this.
0"Caxq"Caxq
Surprisingly, this is actually shorter than the language's shortest known true quine. q and a are otherwise equivalent, except q adds wrapping quotes while a does not.
Windows 10 .EXE, 98 bytes
.rehsilbup erawtfos eht htiw kcehc ,CP ruoy htiw noisrev a dnif oT
.CP ruoy no nur t'nac ppa sihT
-
1\$\begingroup\$ Is there a version for my PC? \$\endgroup\$The Empty String Photographer– The Empty String Photographer2023年10月10日 14:48:12 +00:00Commented Oct 10, 2023 at 14:48
PHP, 41 characters (+25)
Don't know if I understood the assignment correctly. But here's a PHP try:
while(!isset($s) || $s) echo isset($s) ? array_pop($s) : ($s = str_split(file_get_contents(__FILE__)) and null);
edit: this can be much shorter:
echo strrev(file_get_contents(__FILE__));
But since it can be that simple, this is probably not what is being asked...
-
\$\begingroup\$ phpFiddle: phpfiddle.org/main/code/fuy-mv0 \$\endgroup\$nl-x– nl-x2013年12月18日 20:24:32 +00:00Commented Dec 18, 2013 at 20:24
-
\$\begingroup\$ When I click run, I get a whole bunch of useless nonsense (here is a short snippet of it:
>? ;)llun dna ))__ELIF__(f$(tilps_rts = s$( : )s$(pop_yarra ? )s$(tessi ohce )s$ || )s$(tessi!(elihw ;"stnetnoc_"=.f$ ;'teg_elif'=f$ php?<>?}};ESLAF nruter;"ec6x\i66x02円x\e47x96円x\rw; there is much much more). Also, include your character count; this is a code golf. Also, it seems that you are reading your source file, so add 25 to your character count and that is your score. Lowest score wins. \$\endgroup\$Justin– Justin2013年12月18日 21:30:26 +00:00Commented Dec 18, 2013 at 21:30 -
\$\begingroup\$ @Quincunx First off, it is backwards ... but secondly, this is the source code that phpfiddle generates! (but backwards...) They seem to escape a lot of thing, trying to keep things safe. Replace
array_popwitharray_shiftto see the source code not backwards... \$\endgroup\$nl-x– nl-x2013年12月18日 21:33:14 +00:00Commented Dec 18, 2013 at 21:33 -
\$\begingroup\$ Yes I can see the reversed source code, but what is with all the extraneous text? There are a lot of escape sequences, but there are some that aren't (eg:
ESLAF nruteriereturn FALSE). \$\endgroup\$Justin– Justin2013年12月18日 21:36:33 +00:00Commented Dec 18, 2013 at 21:36 -
\$\begingroup\$ @Quincunx Sorry, I'm not catching if you are showing interest in the way phpFiddle works, or if you are criticizing my code. If it's the latter, then in my defense, put my code in a .php file and run it in a browser, and it'll work cleanly. I just thought phpfiddle was a nice thing to let you see, so you can see a bit of it's inner workings. \$\endgroup\$nl-x– nl-x2013年12月18日 21:42:27 +00:00Commented Dec 18, 2013 at 21:42
UNIX shell, 31
Real solution at 52 characters:
A='printf "A$ lave;047円`echo $A|rev`047円=A"';eval $A
But beware! Honesty doesn't pay off in today's world! Penalty is too low!!
6 chars + 25 = 31:
rev 0ドル
-
\$\begingroup\$ On OSX
rev 0ドルgenerates the error:rev: illegal option -- b. \$\endgroup\$kenorb– kenorb2014年07月21日 13:21:02 +00:00Commented Jul 21, 2014 at 13:21
MATLAB, 78 characters:
|
.snoisserpxe ro stnemetats BALTAM ni dilav ton si retcarahc tupni ehT :rorrE
Note that the solution requires you to begin with a special character (alt+0160) and that it prints exactly the reversed message. (Unlike the python solution)
JavaScript jQuery (削除) 119 (削除ここまで) (削除) 92 (削除ここまで) (削除) 74 (削除ここまで) 70 characters
alert($("#answer-16051 pre code").text().split("").reverse().join(""))
Now using jQuery, as minitech suggested in the comments, and manually wrapping with <pre><code> so I can use text() without fear of other code blocks in this post interfering. Manually wrapping with <h4> was incompatible with chromeium when I tested it, so now it should work in most browsers.
This program, if run from this page, finds the code block directly above, reverses its contents, and puts it in an alertbox.
Its easy enough to verify, just paste it into the dev console.
-
\$\begingroup\$ This is way longer than just using a function. And you should use jQuery (on this page) or at least
querySelectoranyways... \$\endgroup\$Ry-– Ry-2013年12月19日 17:58:47 +00:00Commented Dec 19, 2013 at 17:58 -
\$\begingroup\$ @minitech thanks for the tip, I swapped it for a
querySelectornow. I would like to look into the jQuery possibility, but it looks like it will take a little longer for me to figure out. \$\endgroup\$AJMansfield– AJMansfield2013年12月19日 18:37:03 +00:00Commented Dec 19, 2013 at 18:37 -
\$\begingroup\$ @minitech ok, thanks for the
jQuerytip. (I only started learning javascript yesterday.) \$\endgroup\$AJMansfield– AJMansfield2013年12月19日 18:45:56 +00:00Commented Dec 19, 2013 at 18:45 -
\$\begingroup\$ I like this solution; thinking outside the box. \$\endgroup\$user36219– user362192015年04月13日 17:35:59 +00:00Commented Apr 13, 2015 at 17:35
MS-DOS, 24 bytes
eman elif ro dnammoc daB
Output:
Bad command or file name
Windows command prompt, 93 bytes
.elif hctab ro margorp elbarepo ,dnammoc lanretxe ro lanretni na sa dezingocer ton si 'file.'
JavaScript, 62
function f(){alert((f+'f()').split('').reverse().join(''))}f()
Works for me on latest Chrome (v 31.0.1650.63). Some other browsers may give a different output. (If you reverse that output, then it would work :P)
SmileBASIC, (削除) 118 (削除ここまで) 102 bytes
FOR I=-101TO.?MID$(("+CHR$(34))*3,30,102)[-I];:NEXTFOR I=-100TO.?MID$(("+CHR$(34))*3,30,102)[-I];:NEXT
Japt, 12 bytes
"iQ 2w"iQ 2w
Based off the standard Japt quine
Explanation
"iQ 2w" // Take this string. iQ 2w
iQ // Insert a quote. "iQ 2w
2 // Double. "iQ 2w"iQ 2w
w // Reverse. w2 Qi"w2 Qi"
// Implicitly output.
-
\$\begingroup\$ Congrats on your 5th Japt solution; bounty on its way. \$\endgroup\$Shaggy– Shaggy2019年02月26日 13:30:04 +00:00Commented Feb 26, 2019 at 13:30
-
2\$\begingroup\$ @Shaggy wait >_> I wasn't doing this for the bounty \$\endgroup\$ASCII-only– ASCII-only2019年02月26日 21:32:11 +00:00Commented Feb 26, 2019 at 21:32
-
1\$\begingroup\$ Happy coincidentally for you, so, that there's one going :) \$\endgroup\$Shaggy– Shaggy2019年02月26日 22:20:00 +00:00Commented Feb 26, 2019 at 22:20
JavaScript, 56
($=_=>_!=$._?_?$(_.slice(1))+_[0]:')':$('($='+$+')('))()
-
\$\begingroup\$ Please state your environment because this does not work in Chrome. Is this Rhino or what? \$\endgroup\$George Reith– George Reith2013年12月20日 13:39:03 +00:00Commented Dec 20, 2013 at 13:39
-
\$\begingroup\$ @GeorgeReith: Anything with ES6 arrow function support. All SpiderMonkeys should work fine, for example (Rhino included). \$\endgroup\$Ry-– Ry-2013年12月20日 14:42:56 +00:00Commented Dec 20, 2013 at 14:42