28
\$\begingroup\$

Given a character as input, determine if your source code contains that character or not.

Shortest code wins. Though, I'd like to see some "normal" languages too.

UPDATE: the question previosly contained a restriction about using a function whenever possible, now I removed it.

Xcali
17k2 gold badges17 silver badges41 bronze badges
asked Jun 28 at 14:09
\$\endgroup\$
6
  • 1
    \$\begingroup\$ Related. \$\endgroup\$ Commented Jun 28 at 14:30
  • 1
    \$\begingroup\$ Related: Input ∩ Source Code, Counting characters, \$\endgroup\$ Commented Jun 28 at 18:05
  • 3
    \$\begingroup\$ For many languages (not just Zsh as in @pxeger's answer) an empty program produces empty output, which is falsy whereas non-empty output is truthy; and falsy / truthy is an acceptable output criterion in decision problems. So the empty program will (uninterestingly) fulfill the challenge requirements \$\endgroup\$ Commented Jun 28 at 20:18
  • 4
    \$\begingroup\$ I note that this question is tagged [quine] but does not seem to invoke the rules for quine-related challenges, which prohibit reading the source. Perhaps it should be tagged [self-referential] instead. \$\endgroup\$ Commented Jun 29 at 12:42
  • \$\begingroup\$ Very related / possibly a duplicate (only difference is that this challenge asks for a character as input, and that challenge for a substring 🤷). EDIT: Based on the difference in answers, it seems to be different enough to not warrant a duplicate imo. \$\endgroup\$ Commented Jun 30 at 9:21

42 Answers 42

1
2
2
\$\begingroup\$

brainfuck, 86 bytes

>,>+++++[<------>-]<<<++++[->>------------<++++[->->+<[>-]>[->>+<]<<<]<]>>>>>.;=KLMN\^

Try it online!

answered Jul 5 at 13:18
\$\endgroup\$
2
\$\begingroup\$

APL+WIN, (削除) 12 (削除ここまで) 8 bytes.

-4 bytes thanks to Adam

Prompts for input character. 1 if it exists in src code 0 if not.

⎕∊'∊⎕'''

Try it online! Thanks to Dyalog Classic

answered Jun 28 at 14:48
\$\endgroup\$
6
  • \$\begingroup\$ I get 0 running on input ⎕. \$\endgroup\$ Commented Jun 28 at 15:58
  • \$\begingroup\$ @Tbw Thanks. Fixed. \$\endgroup\$ Commented Jun 28 at 16:06
  • \$\begingroup\$ Still doesn't account for the single quote. Probably just needs two more bytes. \$\endgroup\$ Commented Jun 28 at 17:33
  • \$\begingroup\$ @Tbw. Thanks again. Hopefully this time ;) \$\endgroup\$ Commented Jun 28 at 19:10
  • \$\begingroup\$ Why +/ like that and not just ⎕∊'⎕∊'''? or even ⍞∊'⍞∊'''. \$\endgroup\$ Commented Jul 24 at 6:07
2
\$\begingroup\$

Nim, 42 bytes

proc(p='p'):bool=p in'"'&"proc():=bl in'&"

Try it online!

answered Jul 24 at 8:05
\$\endgroup\$
2
\$\begingroup\$

C64 Basic, 100 bytes

The output indicates the position of the searched character within the source code, otherwise it is 0.

C64 screenshot

Try in emulator

answered Jul 24 at 17:08
\$\endgroup\$
2
\$\begingroup\$

Pip, 11 bytes

{}(); aNSTf

Outputs 0 for "does not contain" or a positive number for "does contain." Attempt This Online!

Explanation

{}(); aNSTf
 f Main function
 ST Stringify
 aN Count occurrences of command-line argument in that string
{}(); The stringification adds these characters, so they need to be added
 to the code as some no-ops

Questionable but interesting 10-byte solution

aQ_MRXWSTf

Attempt This Online!

Outputs 0000000000 for "does not contain" or a string of nine 0s and one 1 for "does contain." Technically, if these were treated as strings in Pip, all-zeroes would be falsey and not-all-zeroes would be truthy. However, behind the scenes, they're actually lists, which would all be considered truthy, so I don't think this output method quite passes muster. It could be fixed by adding _MS at the beginning, but that's longer than my main solution.

Explanation

