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
17
+100
\$\begingroup\$

Zsh, 20 bytes

grep "[-grep \"Z-^]"

Attempt This Online!

Input via stdin, outputs via exit code: 0 for yes, 1 for no.

Zsh, 0 bytes


Attempt This Online!

I'm guessing you didn't intend to permit empty solutions (standard rules would forbid it), but this follows your rules. Input is ignored, output is via exit code (0 for no, 1 for yes).

answered Jun 28 at 14:28
\$\endgroup\$
0
13
\$\begingroup\$

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.

answered Jun 28 at 14:48
\$\endgroup\$
6
  • 2
    \$\begingroup\$ I was about to submit the same (+1). \$\endgroup\$ Commented Jun 28 at 15:07
  • \$\begingroup\$ I've posted dupes in the past before checking :D \$\endgroup\$ Commented 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\$ Commented 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\$ Commented 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\$ Commented Jun 28 at 18:58
9
\$\begingroup\$

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

answered Jun 28 at 14:34
\$\endgroup\$
8
\$\begingroup\$

Python: 31 bytes

d=lambda d:d in"\\ \":=abdilmn"
answered Jun 28 at 14:09
\$\endgroup\$
1
  • 2
    \$\begingroup\$ It's not recursive, so you can leave the lambda anonymous and save 3 bytes. \$\endgroup\$ Commented Jun 29 at 21:20
8
\$\begingroup\$

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

answered Jun 28 at 14:27
\$\endgroup\$
7
\$\begingroup\$

Vyxal 3, 8 bytes

"q$c"q$c

Vyxal It Online!

"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

Vyxal It Online!

ᏜᏜ∩∩­⁡​‎‎⁡⁠⁡‏⁠‎⁡⁠⁢‏⁠‎⁡⁠⁣‏⁠‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁤‏‏​⁡⁠⁡‌­
ᏜᏜ∩ # ‎⁡two character string "Ꮬ∩"
 ∩ # ‎⁢intersection of this string and implicit input
💎

Created with the help of Luminespire.

answered Jun 29 at 1:41
\$\endgroup\$
6
\$\begingroup\$

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.

Try it online!

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.

answered Jun 28 at 14:22
\$\endgroup\$
1
  • 1
    \$\begingroup\$ @ITThugNinja I'm not sure what you mean, CṖḤị does not fulfil the requirements. \$\endgroup\$ Commented Jul 26 at 20:13
6
\$\begingroup\$

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

enter image description here

Full program version, 80 bytes

when gf clicked
ask()and wait
say<[an\[\]hfi\\\(wd\<rstloek\)ycg]contains(answer

enter image description here

answered Jun 29 at 15:06
\$\endgroup\$
5
\$\begingroup\$

Uiua 0.17.0-dev.1, (削除) (削除ここまで) 6 bytes SBCS

∊$ $∊

Try on Uiua Pad!

A palindrome! Simply checks whether the character is a member of the string $∊.

answered Jun 28 at 14:24
\$\endgroup\$
5
\$\begingroup\$

C (gcc), (削除) 45 (削除ここまで) 37 bytes

Trying it a "normal" language:

s(s){return strstr("s(){retun ,&;}\\\"",&s);}

Try it online!

Edit:

Kevin Cruijssen found:

s(s){s=strstr("s({=tr\"\,円&);}",&s);}

answered Jun 30 at 1:44
\$\endgroup\$
1
  • 2
    \$\begingroup\$ You forget to check the \. Also, you can save some bytes by using s= instead of return : try it online - 37 bytes \$\endgroup\$ Commented Jun 30 at 10:41
5
\$\begingroup\$

Bash, 27 bytes

[[ "'"'" []=*1ドル' == *1ドル* ]]

Try it online!

Takes one character as an argument, e.g., thisscript.sh '{'. Return value is the output: 0 for true, 1 for false.

answered Jul 1 at 18:14
\$\endgroup\$
4
\$\begingroup\$

Vyxal, 4 bytes

‛‛↔↔

Try it Online!

Outputs an empty string for truthy, a non-empty string for falsey.

Or, if you want total nonsense:

Vyxal O, 0 bytes


Try it Online!

Outputs nothing if the character isn't in the program.

answered Jun 28 at 14:12
\$\endgroup\$
2
  • \$\begingroup\$ Your top program can be `Iǎc`Iǎc, can it not? Based on this answer of yourself. ;) \$\endgroup\$ Commented Jun 30 at 9:24
  • \$\begingroup\$ @KevinCruijssen I didn't see that the function restriction was lifted :p \$\endgroup\$ Commented Jun 30 at 10:15
4
\$\begingroup\$

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

Try it online!

answered Jun 30 at 16:47
\$\endgroup\$
1
  • \$\begingroup\$ push 15 / pop ecx to save one byte. xchg ecx, eax instead of setz to save two bytes (and change 15 to 16 to compensate for last byte matched). Returns 0 or non-zero. \$\endgroup\$ Commented Jul 11 at 18:34
4
\$\begingroup\$

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.

