Write a program that will generate a "true" output iff the input matches the source code of the program, and which generates a "false" output iff the input does not match the source code of the program.
This problem can be described as being related to quines, as the program must be able to somehow compute its own source code in the process.
This is code golf: standard rules apply. Your program must not access any special files, such as the file of its own source code.
Edit: If you so choose, true/false can be replaced with True/False or 1/0.
Example
If the source code of your program is bhiofvewoibh46948732));:/)4, then here is what your program must do:
Input (Stdin)
bhiofvewoibh46948732));:/)4
Output (Stdout)
true
Input
(Anything other than your source code)
Output
false
49 Answers 49
JavaScript : 26
function f(s){return s==f}
I don't know if a JavaScript file really qualifies as a "program".
-
8\$\begingroup\$ +1 This works because all objects have a
.toString()method in JavaScript. \$\endgroup\$Andrew Larsson– Andrew Larsson2013年12月19日 01:33:25 +00:00Commented Dec 19, 2013 at 1:33 -
1\$\begingroup\$ This can be shortened using an arrow function
f=s=>s=='f='+f\$\endgroup\$Jonathan– Jonathan2018年10月03日 16:03:20 +00:00Commented Oct 3, 2018 at 16:03 -
8\$\begingroup\$ @Jonathan yes. But in 2013 it couldn't... \$\endgroup\$Denys Séguret– Denys Séguret2018年10月03日 16:21:12 +00:00Commented Oct 3, 2018 at 16:21
Haskell, 72 characters
main=interact$show.(==s++show s);s="main=interact$show.(==s++show s);s="
Note: there is no end-of-line character at the end of the script.
$ runhaskell Self.hs < Self.hs
True
Perl, (削除) Infinity (削除ここまで) (削除) 41 (削除ここまで) 38 Characters
$_=q(print<>eq"\$_=q($_);eval"|0);eval
Update: The program no longer ends with a newline, which means it will work correctly on multi-line files. You have to enter input from STDIN without hitting enter. On Windows I was only able to do this by reading from a file.
Original solution:
print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(print<>==q(...
-
1\$\begingroup\$ Nicely done! . . . \$\endgroup\$mob– mob2013年04月19日 16:19:46 +00:00Commented Apr 19, 2013 at 16:19
-
\$\begingroup\$ Fails for a file that begins with the code, e.g.
(cat id.pl; echo foo)|perl id.pl\$\endgroup\$Geoff Reedy– Geoff Reedy2013年04月20日 05:27:19 +00:00Commented Apr 20, 2013 at 5:27 -
\$\begingroup\$ @GeoffReedy, thanks; the program did not handle multi-line input before. It is corrected now. \$\endgroup\$user7486– user74862013年04月20日 07:56:25 +00:00Commented Apr 20, 2013 at 7:56
-
\$\begingroup\$ Darn, is this code bowling? \$\endgroup\$Matthew Roh– Matthew Roh2017年07月26日 14:08:42 +00:00Commented Jul 26, 2017 at 14:08
><>, 68 bytes
Fishes love eating fish poop. Now we know they can distinguish theirs from their friends'.
00v 0+1~$^?)0~\;n0\
>:@@:@gi:0(?\:a=?/=?!/1ドル+
0n;n*=f$=2~~/
You can try it online !
-
1\$\begingroup\$ This outputs
1for any prefix of the code as well \$\endgroup\$Jo King– Jo King2018年09月30日 03:12:47 +00:00Commented Sep 30, 2018 at 3:12 -
\$\begingroup\$ @JoKing it was worst than just prefixes of the script, it accepted truncated lines too ! I've fixed it, but I'm disappointed it's not as generic as I wanted it to be, I had to check the reached cell at the end of the script to make sure the whole code was matched. It can certainly be improved, but I'm not sure I will bother. \$\endgroup\$Aaron– Aaron2018年10月03日 15:09:04 +00:00Commented Oct 3, 2018 at 15:09
GolfScript, 11 chars
{`".~"+=}.~
Without the =, this code would be a quine that generates its own source code as a string. The = makes it compare this string to its input and output 1 if they match and 0 if they don't.
Note that the comparison is exact — in particular, a trailing newline at the end of the input will cause it to fail.
Explanation:
{ }is a code block literal in GolfScript;.duplicates this code block, and~executes the second copy (leaving the first on the stack);`stringifies the code block, and".~"+appends.~to it;- finally,
=compares the resulting string with the input (which is pushed on the stack as a string by the GolfScript interpreter before the program starts) and returns1if they match and0if they don't.
JavaScript ES6, (削除) 16 (削除ここまで) 14 bytes
$=_=>_==`$=`+$
Minus two bytes thanks to Neil.
31 bytes if we must take input via prompt.
$=_=>prompt()==`$=${$};$()`;$()
38 bytes if we must output via alert.
$=_=>alert(prompt()==`$=${$};$()`);$()
This is the proper way to do it, as Optimizer's answer does not accept the entire source code.
-
1\$\begingroup\$ Nice, although I would just write
'$='+$. \$\endgroup\$Neil– Neil2016年04月04日 22:53:57 +00:00Commented Apr 4, 2016 at 22:53 -
\$\begingroup\$ Oh, true. @Neil \$\endgroup\$Conor O'Brien– Conor O'Brien2016年04月04日 22:55:16 +00:00Commented Apr 4, 2016 at 22:55
-
1\$\begingroup\$ I'm pretty sure you need the ending
;$()because the function call is part of the quine. This also means that you'll need to switch topromptto account for input. \$\endgroup\$Mama Fun Roll– Mama Fun Roll2016年04月05日 05:14:16 +00:00Commented Apr 5, 2016 at 5:14 -
1\$\begingroup\$ That's not the problem. The function call is necessary because it's a part of the quine. Allowing the user to call it as a function would ruin the quine. \$\endgroup\$Mama Fun Roll– Mama Fun Roll2016年04月05日 13:32:41 +00:00Commented Apr 5, 2016 at 13:32
-
1\$\begingroup\$ try
$=_=>prompt()==`$=${$};$()`;$()\$\endgroup\$Mama Fun Roll– Mama Fun Roll2016年04月05日 16:28:57 +00:00Commented Apr 5, 2016 at 16:28
Python 2, 55
a='a=%r;print a%%a==raw_input()';print a%a==raw_input()
Tested:
a='a=%r;print a%%a==raw_input()';print a%a==raw_input() -> True
(anything else) -> False
-
3\$\begingroup\$ Fails on any file that begins with the first line equal to
a='a=%r;print a%%a==raw_input()';print a%a==raw_input(). \$\endgroup\$boothby– boothby2013年04月17日 18:56:02 +00:00Commented Apr 17, 2013 at 18:56 -
\$\begingroup\$ True, multi-line input is not supported. \$\endgroup\$flornquake– flornquake2013年04月17日 20:06:06 +00:00Commented Apr 17, 2013 at 20:06
-
\$\begingroup\$ A trivial fix would be to replace
raw_input()with__import__('sys').stdin.read(). \$\endgroup\$feersum– feersum2015年02月05日 22:53:26 +00:00Commented Feb 5, 2015 at 22:53 -
\$\begingroup\$ I am confused by the challenge wording ('cause I am not good with english grammar). Is this allowed?
print raw_input()==open(__file__).read()? It is only 40 bytes, uses yourraw_input()approach, but reads it's code. \$\endgroup\$Simon– Simon2018年01月29日 14:12:24 +00:00Commented Jan 29, 2018 at 14:12 -
1\$\begingroup\$ @Simon That is not allowed, it is one of the standard loopholes for challenges like this. And yes, this is what it means by
Your program must not access any special files, such as the file of its own source code.\$\endgroup\$PunPun1000– PunPun10002018年02月23日 18:28:29 +00:00Commented Feb 23, 2018 at 18:28
Node.js : 54
function f(){console.log(f+'f()'==process.argv[2])}f()
You test it by saving it into a file f.js (the exact name has no importance) and using
node f.js "test"
(which outputs false) or
node f.js "$(< f.js)"
(which outputs true)
I also made a different version based on eval :
eval(f="console.log('eval(f='+JSON.stringify(f)+')'==process.argv[2])")
It's now at 72 chars, I'll try to shorten that when I have time.
-
1\$\begingroup\$ @dan1111 Why ? It doesn't access any file. I was just pointing how to launch the program to people not used to node.js. It doesn't read the file. \$\endgroup\$Denys Séguret– Denys Séguret2013年04月18日 06:49:11 +00:00Commented Apr 18, 2013 at 6:49
-
1\$\begingroup\$ All of the Javascript solutions take advantage of the fact that you can access your own source code in JS. That may not be technically "accessing the file of its own source code", but it accomplishes the exact same thing. I suppose your answer is legal, though, as the question didn't specifically forbid this. \$\endgroup\$user7486– user74862013年04月19日 09:32:56 +00:00Commented Apr 19, 2013 at 9:32
-
\$\begingroup\$ Well, you access the source of a function (only the body to be precise) which is part of the program. That's like using mixin() in D. But I don't think the two other JS answers, including one from me, really qualify as "programs", though. \$\endgroup\$Denys Séguret– Denys Séguret2013年04月19日 09:36:51 +00:00Commented Apr 19, 2013 at 9:36
-
\$\begingroup\$ @dystroy actually mixin in D is more like using eval than reading source \$\endgroup\$ratchet freak– ratchet freak2013年04月19日 09:51:44 +00:00Commented Apr 19, 2013 at 9:51
-
\$\begingroup\$ @ratchetfreak yes, you're right. But I think your program uses a kind of toString of the enum value, right ? And any code using eval/mixin is about the same trick than using the source of the function. \$\endgroup\$Denys Séguret– Denys Séguret2013年04月19日 09:54:18 +00:00Commented Apr 19, 2013 at 9:54
Smalltalk (Pharo 2.0 dialect), 41 bytes
Implement this 41 chars method in String (ugly formatting for code-golf):
isItMe^self=thisContext method sourceCode
Then evaluate this in a Workspace (printIt the traditional Smalltalk way)
The input is not read from stdin, it's just a String to which we send the message (what else a program could be in Smalltalk?):
'isItMe^self=thisContext method sourceCode' isItMe.
But we are cheating, sourceCode reads some source file...
Here is a variant with 51 chars which does not:
isItMe
^ self = thisContext method decompileString
And test with:
'isItMe
^ self = thisContext method decompileString' isItMe
If a String in a Workspace is not considered a valid input, then let's see how to use some Dialog Box in 116 chars
Just evaluate this sentence:
(UIManager default request: 'type me') = (thisContext method decompileString withSeparatorsCompacted allButFirst: 7)
Since decompile format includes CR and TAB, we change that withSeparatorsCompacted.
Then we skip the first 7 chars are 'doIt ^ '
Finally a 105 chars variant using stdin, just interpret this sentence from command line, just to feel more mainstream:
Pharo -headless Pharo-2.0.image eval "FileStream stdin nextLine = (thisContext method decompileString withSeparatorsCompacted allButFirst: 7)"
flex - 312 chars
Q \"
N \n
S " "
B \\
P "Q{S}{B}{Q}{N}N{S}{B}n{N}S{S}{Q}{S}{Q}{N}B{S}{B}{B}{N}P{S}{Q}{P}{Q}{N}M{S}{Q}{M}{Q}{N}%%{N}{P}{N}{M}{N} putchar('1');"
M "(.|{N})* putchar('0');"
%%
Q{S}{B}{Q}{N}N{S}{B}n{N}S{S}{Q}{S}{Q}{N}B{S}{B}{B}{N}P{S}{Q}{P}{Q}{N}M{S}{Q}{M}{Q}{N}%%{N}{P}{N}{M}{N} putchar('1');
(.|{N})* putchar('0');
Can probably be made shorter, but it works with multi-line input (necessary since the source code is multiple lines) and even for inputs that contain the program as a substring. It seems many of the answers so far fail on one or both of these.
Compile command: flex id.l && gcc -lfl lex.yy.c
D (133 chars)
enum c=q{import std.stdio;import std.algorithm;void main(){auto i=readln();writeln(equal("auto c=q{"~c~"};mixin(c);",i));}};mixin(c);
JavaScript (V8), 35
function i(){alert(prompt()==i+[])}
call i() and it will prompt for input
-
\$\begingroup\$ The
+[]should be optional as JS will auto-type-cast it \$\endgroup\$Downgoat– Downgoat2016年01月18日 18:10:38 +00:00Commented Jan 18, 2016 at 18:10
GolfScript - 26
":@;[34]@+2*=":@;[34]@+2*=
Inspired from http://esolangs.org/wiki/GolfScript#Examples
Another version:
"[34].@@;+2*="[34].@@;+2*=
Too bad that \ is both swap and escape...
Python 2, 47 bytes
_='_=%r;print _%%_==input()';print _%_==input()
A simple quine with the added check.
-
\$\begingroup\$ This doesn't work.
printis a function is Python 3. You'd need to doprint(_%%_==input())';print(_%_==input())or change it to Python 2. \$\endgroup\$user45941– user459412016年07月22日 05:52:34 +00:00Commented Jul 22, 2016 at 5:52
CJam, 12 bytes
{s"_~"+q=}_~
Explanation
This just uses the standard CJam quine framework.
{s"_~"+q=} e# Push this block (function literal).
_~ e# Copy and run it.
What the block does:
s e# Stringify the top element (this block itself).
"_~"+ e# Append "_~". Now the source code is on the stack.
q e# Read the input.
= e# Check if it equals the source code.
-
\$\begingroup\$ This is exactly the solution I had ._. \$\endgroup\$Esolanging Fruit– Esolanging Fruit2018年03月27日 07:45:39 +00:00Commented Mar 27, 2018 at 7:45
Python, 187 bytes
import sys;code="import sys;code=!X!;print(sys.stdin.read()==code.replace(chr(33),chr(34)).replace(!X!,code,1))";print(sys.stdin.read()==code.replace(chr(33),chr(34)).replace("X",code,1))
Careful not to add newline at the end. Someone with better Python-fu might be able to shorten it.
-
2\$\begingroup\$ You can use
C=chrto drop several bytes. Also, shorten the variable namecode. \$\endgroup\$Zach Gates– Zach Gates2015年09月19日 03:06:41 +00:00Commented Sep 19, 2015 at 3:06 -
2\$\begingroup\$ Since nobody said it for over a year, Welcome to PPCG! \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2016年10月16日 14:41:11 +00:00Commented Oct 16, 2016 at 14:41
Tcl, 111 chars
set c {set c {$c};puts [expr {[read stdin] eq [subst -noc \$c]}]};puts [expr {[read stdin] eq [subst -noc $c]}]
Perl, 52 char
$_='$/=$\;$_="\$_=47円$_47円;eval";print<>eq$_|0';eval
Husk, 11 bytes
=hS+s"=hS+s
Explanation
The explanation uses ̈ to delimit strings (to avoid unreadable escaping):
"=hS+s -- string literal: ̈=hS+s ̈
S+ -- join itself with
s -- | itself "showed": ̈"=hS+s" ̈
-- : ̈=hS+s"=hS+s" ̈
h -- init: ̈=hS+s"=hS+s ̈
= -- is the input equal?
By removing the function = you can verify that it will indeed only match the source itself.
Jelly, 10 bytes
"Ṿ;$="Ṿ;$=
"Ṿ;$="Ṿ;$=
"Ṿ;$=" String literal: 'Ṿ;$='
$ Next two links act on the string literal
Ṿ Uneval: '"Ṿ;$="'
; Append string: '"Ṿ;$="Ṿ;$=' (source code)
= Is the string above equal to the input?
Zsh, 33 bytes
f () {
[ "`type -f f`" = 1ドル ]
}
The extra spaces, tab, and newlines are required. type -f f does not print the original source, but the function formatted in a particular way, with indentation and a trailing newline.
R, 54 bytes
f=function(s)s==paste0("f=function(s)s==", body(f)[3])
body gets the body of the function (splitting it a bit, so that body(f)[3] is everything from paste0 onwards). Interestingly, body reformats the code, adding spaces after commas, etc. This is thus a rare case of an R golf answer with a space after a comma.
This works because body(f) is an object of type language, and there exists an as.character method for this type. On the other hand, f and args(f) are of type closure, and cannot be converted to character type as far as I can tell. Please don't ask me what the language type is for...
-
\$\begingroup\$ As we've noted before, R's code-reformatting is quite version-dependent. My old R version 3.2.1 let's me use
f=function(x)x==paste0('f=',capture.output(f))successfully for only 46 bytes, but the later version in TIO fills it up with annoying spaces & newlines... \$\endgroup\$Dominic van Essen– Dominic van Essen2021年01月03日 11:59:22 +00:00Commented Jan 3, 2021 at 11:59 -
\$\begingroup\$ @DominicvanEssen Good find!
capture.outputworks as you would want on my later version of R (3.6.3), but it adds a bytecode at the end. The output is thus a vector of 2 logicals: the first one corresponds to what you want, the second tests equality with the bytecode. I believe this is acceptable: in R, a vector of two logicals is truthy iff the first logical isTRUE(per the definition along the lines of "how wouldifhandle it?). You should post this separately! \$\endgroup\$Robin Ryder– Robin Ryder2021年01月03日 15:35:12 +00:00Commented Jan 3, 2021 at 15:35 -
\$\begingroup\$ Thanks very much for checking that! It seems that only specific R versions don't work (annoyingly including the one at TIO) - I now tested version 4 and it works Ok, too. So I've posted a separate version-dependent R answer now. \$\endgroup\$Dominic van Essen– Dominic van Essen2021年01月04日 13:59:07 +00:00Commented Jan 4, 2021 at 13:59
R (version-dependent), 46 bytes
f=function(x)x==paste0('f=',capture.output(f))
capture.output - as the function name suggests - retrieves the text string that results from entering the function name f directly to R, which should output the body of the function. We manually prepend this with f= so that it exactly* matches the program code, and check whether the function argument x is the same as this.
*See below
There are a couple of caveats, though, that depend on the R version used:
- Early versions of R (like my own version 3.2.1) return the originally-defined function body as-is when the function name is entered to R. However, later versions add additional formatting, like inserting spaces and newlines, which breaks this code: this unfortunately includes the installation (3.5.2) on 'Try it online'. Later still, much newer R versions (I tested 4.0.3) return to the as-is output formatting and everything is Ok again... except...
- The most recent versions of R compile functions at the first run, and thereafter append a 'byte code' specific for the compiled function to the outputted function body. This means that
capture.outputreturns two strings, beginning at the second time the function is run. Luckily, the function body is still the first one (the byte code is the second one), so the result of supplying the program code as input isTRUE FALSE, which evaluates to 'truthy' in R and so counts as a 'true' output for this challenge.
Special thanks to Robin Ryder for helping to uncover the version-dependence of R's function outputting.
Vyxal, 12 bytes
`:q$+=`:q$+=
I finished making this, and then realized that it is almost identical to the Vyxal quine that @Lyxal wrote, but with the addition of = to check if the input is equal.
Explanation:
# Implicit input
`:q$+=` # Push the string ':q$+='
: # Duplicate the string
q # Quotify; put backticks around the topmost string
$ # Swap the top two values on the stack
+ # Concatenate the two strings
= # Check if the input is equal to the string
# Implicit output
C - (削除) 186 (削除ここまで) 176 characters
One liner:
*a="*a=%c%s%c,b[999],c[999];main(){sprintf(b,a,34,a,34);gets(c);putchar(strcmp(b,c)?'0':'1');}",b[999],c[999];main(){sprintf(b,a,34,a,34);gets(c);putchar(strcmp(b,c)?'0':'1');}
With whitespace (note that this breaks the program):
*a="*a=%c%s%c,b[999],c[999];main(){sprintf(b,a,34,a,34);gets(c);putchar(strcmp(b,c)?'0':'1');}",b[999],c[999];
main() {
sprintf(b,a,34,a,34);
gets(c);
putchar(strcmp(b,c)?'0':'1');
}
JavaScript ES6, 14 bytes
Like the other javascript answer but includes it's entire source code and not just the function
a=q=>'a='+a==q
Example usage:
a("q=>'a='+a==q") // returns false
a("a=q=>'a='+a==q") // returns true
true/falseoutput a strong requirement, or are variations (True/False,1/0) acceptable as well? \$\endgroup\$