76
\$\begingroup\$

Intro

The challenge is to create a program/function that prints the intersection of its own source code and a given string input. This is code golf and to be more precise:

  • Let I be the input set
    • {"a","b","c"}
  • Let S be the source code set
    • {"b","f"}
  • Then the intersection is what they share
    • I ∩ S = {"b"}

Input

Input is flexible. It should be able to handle the character encoding used for the source code.

Output

Output is flexible. It should be the set of characters that the input and source code share. Also, sets are unordered collections of distinct objects. In summary:

  • Output is flexible:
    • Could be any data structure (string or otherwise)
    • Could unordered
    • Could have a trailing \n
    • Should be distinct

Restriction

Similar to challenges, the program/function may not read its own source code and 0-byte solutions are not allowed.

Examples

  • #1
functor x(I){ return I ∩ self; }

Inputs                                Outputs
------                                -------
enter preformatted text here      ->  {"e","n","t","r","f","o","x"}

["Albrt"," Einstin"]              ->  {"l","r","t","n","s"}
  • #2
(_)->_&"(_)->&\"\\"

Inputs                                Outputs
------                                -------
"Security at the expense of       ->  "
usability comes at the expense 
of security."

(0____0)                          ->  (_)
  • #3
ಠa益длф


Inputs                                Outputs
------                                -------
Far out in the uncharted backwaters ->"a"    
of the unfashionable end of the 
Western Spiral arm of the Galaxy lies 
a small unregarded yellow sun. 
Orbiting this at a distance of roughly 
ninety-eight million miles is an 
utterly insignificant little blue-green 
planet whose ape-descended life forms 
are so amazingly primitive that they 
still think digital watches are a pretty 
neat idea.

(ノಠ益ಠ)ノ彡┻━┻                      ->"ಠ益"

Test Cases

Albert Einstein

\__( O__O)_/

!@#$%^&*()_+{}|:"<>?

1234567890-=[]\;',./

(ノಠ益ಠ)ノ彡┻━┻

“¤>%,oỊȤʠ“ØụĊ5D³ṃṠɼQ»j;Ç;“;}¶”

┬──┬ ノ( ゜-゜ノ)

Far out in the uncharted backwaters of the unfashionable end of the Western Spiral arm of the Galaxy lies a small unregarded yellow sun. Orbiting this at a distance of roughly ninety-eight million miles is an utterly insignificant little blue-green planet whose ape-descended life forms are so amazingly primitive that they still think digital watches are a pretty neat idea.

Update

  • [16-08-10]: sets are unordered collections of distinct objects
  • [16-08-10]: trailing newline is acceptable
\$\endgroup\$
15
  • 2
    \$\begingroup\$ May the output contain duplicate characters? \$\endgroup\$ Commented Aug 10, 2016 at 18:44
  • 1
    \$\begingroup\$ @DigitalTrauma From examples #1, #2 and #3 it appears not \$\endgroup\$
    – Luis Mendo
    Commented Aug 10, 2016 at 18:50
  • \$\begingroup\$ @DigitalTrauma Sorry for the ambiguity, sets (in the mathematical sense) ignore order and have no repetition. \$\endgroup\$ Commented Aug 10, 2016 at 19:05
  • 27
    \$\begingroup\$ Congratulations for coming up with a generalised quine where the best solutions are not based on the language's standard quine. :) \$\endgroup\$ Commented Aug 10, 2016 at 20:27
  • 1
    \$\begingroup\$ If a set should not have repetition, shouldn't the input sets also not contain repetition? Or is the input actually not a set? \$\endgroup\$
    – user81655
    Commented Aug 11, 2016 at 15:37

47 Answers 47

1
2
2
\$\begingroup\$

Pyth, 6 8 Bytes