Try it online!

answered Jul 14 at 5:14
\$\endgroup\$
4
\$\begingroup\$

C++ (gcc), (削除) 65 (削除ここまで) 63 bytes

[](int*i){*i=0;for(int t:" \"*()01:;=[]forint{|}")*i|=t==i[1];}

Try it online!

answered Jul 4 at 13:29
\$\endgroup\$
3
\$\begingroup\$

><> (Fish), 56 bytes

i:::::::"i"=}":"=}'"'=}"'"=}"}"=}"*"=}"n"=}";"=+++++++n;

Try it

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, ;

answered Jun 28 at 20:31
\$\endgroup\$
3
\$\begingroup\$

JavaScript (Node.js), 22 bytes

f=x=>(f+f).includes(x)

Try it online!

-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

Try it online!

Worse than former solution but fun to keep

answered Jun 28 at 14:40
\$\endgroup\$
3
  • \$\begingroup\$ What do you mean "if @ is valid token"? \$\endgroup\$ Commented Jun 29 at 12:34
  • \$\begingroup\$ Also, doesn't f=x=>(f+f).indexOf(x)+1 work 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\$ Commented Jun 29 at 12:47
  • \$\begingroup\$ For that matter, f=x=>(f+f).includes(x) is 22 bytes and returns true/false. \$\endgroup\$ Commented Jun 29 at 12:49
3
\$\begingroup\$

PowerShell (Windows and Core), 26 bytes

'''|% *ns$arg'|% *ns $args

Try it online!

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.

answered Jun 30 at 11:36
\$\endgroup\$
3
\$\begingroup\$

05AB1E (legacy), 11 bytes

"I'+å"'"+Iå

Try it online!

answered Jul 1 at 5:09
\$\endgroup\$
2
  • \$\begingroup\$ Wasn't you mean 2sable by 05AB1E legacy? \$\endgroup\$ Commented 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\$ Commented Jul 4 at 10:17
3
\$\begingroup\$

Ruby -n, (削除) 22 (削除ここまで) 16 bytes

p~/[p\\\/\]\[~]/

Try it online!

Thanks Value Ink as usual for -3 bytes

answered Jun 30 at 21:11
\$\endgroup\$
1
3
\$\begingroup\$

Tcl, 43 bytes

puts [regexp $argv [read [open [info sc]]]]

Try it online!

Program which takes an argument via command line.

answered Jun 29 at 16:58
\$\endgroup\$
7
  • \$\begingroup\$ unfamiliar with tcl but isn't this just reading your own source? \$\endgroup\$ Commented Jul 1 at 15:00
  • 2
    \$\begingroup\$ 42 bytes, legal \$\endgroup\$ Commented Jul 1 at 15:03
  • \$\begingroup\$ @Themoonisacheese Yes, it reads the source. If I understand it correctly, there is no restriction against it. \$\endgroup\$ Commented Jul 1 at 18:18
  • \$\begingroup\$ it's a bit unclear. the post has the quine tag, which usually means it includes the defaults for quines. \$\endgroup\$ Commented 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\$ Commented Oct 24 at 12:33
3
\$\begingroup\$

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

answered Jul 8 at 8:37
\$\endgroup\$
3
\$\begingroup\$

Perl 5, 14 bytes

$_=y;\;y$_=\\;

Try it online!

answered Jul 9 at 21:51
\$\endgroup\$
3
\$\begingroup\$

Japt, 2 bytes

Simply checks if the input contains the character ø.

øø

Try it

answered Jun 30 at 12:02
\$\endgroup\$
2
  • \$\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\$ Commented Jul 21 at 14:21
  • \$\begingroup\$ Not any more, @Mukundan314! \$\endgroup\$ Commented Jul 23 at 20:42
2
\$\begingroup\$

TinyAPL beta 0.13.0, 10 bytes

∊∘"∊∘⍘"⍘⍘"

is the character contained in the string ∊∘"⍘?

answered Jun 28 at 14:58
\$\endgroup\$
2
\$\begingroup\$

JavaScript (Node.js), 22 bytes

f=x=>(f+f).includes(x)

Try it online!

JavaScript (V8), 29 bytes

t=>/[=>/[\]\\.tes()]/.test(t)

Try it online!

answered Jun 29 at 12:33
\$\endgroup\$
2
\$\begingroup\$

Haskell + hgl, 10 bytes

fe"fe\"\\"

Attempt This Online!

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"

Attempt This Online!

e/$sh"e/$sh"

Attempt This Online!

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.

answered Jun 29 at 15:57
\$\endgroup\$
2
\$\begingroup\$

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]));
answered Jul 1 at 13:24
\$\endgroup\$
2
\$\begingroup\$

PHP, 50 bytes

<?php fn($s)=>strpos('<?ph fn($s)=>tro\',;\\',$s);

Run online here

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.

answered Jul 1 at 15:22
\$\endgroup\$
2
\$\begingroup\$

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.

answered Jun 30 at 20:38
\$\endgroup\$
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.