62
\$\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
  • \$\begingroup\$ Very much related \$\endgroup\$ – Deadcode Jan 20 at 7:37

40 Answers 40

1
2
1
\$\begingroup\$

Keg, 36 28 26 bytes

`:&\`ⁿ^\`ⁿ&⅀=`:&\`ⁿ^\`ⁿ&⅀=

Try it online!

Same concept as before, but shorter.

Answer History

`:&\`ⁿ⅍^\`ⁿ⅍++&+¿=`:&\`ⁿ⅍^\`ⁿ⅍++&+¿=

Try it online!

Who knew generating a program that knows itself would be so unreadable!

Explained

The very first step here is to generate the string that will be checked against. At this point, we don't particularly know what the checking part will look like, but we do know that there will be a string that looks like so:

`\`ⁿ⅍^\`ⁿ⅍++`

Which would consequently be followed by:

 \`ⁿ⅍^\`ⁿ⅍++

This creates a string which contains itself quoted... Something needed for when input is taken, as the quotes are going to be present.

From here, string equality is quite easy, coming in at a trivial 2 bytes:

¿=

Incorporating this into the string gives:

`\`ⁿ⅍^\`ⁿ⅍++¿=`\`ⁿ⅍^\`ⁿ⅍++¿=

At this stage, one might think the quine is complete... It has the string part and it has the equality part... What else could be needed? Well. Let me tell you we ain't done yet.

Y'see, we have a quoted string on the stack, but we don't have it's unquoted equivalent present, meaning we need to account for that. The register works well here:

`:&\`ⁿ⅍^\`ⁿ⅍++&+¿=`:&\`ⁿ⅍^\`ⁿ⅍++&+¿=

And there you have it: a self identifying program written completely on a phone keyboard.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ It still outputs 1 if the input is followed by a newline and then anything else \$\endgroup\$ – Jo King Jan 19 at 23:32
  • \$\begingroup\$ Well of course it would, because Keg only reads one line at a time. I'll have to clarify input methods with Keg, because otherwise multi line input isn't possible. \$\endgroup\$ – Lyxal Jan 19 at 23:38
0
\$\begingroup\$

PowerShell, 28 bytes

Very similar to this JavaScript answer, uses function provider.

filter f{"$Function:f"-eq$_}

Example

PS > '"$Function:f"-eq$_' | f
True

PS > 'xxx' | f
False
| improve this answer | |
\$\endgroup\$
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                                  ..$
| improve this answer | |
\$\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
| improve this answer | |
\$\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
| improve this answer | |
\$\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.

| improve this answer | |
\$\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.

| improve this answer | |
\$\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}
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Python 3.8 (pre-release), 42 bytes

exec(s:="print('exec(s:=%r)'%s==input())")

Try it online!

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Japt, 22 16 14 bytes

"iQ ²¶U"iQ ²¶U

Try it online!

Explanation

"iQ ²¶U"iQ ²¶U
"iQ ²¶U"        // Take this string.        iQ ²¶U
        iQ      // Insert a quote.          "iQ ²¶U
           ²    // Double.                  "iQ ²¶U"iQ ²¶U
            ¶U  // Check if equal to input.
| improve this answer | |
\$\endgroup\$
1
2

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.