{@"{@\"\

Expects input as a string.

Try it online!

Explanation:

 @     Q    Intersect implied input with:
  "{@\"\     The string containing {@"\
{          and then remove duplicates.
\$\endgroup\$
2
  • \$\begingroup\$ Unfortunately, that doesn't remove duplicates. \$\endgroup\$
    – Dennis
    Commented Aug 15, 2016 at 22:14
  • \$\begingroup\$ Fixed! Unfortunately, that costs two additional bytes, tying it with the other Pyth answer. \$\endgroup\$
    – Steven H.
    Commented Aug 15, 2016 at 22:17
2
\$\begingroup\$

q (38 bytes)

Reads from stdin

"readint0\\\" "inter read0 0

edit: was missing backslash

\$\endgroup\$
2
  • \$\begingroup\$ I'm not familiar with q, could you add a link? \$\endgroup\$ Commented Sep 12, 2016 at 16:13
  • \$\begingroup\$ sure, code.kx.com \$\endgroup\$
    – skeevey
    Commented Sep 13, 2016 at 17:41
2
\$\begingroup\$

MATLAB, 32 bytes

@(t)intersect(t,'@(t)inersc,''')

Essentially the same as Stewie Griffin's approach (the intersect built-in is hard to avoid), but this edition saves an entire 5 bytes over his by taking the input as an anonymous function, with the variable name t chosen to occur within intersect (any other character in that string would have done as well). Call as ans('yourInput').

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

Java 8, 131 129 131 bytes

a->{java.util.Set r=new java.util.HashSet();for(String s:a)if("a->{jv.utilSe r=nwHsh();fog:\"\\cd}".contains(s))r.add(s);return r;}

+2 bytes again due to a bug-fix

Explanation:

Try it here.

a->{                    // Method with String-array parameter and Set return-type
  java.util.Set r=new java.util.HashSet();
                        //  The result Set
  for(String s:a)       //  Loop over the input String-array
    if("a->{jv.utilSe r=nwHsh();fog:\"\\cd}".contains(s))
                        //   If the current character is part of the source-code:
      r.add(s);         //    Add it to the Set
                        //  End of loop (implicit / single-line body)
  return r;             //  Return the result-Set
}                       // End of method
\$\endgroup\$
0
2
\$\begingroup\$

Brachylog (newer), 18 bytes

{∈"{∈\\\"&}ˢd"&}ˢd

Try it online!

It's this plus the de-duplication predicate. Doesn't necessarily beat Fatalize's solution since his is in an older and very different version of the language.

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

Perl 6, 38 bytes

<say .comb∩$*IN.comb#<>~EVAL>~~.EVAL

Try it online!

The generalised quine still manages to beat out the trivial program by a few bytes

Perl 6, 41 bytes

say <$*. <>INabcmosy∩>.comb∩$*IN.comb

Try it online!

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

W, 12 bytes

34C+t"34C+tt

Explanation

34C+t"       % Everyone in the execution sequence excluding a quote
      34C+   % Add a (") quote
          t  % Trim out everything in this string in input
           t % Trim the string so that the intersection is kept
\$\endgroup\$
2
\$\begingroup\$

Python 3, 33 bytes

lambda a:{*"\"\lambd* :{}&"}&{*a}

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Python, 48 bytes

First thing I though of. It can probably be golfed more.

Input comes from sdtin. Output goes to stdout as a set object

print set(raw_input())&set("raw_pint se(u)\&\"")
\$\endgroup\$
4
  • \$\begingroup\$ Is this 40 bytes? \$\endgroup\$
    – Jeremy
    Commented Aug 10, 2016 at 19:34
  • 1
    \$\begingroup\$ doesn't work for input `\` \$\endgroup\$
    – KarlKastor
    Commented Aug 10, 2016 at 19:43
  • \$\begingroup\$ @Jeremy You're right, I forgot to update that. It's 47. \$\endgroup\$
    – Iguanodon
    Commented Aug 10, 2016 at 20:00
  • \$\begingroup\$ @KarlKastor Fixed \$\endgroup\$
    – Iguanodon
    Commented Aug 10, 2016 at 20:02
1
\$\begingroup\$

PHP, 61 bytes

<?=preg_filter("_<\?=[aefiglprtv$(\_[\],)\\;1\"]_",$argv[1]);

takes input from first command line argument.

no regex, 74 bytes

for(;""<$c=$argv[1][$i++];)if(strstr("aceghiosrtv()$<=[1]\;\"",$c))echo$c;

takes input from first command line argument. Run with -nr.

Note: ; needs no escaping; so PHP takes the backslash before the semicolon literally.

\$\endgroup\$
1
\$\begingroup\$

Java (OpenJDK 8), 58 bytes

s->s.filter("s->.filter(\"\\:cona)d"::contains).distinct()

Try it online!

Accepts the input as Stream<String>, whereas each character of the input is a stream element, and returns a filtered, distinct stream.

\$\endgroup\$
1
\$\begingroup\$

Lua, 91 bytes

Old challenge, but still lacking a Lua solution, so...

s=...(" s=.(\\\"):gub,fnctiod1awre"):gsub(".",function(c)c=s:find(c,1,1)and io.write(c)end)

Try it online!

\$\endgroup\$
1
\$\begingroup\$

J, 18 bytes

'''&()[-.'&([-.-.)

How it works

 ''&()[-.           the character set, '' stands for '
          &         is the left operand
           ([    )  the set> itself, ignore <the input
           (   -.)  the set> except any characters in <the input
                      (the wrong ones remain)
           ( -.  )  the set itself> except <the wrong characters

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ It's easier to see where your code ends and the explanation begins if you put the explanation in a separate programming puzzle. Also, consider adding a try it online link to your answer so that users can test your submission. Besides that, great first post and welcome to PPCG! \$\endgroup\$
    – LyricLy
    Commented Sep 23, 2017 at 10:44
1
\$\begingroup\$

Husk, 7 bytes

n"n\\\"

Try it online!

n          The intersection of
 "n\\\"    the string n, \, "
           with the input.

I tried to work around having to escape the quote with a backslash, but that just made it longer:

Husk, 8 bytes

n:'""n:'

Try it online!

n         The intersection of
  '"      the character "
 :        prepended to
  "n:'    the string n, :, '
          with the input.

Another attempt to eliminate backslashes, which would be two bytes shorter if it didn't need to remove duplicates:

Husk, 7 bytes

uns"nsu

Try it online!

u          Without duplicate elements,
 n         the intersection of
  s        the string representation of
   "nsu    the string n, s, u
           with the input.
\$\endgroup\$
1
\$\begingroup\$

Burlesque, 10 bytes

#QupIN#Qvv

Try it online!

Turns out the lazy way worse than doing it generally because I can't get a quote within a quote nicely. "IN_+"'"_+IN

#Q  #Push the code onto the stack
up  #Convert to string
IN  #Intersection
#Q  #Make sure a #Q is captured
vv  #Drop the resulting push
\$\endgroup\$
1
  • \$\begingroup\$ @a'_' Do you mean because it produces an extra "\"? If so, then adding a "Q" cures it. Try it online! \$\endgroup\$ Commented Jan 19, 2020 at 23:40
1
\$\begingroup\$

brainfuck, 150 bytes

,[[>+>+<<-]>>[-[->>+<<]+>>]+>+[--<[-<<]>.>+>[-]]-<[-<<]>[-]<,] !"#$%&'()*/0123456789:;=?@ABCDEFGHIJKLMNOPQRSTUVWXYZ\\^_`abcdefghijklmnopqrstuvwxyz{|}~

Try it online!

Support 32-126

main code(set remove dup) 62

\$\endgroup\$
1
\$\begingroup\$

Japt v2.0a0, 15 13 bytes

My very first "proper" quine. At long fecking last!

oQ+"oQ+ â"Q â

Try it

oQ+"..."Q â     :Implicit input of string
o               :Keep only those characters that appear in
 Q              :  Quotation mark
  +"..."        :  Append literal string
        Q       :  Passed as a second argument to the o method to disable the case insensitive flag (any truthy value would work)
          â     :Deduplicate
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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