21
\$\begingroup\$

Given a string, return whether the string is a substring of the program's source code.

Standard quine rules apply, meaning you cannot read your own source code. The length of the input is guaranteed to be less than or equal to the length of the program. You may return any two distinct values, not necessarily truthy and falsey values. You may also submit a function, rather than a full program.

This is a so shortest code wins!

An example

If your source code is print(input() = False), it should return True for nt(i but False for tupn.

\$\endgroup\$
  • 1
    \$\begingroup\$ Related \$\endgroup\$ – DJMcMayhem Nov 14 '17 at 16:09
  • 2
    \$\begingroup\$ @totallyhuman as with most challenges, yes. \$\endgroup\$ – caird coinheringaahing Nov 14 '17 at 16:13
  • 1
    \$\begingroup\$ Also related. \$\endgroup\$ – AdmBorkBork Nov 14 '17 at 16:22
  • 10
    \$\begingroup\$ @StanStrum It is not to point out duplicates, it is to show related challenges that people might be interested in and to show them on the sidebar to the right. \$\endgroup\$ – totallyhuman Nov 14 '17 at 17:25
  • 1
    \$\begingroup\$ Can the input be empty? (Actually, can the code be empty?) \$\endgroup\$ – Lynn Nov 14 '17 at 22:10

11 Answers 11

10
\$\begingroup\$

Python 2, 41 bytes

s="print input()in's=%r;exec s'%s";exec s

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ As a reference, the Python 3 port is 44 bytes. \$\endgroup\$ – Mr. Xcoder Nov 14 '17 at 16:11
6
\$\begingroup\$

JavaScript, 25 bytes

f=s=>('f='+f).includes(s)

Try it online!

I'm personally not a fan of this, but it's allowed.

Alternate (invalid?) solution, 19 bytes

This takes input as a regex.

f=s=>s.test('f='+f)

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Doesn't it read itself? \$\endgroup\$ – Adám Nov 14 '17 at 16:16
  • 1
    \$\begingroup\$ @Adám codegolf.meta.stackexchange.com/a/13638/68615 \$\endgroup\$ – totallyhuman Nov 14 '17 at 16:17
  • \$\begingroup\$ What is the purpose of explicitly mentioning (Node.js)? Doesn't it work in browsers too? \$\endgroup\$ – user72349 Nov 14 '17 at 17:38
  • \$\begingroup\$ @ThePirateBay Works as expected in Chrome. \$\endgroup\$ – steenbergh Nov 14 '17 at 17:45
  • 1
    \$\begingroup\$ You guys are thinking way ahead, it's just from the TIO template. :P \$\endgroup\$ – totallyhuman Nov 14 '17 at 18:45
5
\$\begingroup\$

Java 8, 124 112 bytes (function)

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

Try it here.


Here is it as full program instead (to see one of the reasons why functions are allowed on PPCG, because some languages -like Java- require very verbose mandatory boilerplate code for full programs).

Java 8, 226 214 bytes (full program)

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).contains(a[0]));}}";System.out.print(s.format(s,34,s).contains(a[0]));}}

Try it here.


Explanation:

  • 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.

And then .contains(...) is used to check if this source code contains the given input.

\$\endgroup\$
  • \$\begingroup\$ This gives me true for all strings when I "Try it online". \$\endgroup\$ – MichaelK Nov 15 '17 at 14:45
  • 1
    \$\begingroup\$ @MichaelKarnerfors not for me... Are you sure you don't add arguments each time? Only one argument is used. You have to run the program changing the argument each time you make a new test. \$\endgroup\$ – Olivier Grégoire Nov 15 '17 at 15:04
  • \$\begingroup\$ @OlivierGrégoire You are correct, I used the TIO page wrong. Thank you. :) \$\endgroup\$ – MichaelK Nov 15 '17 at 15:07
3
\$\begingroup\$

Bash, 43, 28 bytes

[[ $BASH_COMMAND = *"$1"* ]]

try it online

\$\endgroup\$
  • \$\begingroup\$ I don't know Bash, but can this be golfed further by removing a lot of the whitespace? \$\endgroup\$ – caird coinheringaahing Nov 14 '17 at 16:30
  • \$\begingroup\$ @cairdcoinheringaahing I don't think so, typeset formats it like this AFAICT. Try it online! \$\endgroup\$ – Erik the Outgolfer Nov 14 '17 at 16:53
  • \$\begingroup\$ but may be improved maybe using another technique \$\endgroup\$ – Nahuel Fouilleul Nov 14 '17 at 18:46
  • \$\begingroup\$ just found another solution \$\endgroup\$ – Nahuel Fouilleul Nov 14 '17 at 19:04
  • \$\begingroup\$ What does $1 do? \$\endgroup\$ – caird coinheringaahing Nov 14 '17 at 19:54
