Is it a substring of itself?

21

2

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.

caird coinheringaahing

Posted 2017-11-14T15:55:41.097

Reputation: 13 702

1Related – James – 2017-11-14T16:09:55.863

Is a function allowed? – totallyhuman – 2017-11-14T16:12:16.070

2@totallyhuman as with most challenges, yes. – caird coinheringaahing – 2017-11-14T16:13:09.500

1Also related. – AdmBorkBork – 2017-11-14T16:22:57.733

guys mentioning all related questions that don’t make it a duplicate is unnecessary. – Stan Strum – 2017-11-14T17:24:49.423

10@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. – totallyhuman – 2017-11-14T17:25:42.170

1Can the input be empty? (Actually, can the code be empty?) – Lynn – 2017-11-14T22:10:31.283

@Lynn No, the code cannot be empty – caird coinheringaahing – 2017-11-14T22:39:39.117

-1 because this is a generalized narcissist

– Esolanging Fruit – 2017-12-19T04:16:51.537

Answers

10

Python 2, 41 bytes

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

Try it online!

totallyhuman

Posted 2017-11-14T15:55:41.097

Reputation: 15 378

1

As a reference, the Python 3 port is 44 bytes.

– Mr. Xcoder – 2017-11-14T16:11:05.697

6

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!

totallyhuman

Posted 2017-11-14T15:55:41.097

Reputation: 15 378

Doesn't it read itself? – Adám – 2017-11-14T16:16:37.590

1

@Adám https://codegolf.meta.stackexchange.com/a/13638/68615

– totallyhuman – 2017-11-14T16:17:37.530

What is the purpose of explicitly mentioning (Node.js)? Doesn't it work in browsers too? – None – 2017-11-14T17:38:24.063

@ThePirateBay Works as expected in Chrome. – steenbergh – 2017-11-14T17:45:54.020

@steenbergh. I know, of course, but I'm wondering what is the purpose of mentioning Node.js while it works on any JavaScript engine. – None – 2017-11-14T17:50:38.750

@ThePirateBay There are some differences between the Javascript implementations of Node, Spidermonkey and ... the other one. I guess it's mentioned to avoid hitting one of those differences, though in this case I think it's a bit superfluous. – steenbergh – 2017-11-14T17:53:44.097

@steenbergh. Well, I think the only reason for it is because he just copied "Code Golf submission" field from TIO without modifying anything, which includes Node.js next to JavaScript. I agree, it is superfluous in this case and may be confusing to people which see this answer. – None – 2017-11-14T17:57:57.420

1You guys are thinking way ahead, it's just from the TIO template. :P – totallyhuman – 2017-11-14T18:45:39.693

5

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.

Kevin Cruijssen

Posted 2017-11-14T15:55:41.097

Reputation: 67 575

This gives me true for all strings when I "Try it online". – MichaelK – 2017-11-15T14:45:52.697

1@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. – Olivier Grégoire – 2017-11-15T15:04:18.767

@OlivierGrégoire You are correct, I used the TIO page wrong. Thank you. :) – MichaelK – 2017-11-15T15:07:05.123

3

Bash, 43, 28 bytes

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

try it online

Nahuel Fouilleul

Posted 2017-11-14T15:55:41.097

Reputation: 5 582

I don't know Bash, but can this be golfed further by removing a lot of the whitespace? – caird coinheringaahing – 2017-11-14T16:30:29.467

@cairdcoinheringaahing I don't think so, typeset formats it like this AFAICT. Try it online!

– Erik the Outgolfer – 2017-11-14T16:53:59.990

but may be improved maybe using another technique – Nahuel Fouilleul – 2017-11-14T18:46:17.510

just found another solution – Nahuel Fouilleul – 2017-11-14T19:04:57.123

What does $1 do? – caird coinheringaahing – 2017-11-14T19:54:32.603

@cairdcoinheringaahing it 's the first argument in a script, for example script.sh '[[' or a function f '=', the result is the exit status 0 means success, 1 failed – Nahuel Fouilleul – 2017-11-14T20:02:02.727

the double quotes arround $1 prevent wildcard characters * or ? inside argument to have a special meaning and are taken literally – Nahuel Fouilleul – 2017-11-14T20:08:10.160

This reads the source file, no? – totallyhuman – 2017-11-14T21:17:59.893

not really, it's a special variable, see also https://codegolf.meta.stackexchange.com/a/13638/68615, it's part of language, it's not the same as accessing to the source

– Nahuel Fouilleul – 2017-11-14T21:37:35.510

Your first solution is more instructive to me, but a nice find with that environment variable. +1 – seshoumara – 2017-11-14T23:30:08.790

2

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.

steenbergh

Posted 2017-11-14T15:55:41.097

Reputation: 7 772

What is A in this context? – caird coinheringaahing – 2017-11-14T16:11:54.337

@cairdcoinheringaahing a little error on my side, should've beenB and an explanation is added. – steenbergh – 2017-11-14T16:49:56.837

2

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.

Laikoni

Posted 2017-11-14T15:55:41.097

Reputation: 23 676

2

Wolfram Language (Mathematica), 33 bytes

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

Try it online!

Martin Ender

Posted 2017-11-14T15:55:41.097

Reputation: 184 808

1nice! +1. I see comments for "ungolfed syntax" coming ;-) – J42161217 – 2017-11-14T18:28:48.373

2

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.

Dennis

Posted 2017-11-14T15:55:41.097

Reputation: 196 637

1

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)

eaglgenes101

Posted 2017-11-14T15:55:41.097

Reputation: 577

0

Perl 5, 60 + 2 (-pl) bytes

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

try it online

Nahuel Fouilleul

Posted 2017-11-14T15:55:41.097

Reputation: 5 582

0

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)

Kevin Cruijssen

Posted 2017-11-14T15:55:41.097

Reputation: 67 575