57
\$\begingroup\$

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
\$\endgroup\$
  • 7
    \$\begingroup\$ Is the true/false output a strong requirement, or are variations (True/False, 1/0) acceptable as well? \$\endgroup\$ – Cristian Lupascu Apr 17 '13 at 14:54
  • \$\begingroup\$ Is it a problem if the program outputs a little more than true/false (if it keeps being unambiguous and ends with true/false) ? \$\endgroup\$ – Denys Séguret Apr 19 '13 at 11:31
  • 1
    \$\begingroup\$ Related: Interpret your lang, but not yourself? \$\endgroup\$ – Ilmari Karonen Jan 11 '14 at 5:28
  • 5
    \$\begingroup\$ So you mean a Narcissist program? \$\endgroup\$ – PyRulez Jul 15 '17 at 5:57

36 Answers 36

0
\$\begingroup\$

V, 19 bytes

ñOÑ~"qpxØ¥^¨©î±¥$

Try it online!

Prints 1 or 0. Here is a hexdump:

00000000: f14f d11b 7e22 7170 78d8 a55e a881 a9ee  .O..~"qpx..^....
00000010: b1a5 24                                  ..$
\$\endgroup\$
0
\$\begingroup\$

Java 8, 288 bytes (Full Program with STDIN)

interface M{static void main(String[]a){String s="interface M{static void main(String[]a){String s=%c%s%1$c;System.out.print(s.format(s,34,s).equals(new java.util.Scanner(System.in).nextLine()));}}";System.out.print(s.format(s,34,s).equals(new java.util.Scanner(System.in).nextLine()));}}

Try it online.

Java 8, 210 bytes (Full Program with Console Arguments)

interface M{static void main(String[]a){String s="interface M{static void main(String[]a){String s=%c%s%1$c;System.out.print(s.format(s,34,s).equals(a[0]));}}";System.out.print(s.format(s,34,s).equals(a[0]));}}

Try it online.

Java 8, 108 bytes (Function)

i->{String s="i->{String s=%c%s%1$c;return s.format(s,34,s).equals(i);}";return s.format(s,34,s).equals(i);}

Try it online.

Explanation:

-part:

  • The String s contains the unformatted source code.
  • %s is used to input this String into itself with the s.format(...).
  • %c, %1$c and the 34 are used to format the double-quotes.
  • s.format(s,34,s) puts it all together

Challenge-part:

  • .equals(...) checks if this formatted source code equals the input.
    • java.util.Scanner(System.in).nextLine() is used as this input for STDIN
    • a[0] is used as this input for Console arguments
    • i is used as this input for the lambda function
\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

Pascal (FPC), 167 157 bytes

const b=#39';var s:string;begin read(s);write(s=a+b+#39#59#97#61#39+a+b)end.';a='const b=#39';var s:string;begin read(s);write(s=a+b+#39#59#97#61#39+a+b)end.

Try it online!

While substring can be easily extracted, like in my regular quine, unfortunately, they cannot be concatenated because FPC thinks it is an array in these circumstances. In a and b are characters before and after constant definitions. #39#59#97#61#39 is a replacement for ';a=' as this is not represented in the constants. #39 represents ' and is at the start of b, glued together to the rest of b, to shorten the comparison expression as much as possible.

\$\endgroup\$
0
\$\begingroup\$

Zsh, 33 bytes

f () {
	[ "`type -f f`" = $1 ]
}

Try it online!

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.

\$\endgroup\$
0
\$\begingroup\$

TeX, 55 bytes

The file has to be saved as a.tex and should be run using pdftex a.tex. The script doesn't terminate after returning 1 or 0, if that should be necessary append it by \end (+4 bytes). If it should work for arbitrary file names replace \openin0a with \openin0\jobname (+7 bytes).

\openin0=a\read0to\0\read1to~\message{\ifx\0~1\else0\fi}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.