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.
-
1\$\begingroup\$ Related. \$\endgroup\$Neil– Neil2025年06月28日 14:30:51 +00:00Commented Jun 28 at 14:30
-
1\$\begingroup\$ Related: Input ∩ Source Code, Counting characters, \$\endgroup\$xnor– xnor2025年06月28日 18:05:28 +00:00Commented 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\$Luis Mendo– Luis Mendo2025年06月28日 20:18:35 +00:00Commented 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\$Steve Bennett– Steve Bennett2025年06月29日 12:42:24 +00:00Commented 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\$Kevin Cruijssen– Kevin Cruijssen2025年06月30日 09:21:54 +00:00Commented Jun 30 at 9:21
42 Answers 42
Zsh, 20 bytes
grep "[-grep \"Z-^]"
Input via stdin, outputs via exit code: 0 for yes, 1 for no.
Zsh, 0 bytes
I'm guessing you didn't intend to permit empty solutions (standard quine rules would forbid it), but this follows your rules. Input is ignored, output is via exit code (0 for no, 1 for yes).
Python, 17 bytes
'\.count\''.count
An unnamed function that accepts a character and returns 1 (truthy) if present in its source code or 0 (falsey) if not.
Try it online! (Python 3) Or Try in Python 2 Or Try in Python 1.
-
2\$\begingroup\$ I was about to submit the same (+1). \$\endgroup\$Albert.Lang– Albert.Lang2025年06月28日 15:07:37 +00:00Commented Jun 28 at 15:07
-
\$\begingroup\$ I've posted dupes in the past before checking :D \$\endgroup\$Jonathan Allan– Jonathan Allan2025年06月28日 15:08:57 +00:00Commented Jun 28 at 15:08
-
\$\begingroup\$ That's not a function definition, but a function expression. I didnt see that coming, nor that I'm against this. \$\endgroup\$YurichBRO– YurichBRO2025年06月28日 15:36:22 +00:00Commented Jun 28 at 15:36
-
2\$\begingroup\$ @ЮрычBRO In Python, strictly speaking, it's a
builtin_function_or_method(one of the "callables", and hence maps input to output). \$\endgroup\$Jonathan Allan– Jonathan Allan2025年06月28日 16:21:18 +00:00Commented Jun 28 at 16:21 -
3\$\begingroup\$ @ЮрычBRO Function expressions are always permitted under our standard rules so FWIW your own answer can be shortened by 3 bytes (
lambda d:d in"\\ \":abdilmn") \$\endgroup\$pxeger– pxeger2025年06月28日 18:58:46 +00:00Commented Jun 28 at 18:58
Retina 0.8.2, 1 byte
1
Try it online! Link includes... test cases? But there's not much to test really. The program counts the number of 1s in the input, so if the input is 1, the program outputs 1 accordingly, otherwise the input character is not in the program's code, and therefore the output is 0.
(Shamelessly stolen from my answer to Source Code Byte Frequency. As you can see, almost any single character would work.)
Google Sheets, (削除) 24 (削除ここまで) 22 bytes
=bul(A1,"=bul(A1,"")")
Put the character in cell A1 and the formula in B1. Gets the 1-indexed first occurrence of the character in the code, or an error value if it's not present.
Google Sheets supports international function names, and bul() is find() in Turkish. It is a valid function name in any locale, and will get automatically translated to the spreadsheet locale if it's not Turkish, and will continue to work after translation. To avoid translation, set File > Settings > Locale > Türkiye.
In an English locale, you can get the equivalent like this (24 bytes):
=find(A1,"=find(A1,"")")
A variation of =find(A1,formulatext(B1)) would work too, and there are shorter names for those functions in other languages, but that would violate self-referential rules.
Vyxal 3, 8 bytes
"q$c"q$c
"q$c"q$c
"q$c" # literal string
q # quoted
$c # contains implicit input?
💎
Created with the help of Luminespire.
<script type="vyxal3">
"q$c"q$c
</script>
<script>
args=[["\""],["$"],["c"],["a"],["%"],["P"]]
</script>
<script src="https://themoonisacheese.github.io/snippeterpreter/snippet.js" type="module"/>
less fun:
Vyxal 3, 4 bytes
ᏜᏜ∩∩
outputs empty string for falsey and the character if truthy
ᏜᏜ∩∩
ᏜᏜ∩ # two character string "Ꮬ∩"
∩ # intersection of this string and implicit input
💎
Created with the help of Luminespire.
Jelly, 4 bytes
e)e)
A monadic Link that accepts a single character and yields 1 (truthy) if it is one of the two used in the source code and 0 (falsey) otherwise.
How?
e)e) - Link: character, C
)e) - two-character list -> "e)"
e - {C} exists in {that}?
If a full program were allowed, then 0 bytes - ignoring stdin TIO.
-
1\$\begingroup\$ @ITThugNinja I'm not sure what you mean,
CṖḤịdoes not fulfil the requirements. \$\endgroup\$Jonathan Allan– Jonathan Allan2025年07月26日 20:13:32 +00:00Commented Jul 26 at 20:13
Scratch, 44 bytes
Note: neither of these check for newlines because I have no idea how you would feed them one.
define(f
say<[yti\[doscn\]\\<afe(]contains(f
Full program version, 80 bytes
when gf clicked
ask()and wait
say<[an\[\]hfi\\\(wd\<rstloek\)ycg]contains(answer
Uiua 0.17.0-dev.1, (削除) (削除ここまで) 6 bytes SBCS
∊$ $∊
A palindrome! Simply checks whether the character is a member of the string $∊.
C (gcc), (削除) 45 (削除ここまで) 37 bytes
Trying it a "normal" language:
s(s){return strstr("s(){retun ,&;}\\\"",&s);}
Edit:
Kevin Cruijssen found:
s(s){s=strstr("s({=tr\"\,円&);}",&s);}
-
2\$\begingroup\$ You forget to check the
\. Also, you can save some bytes by usings=instead ofreturn: try it online - 37 bytes \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2025年06月30日 10:41:42 +00:00Commented Jun 30 at 10:41
Bash, 27 bytes
[[ "'"'" []=*1ドル' == *1ドル* ]]
Takes one character as an argument, e.g., thisscript.sh '{'. Return value is the output: 0 for true, 1 for false.
Vyxal, 4 bytes
‛‛↔↔
Outputs an empty string for truthy, a non-empty string for falsey.
Or, if you want total nonsense:
Vyxal O, 0 bytes
Outputs nothing if the character isn't in the program.
-
\$\begingroup\$ Your top program can be
`Iǎc`Iǎc, can it not? Based on this answer of yourself. ;) \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2025年06月30日 09:24:34 +00:00Commented Jun 30 at 9:24 -
\$\begingroup\$ @KevinCruijssen I didn't see that the function restriction was lifted :p \$\endgroup\$2025年06月30日 10:15:15 +00:00Commented Jun 30 at 10:15
X86 Assembly (nasm), 15 bytes
bf 00 00 00 00 66 b9 0f 00 f2 ae 0f 94 c0 c3
Function that scans its own bytecode for the byte in AL, returns 1/0 in AL.
decompiled:
find_matches:
mov edi, find_matches ; EDI = address of self
mov cx, 15 ; ECX = string length
repne scasb ; scan for AL
setz al ; return 1 or 0
ret
-
\$\begingroup\$
push 15 / pop ecxto save one byte.xchg ecx, eaxinstead ofsetzto save two bytes (and change 15 to 16 to compensate for last byte matched). Returns 0 or non-zero. \$\endgroup\$peter ferrie– peter ferrie2025年07月11日 18:34:55 +00:00Commented Jul 11 at 18:34
Lenguage, 2692 bytes
This program consists of every byte from 0 - 255 repeated for 2692 bytes. As this is too long for an answer, a snippit of the code can be seen below:
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
00000000 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F ................
00000010 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F ................
00000020 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F !"#$%&'()*+,-./
00000030 30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 0123456789:;<=>?
00000040 40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F @ABCDEFGHIJKLMNO
00000050 50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F PQRSTUVWXYZ[\]^_
00000060 60 61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F `abcdefghijklmno
00000070 70 71 72 73 74 75 76 77 78 79 7A 7B 7C 7D 7E 7F pqrstuvwxyz{|}~
00000080 80 81 82 83 84 85 86 87 88 89 8A 8B 8C 8D 8E 8F €‚ƒ„...†‡ˆ‰Š‹ŒŽ
00000090 90 91 92 93 94 95 96 97 98 99 9A 9B 9C 9D 9E 9F ‘’""•–— ̃TMš›œžŸ
000000A0 A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 AA AB AC AD AE AF ¡¢£¤\¦§ ̈©a«¬® ̄
000000B0 B0 B1 B2 B3 B4 B5 B6 B7 B8 B9 BA BB BC BD BE BF °±23 ́μ¶· ̧1o»1⁄41⁄23⁄4¿
000000C0 C0 C1 C2 C3 C4 C5 C6 C7 C8 C9 CA CB CC CD CE CF ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏ
000000D0 D0 D1 D2 D3 D4 D5 D6 D7 D8 D9 DA DB DC DD DE DF ×ばつØÙÚÛÜÝÞß
000000E0 E0 E1 E2 E3 E4 E5 E6 E7 E8 E9 EA EB EC ED EE EF àáâãäåæçèéêëìíîï
000000F0 F0 F1 F2 F3 F4 F5 F6 F7 F8 F9 FA FB FC FD FE FF ðñòóôõö÷øùúûüýþ.
This program is equivilent to the BrainFuck code ,>+., which reads a byte, ignores it, and outputs 0x01 as every possible input byte is contained within this source code.
C++ (gcc), (削除) 65 (削除ここまで) 63 bytes
[](int*i){*i=0;for(int t:" \"*()01:;=[]forint{|}")*i|=t==i[1];}
><> (Fish), 56 bytes
i:::::::"i"=}":"=}'"'=}"'"=}"}"=}"*"=}"n"=}";"=+++++++n;
For each character, save in the stack if they are equal. Then add all the qualities up and print them.
Contains the following characters:
i, :, ', ", =, }, +, n, ;
JavaScript (Node.js), 22 bytes
f=x=>(f+f).includes(x)
-2B Steve Bennett
f= both appear in function, so no need extra
JavaScript (Node.js), 24 bytes, assuming ascii input
A=B=>B<":"?"#;@ !">B:B<A
Worse than former solution but fun to keep
-
\$\begingroup\$ What do you mean "if @ is valid token"? \$\endgroup\$Steve Bennett– Steve Bennett2025年06月29日 12:34:57 +00:00Commented Jun 29 at 12:34
-
\$\begingroup\$ Also, doesn't
f=x=>(f+f).indexOf(x)+1work just as well, and save a byte? I don't think it's required that the program output strictly true/false; truthy/falsy is fine. \$\endgroup\$Steve Bennett– Steve Bennett2025年06月29日 12:47:18 +00:00Commented Jun 29 at 12:47 -
\$\begingroup\$ For that matter,
f=x=>(f+f).includes(x)is 22 bytes and returns true/false. \$\endgroup\$Steve Bennett– Steve Bennett2025年06月29日 12:49:34 +00:00Commented Jun 29 at 12:49
PowerShell (Windows and Core), 26 bytes
'''|% *ns$arg'|% *ns $args
Another 'normal' language, I guess.
A string with the characters used is passed to % (alias for ForEach-Object), which invokes the string's member "Contains()" (the only member matching *ns) for the argument.
-
\$\begingroup\$ Wasn't you mean 2sable by 05AB1E legacy? \$\endgroup\$QOO-OOKALAN– QOO-OOKALAN2025年07月01日 19:05:43 +00:00Commented Jul 1 at 19:05
-
\$\begingroup\$ @QOO-OOKALAN 2sable and 05AB1E (legacy) are two different ones. It once started as 05AB1E, then split into 2sable where implicit inputs were introduced. Then those were also added to 05AB1E, and 2sable basically became obsolete. And a few years later, the 05AB1E codebase, which was build in Python, had been changed to an Elixir backend, and loads of builtins had been changed/removed/added. The 05AB1E (legacy) is the old Python-backend version; and simply 05AB1E is the new Elixir-backend version. All three are separately available on tio.run/# (although 05AB1E is outdated on TIO). \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2025年07月04日 10:17:46 +00:00Commented Jul 4 at 10:17
-
\$\begingroup\$ Why not
-n,p !!/[p\[\\ !\/\]]/for 19? Also, program flags no longer count towards bytecount (and even if they still did, the flag had an edit distance of 1 because there's only one byte difference betweenruby -e 'code'andruby -pe 'code') \$\endgroup\$Value Ink– Value Ink2025年06月30日 23:14:07 +00:00Commented Jun 30 at 23:14
Tcl, 43 bytes
puts [regexp $argv [read [open [info sc]]]]
Program which takes an argument via command line.
-
\$\begingroup\$ unfamiliar with tcl but isn't this just reading your own source? \$\endgroup\$Themoonisacheese– Themoonisacheese2025年07月01日 15:00:52 +00:00Commented Jul 1 at 15:00
-
2\$\begingroup\$ 42 bytes, legal \$\endgroup\$Themoonisacheese– Themoonisacheese2025年07月01日 15:03:21 +00:00Commented Jul 1 at 15:03
-
\$\begingroup\$ @Themoonisacheese Yes, it reads the source. If I understand it correctly, there is no restriction against it. \$\endgroup\$sergiol– sergiol2025年07月01日 18:18:36 +00:00Commented Jul 1 at 18:18
-
\$\begingroup\$ it's a bit unclear. the post has the
quinetag, which usually means it includes the defaults for quines. \$\endgroup\$Themoonisacheese– Themoonisacheese2025年07月01日 18:53:55 +00:00Commented Jul 1 at 18:53 -
\$\begingroup\$ @Themoonisacheese: the post does not have the
[quine]tag. Perhaps it had at the time, but now it doesn't. And at the time I didn't know what a quine was. \$\endgroup\$sergiol– sergiol2025年10月24日 12:33:50 +00:00Commented Oct 24 at 12:33
Dart, 22 bytes
Unnamed function returning true if passes a single-character string which occurs in its source, and false if the character doesn't occur in the source.
'contains.\'\\'.contains
Usage:
print('contains.\'\\'.contains('a')); // true
print('contains.\'\\'.contains('#')); // false
Annoying to have to escape the quote and then include the backslash too.
A same-length alternative is 'contains."'"'".contains, which then has to include " in the characters.
As a named reusable function, it could be:
var c='vr =contains.;\'\\'.contains;
bringing it to 34 bytes (adding two occurrences of vr =; and one extra c and a).
-
\$\begingroup\$ codegolf.stackexchange.com/a/209364/91267. I was reminded of this answer as soon as I read the question but didn't expect them to be exactly same lol \$\endgroup\$Mukundan314– Mukundan3142025年07月21日 14:21:21 +00:00Commented Jul 21 at 14:21
-
\$\begingroup\$ Not any more, @Mukundan314! \$\endgroup\$Shaggy– Shaggy2025年07月23日 20:42:28 +00:00Commented Jul 23 at 20:42
TinyAPL beta 0.13.0, 10 bytes
∊∘"∊∘⍘"⍘⍘"
is the character contained in the string ∊∘"⍘?
JavaScript (Node.js), 22 bytes
f=x=>(f+f).includes(x)
JavaScript (V8), 29 bytes
t=>/[=>/[\]\\.tes()]/.test(t)
Haskell + hgl, 10 bytes
fe"fe\"\\"
Explanation
fe checks if the second argument is an element of the first. "fe\"\\" is the string of all characters in the program.
An honorable mention to these 12 byte programs, which are longer but a bit more fun:
fe$sh"fe$sh"
e/$sh"e/$sh"
These one uses sh to enclose the string in quotes so that we don't have to use a backslash to escape the quote and then a backslash to escape the backslash.
Reflection
I've been saying for a long while that I should swap the names of e and fe, since fe is undoubtedly the more commonly used one. This would save 2 bytes on both versions of the answer.
Other than that I think this is as short as hgl is reasonably going to go on this particular task.
C#, 64 bytes
I'd like to see some "normal" languages too.
C#, top level statements enabled. The character must be given as command line argument. The output is True or False.
Console.WriteLine("Consle.WritL(\"\\ag[0]);".Contains(args[0]));
PHP, 50 bytes
<?php fn($s)=>strpos('<?ph fn($s)=>tro\',;\\',$s);
The code is an anonymous function that takes the character as a string. The result is false if not contained, or an integer (the index of the first occurrence in the deduplicated code string) if contained.
Maple, 46 bytes
c->evalb(c in[";",seq(String(op(procname)))]);
op(procname) captures all the code except the final semicolon; hence the added ";" in the list.
Explore related questions
See similar questions with these tags.