2
\$\begingroup\$

Haskell, 92 bytes

import Data.List;f s=isInfixOf s$(++)<*>show$"import Data.List;f s=isInfixOf s$(++)<*>show$"

Try it online! Obvious extension of the standard quine. Getting rid of the import would be nice, but I doubt isInfixOf can be computed in a shorter amount of bytes.

\$\endgroup\$
2
\$\begingroup\$

Wolfram Language (Mathematica), 33 bytes

!StringFreeQ[ToString[#0], #1] & 

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ nice! +1. I see comments for "ungolfed syntax" coming ;-) \$\endgroup\$ – J42161217 Nov 14 '17 at 18:28
2
\$\begingroup\$

QBIC, 28 bytes

?instr(B+B,;)#?instr(B+B,;)#

This prints 0 if the input is not a substring of the source, and X otherwise where X is the (first) index of the substring.

Explanation

Latter part:
#?instr(B+B,;)#   Define a string literal B$ with a copy of the source

First part:
?                 PRINT
 instr(   , )     the index of
           ;          the cmd line parameter A$
       B+B            in B$ concatenated with itself

# defines a string literal in QBIC, and assigns it to the first available string variable. That is B$ in this program, because A$ is already taken by ; (read a string from cmd line). Then, everything up to the delimiter is fed into the literal; the delimiter is a backtick - which also makes it the only ASCII char not includable in string lits. In this case, QBIC doesn't need a backtick, because the literal is terminated at the end of the code by QBIC's auto-close feature. For more information on QBIC's literals, see the Showcase thread.

\$\endgroup\$
  • \$\begingroup\$ What is A in this context? \$\endgroup\$ – caird coinheringaahing Nov 14 '17 at 16:11
  • \$\begingroup\$ @cairdcoinheringaahing a little error on my side, should've beenB and an explanation is added. \$\endgroup\$ – steenbergh Nov 14 '17 at 16:49
2
\$\begingroup\$

Jelly, 10 bytes

“;⁾vṾƓẇ”vṾ

Try it online!

How it works

“;⁾vṾƓẇ”vṾ  Main link. No arguments.

“;⁾vṾƓẇ”    Set the left argument and the return value to ';⁾vṾƓẇ'.
         Ṿ  Uneval; yield '“;⁾vṾƓẇ”'.
        v   Dyadic eval; eval ';⁾vṾƓẇ' with argument '“;⁾vṾƓẇ”'.

  ⁾vṾ       Yield 'vṾ'.
 ;          Append it to '“;⁾vṾƓẇ”', yielding the source code.
     Ɠ      Read a string from STDIN.
      ẇ     Check if it's a substring of the source code.
\$\endgroup\$
1
\$\begingroup\$

Julia, 72 bytes

I now understand what people mean when they say that quine problems are just variations on the classic quine.

x="~y=contains\"x=\$(repr(x));\$x\",y)";~y=contains("x=$(repr(x));$x",y)

Explanation

#Defines x to be the next line of the source, with the help of escaping characters
x="~y=contains\"x=\$(repr(x));\$x\",y)"; 
#Interpolates together a comparison string, including repr(x), the re-escaped from of x, and x itself, for comparison. 
~y=contains("x=$(repr(x));$x",y)
\$\endgroup\$
0
\$\begingroup\$

Perl 5, 60 + 2 (-pl) bytes

$_=(sprintf$a=q($_=(sprintf$a=q(%s),$a)=~/\Q$_/),$a)=~/\Q$_/

try it online

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

05AB1E, 17 bytes

0"D34çýIå"D34çýIå

Modification of the default 0"D34çý"D34çý by adding .

Try it online.

Explanation:

0                    # Push 0 to the stack
                     #  STACK: [0]
 "D34çýIå"           # Push the string 'D34çýIå' to the stack
                     #  STACK: [0, 'D34çýIå']
          D          # Duplicate this string
                     #  STACK: [0, 'D34çýIå', 'D34çýIå']
           34ç       # Push '"' to the stack
                     #  STACK: [0, 'D34çýIå', 'D34çýIå', '"']
              ý      # Join the stack by this '"' delimiter
                     #  STACK: ['0"D34çýIå"D34çýIå']
               I     # Take the input
                     #  STACK: ['0"D34çýIå"D34çýIå', 'Iå"D']
                å    # Check if it's a substring of the source code
                     #  STACK [1]
                     # (Output the top of the stack implicitly)
\$\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.