aQ_MRXWSTf
 STf Stringify main function
 MR Find every regex match of
 XW Word character (letter/number/underscore)
 MR and map this function to it:
aQ_ Is it equal to the command-line argument?

In other words, filter out all the extra characters added during stringification before checking if the program argument is in the result.

answered Jul 24 at 17:12
\$\endgroup\$
2
\$\begingroup\$

GolfScript, 9 bytes

"\\\"?"\?

Output: Truthy if not negative, else falsy

Try this online!

answered Jul 28 at 6:19
\$\endgroup\$
2
\$\begingroup\$

Bespoke, 85 bytes

AAAAA@AA@AAAA@AA@AAAAAAAA@AAAAAAAAAA@AAA@AA@AAA@AA@AAAAAAAA@AAAAA@AAAAAAAA@A@AAAAAA@A

Equivalent to the following code (in "mnemonic" form):

INPUT CH
PUSH BI STACKTOP QUOTIENTOF
PUT XX:TRI BI;STACKTOP MINUS
STACKTOP F
OUTPUT N
answered Jul 28 at 7:55
\$\endgroup\$
0
\$\begingroup\$

Tcl, 34 bytes

proc C c {regexp $c [info body C]}

Try it online!

This only scans the part between the {}. Is my answer valid?

answered Jun 30 at 20:38
\$\endgroup\$
0
\$\begingroup\$

Alice, 17 bytes

' wt.0g!].$KI(?O@

Try it online!

Takes the searched character as input. Prints to stdout the character if found or nothing otherwise.

' wt.0g!].$KI(?O@­⁡​‎‎⁡⁠⁡‏⁠‎⁡⁠⁢‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁣‏⁠‎⁡⁠⁤‏⁠‎⁡⁠⁣⁢‏⁠‎⁡⁠⁣⁣‏⁠‎⁡⁠⁣⁤‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁢⁡‏⁠‎⁡⁠⁢⁢‏⁠‎⁡⁠⁢⁣‏‏​⁡⁠⁡‌⁤​‎‎⁡⁠⁢⁤‏⁠‎⁡⁠⁣⁡‏‏​⁡⁠⁡‌⁢⁡​‎‎⁡⁠⁤⁡‏⁠‎⁡⁠⁤⁢‏‏​⁡⁠⁡‌⁢⁢​‎‎⁡⁠⁤⁣‏‏​⁡⁠⁡‌⁢⁣​‎‎⁡⁠⁤⁤‏⁠‎⁡⁠⁢⁡⁡‏‏​⁡⁠⁡‌­
' # ‎⁡Pushes 32, the ascii code of the space char
 wt .$K # ‎⁢For each `i` between 32 to 0
 .0g # ‎⁣Pushes the character at position (`i`, 0) in the source code as its ascii code
 !] # ‎⁤Pops, writes it to the tape an shift the tape right
 I( # ‎⁢⁡Reads an argument from the input and search it on the tape
 ? # ‎⁢⁢Read the tape at the current position and pushes the value
 O@ # ‎⁢⁣Print to stdout if found then exit
💎

Created with the help of Luminespire.

Alice, 28 bytes without source reading

'"''"w!]d$KI(?O@"w!]d$KI(?O@

Try it online!

answered Jul 14 at 9:25
\$\endgroup\$
0
\$\begingroup\$

sed -n, 15 bytes

/[]\[\\\/q1]/q1

Try it online!

Reads from standard input, exit code 1 if contained in the source, 0 otherwise.

answered Jul 29 at 21:48
\$\endgroup\$
0
\$\begingroup\$

AWK, 11 bytes

/[[\]\/\\]/

Attempt This Online!

Returns the char if match, or nothing if no match.

answered Jul 30 at 0:31
\$\endgroup\$
-5
\$\begingroup\$

Ruby, 37 bytes

p File.read(__FILE__).include?ARGV[0]

Try it online!

answered Jun 30 at 9:24
\$\endgroup\$
2
  • 4
    \$\begingroup\$ Welcome to Code Golf! unfortunately, our rules for the quine tag prohibit reading your own source code, as that's considered an improper quine. \$\endgroup\$ Commented Jun 30 at 9:29
  • \$\begingroup\$ 42 bytes legal version \$\endgroup\$ Commented Jun 30 at 9:32
1
2

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.