14
\$\begingroup\$

A very simple challenge today. Output a truthy value whether the ASCII character (or it's code) supplied is a brainfuck instruction (one of +-,.<>[]), and a falsy value if it's a comment (everything else).

Input

+
#
<
>
.
P
,

Output

true
false
true
true
true
false
true
\$\endgroup\$
8
  • \$\begingroup\$ As in, can the answer be formatted as either a program or function? (such as requiring main(){} and all of that in C answers) \$\endgroup\$ Apr 11, 2020 at 17:57
  • 3
    \$\begingroup\$ @RedwolfPrograms doesn't matterdoesn't matter \$\endgroup\$ Apr 11, 2020 at 18:02
  • 1
    \$\begingroup\$ Is the input character guaranteed to be printable ASCII? (32-126) \$\endgroup\$
    – Arnauld
    Apr 11, 2020 at 18:47
  • 1
    \$\begingroup\$ No, but I'd like to see (a part of an already valid answer) that assumes it's printable as an "addon". As I've stated (or rather deleted the comment afterwards), as it's quite a boring challenge, I'm looking for some creativity \$\endgroup\$ Apr 11, 2020 at 18:50
  • 3
    \$\begingroup\$ @LuisMendo No, according to the esolang wiki, it's often lowercase \$\endgroup\$ Apr 12, 2020 at 15:56

45 Answers 45

1
2
1
\$\begingroup\$

Factor, 17 bytes

[ "+-,.<>[]"in? ]

Try it online!

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

Ly, 14 bytes

"+-,.()[]"i~u>

Try it online!

"+-,.()[]"      - Push legit characters onto the stack
          i     - Read codepoint from STDIN
           ~    - Pop stack, search stack for that codepoint, push 1|0 result
            u   - Print top of stack as a number (will be 1 or 0)
             >  - Switch stacks to avoid printing the current contents
\$\endgroup\$
1
\$\begingroup\$

Scratch, 22 bytes

if <symbol v] contains (input) then
set [output v] to [true]
else
set [output v] to [false]

Try it online!

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

jq, 18 bytes

inside("+-,.<>[]")

Try it online!

  • Fix+4 thanks to @JoKing
\$\endgroup\$
6
  • \$\begingroup\$ Bounty started. By the way, this is 100 because SE has silly rules saying that if you've posted an answer, the minimum bounty is +100. \$\endgroup\$
    – emanresu A
    Sep 2, 2021 at 8:56
  • 3
    \$\begingroup\$ The output is always true for every input. \$\endgroup\$ Sep 3, 2021 at 0:07
  • 3
    \$\begingroup\$ You should be using inside \$\endgroup\$
    – Jo King
    Sep 4, 2021 at 7:54
  • \$\begingroup\$ I'll award the bounty once this answer becomes valid. \$\endgroup\$
    – emanresu A
    Sep 8, 2021 at 19:49
  • \$\begingroup\$ @emanresuA fixed thx to jo king, if you have already lost 100 rep for prev. Bounty you are free to not award the bounty again \$\endgroup\$
    – Wasif
    Sep 9, 2021 at 7:41
1
\$\begingroup\$

Lua, 31 bytes

print((...):match'[[%]+-.,><]')

Try it online!

Prints either the inputted character (strings are always truthy) or nil (which is falsy).

\$\endgroup\$
0
\$\begingroup\$

C (gcc) XPG4.2/Single UNIX Specification, Version 3, POSIX:2001, 30 bytes

f(c){c=!!index("+-,.<>[]",c);}

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ nitpick: index() isn't included by the C standard, so shouldn't it be stated? \$\endgroup\$ Apr 11, 2020 at 18:17
  • 1
    \$\begingroup\$ @FryAmTheEggman Ah, well deciphered! Ok, posted the version of POSIX it's good to go on. Thanks! :-) \$\endgroup\$
    – Noodle9
    Apr 11, 2020 at 18:40
  • \$\begingroup\$ This is totally not necessary. C (gcc) works just fine. \$\endgroup\$
    – S.S. Anne
    Apr 23, 2020 at 20:44
0
\$\begingroup\$

C (gcc), 31 bytes

f(c){c=!!strchr("+-,.<>[]",c);}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Ruby -nl, 16 bytes

Input is STDIN. Prints 0 (Ruby truthy) or nil (falsy).

Annoyingly, it seems like Ruby demands that both [ and ] be escaped in the character class regex.

p~/[+-,.<>\[\]]/

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ That's apparently because Ruby allows nested character classes. \$\endgroup\$
    – Neil
    Apr 12, 2020 at 9:31
0
\$\begingroup\$

JS, 26 bytes

c=>'+-,.<>[]'.indexOf(c)+1

x86_16 machine code, 40 bytes

Outputs '0' (false) or '1' (true) on the screen.

bb1f 7cb1 5080 3f00 7409 380f 7409 83c3
01eb f2b0 30eb 02b0 31b4 0ecd 10eb fe2b
2d2c 2e3c 3e5b 5d00

To boot, pad it to 510 bytes with zeros and add 55aa at the end!
This means "uncomment the last two assembly lines and rebuild".

Ungolfed (compilable, compile using nasm -fbin -o is-bf is-bf.asm):

[org 0x7c00]

mov bx, string
mov cl, 'P' ; the character
loop_:
    cmp byte [bx], 0
    je false
    cmp byte [bx], cl
    je true
    add bx, 1
    jmp loop_
false:
    mov al, '0'
    jmp print
true:
    mov al, '1'
print:
    mov ah, 0eh
    int 10h
jmp $ ; hang forever



string: db "+-,.<>[]", 0
; times 510 - ($-$$) db 0
; dw 0xaa55
\$\endgroup\$
3
  • \$\begingroup\$ you don't have to make the assembly snippet bootable, you can aswell write a callable function \$\endgroup\$ Apr 12, 2020 at 11:25
  • 1
    \$\begingroup\$ I like bootable snippets more because I can boot them in QEMU and see the results :) \$\endgroup\$
    – Muskovets
    Apr 12, 2020 at 11:26
  • 2
    \$\begingroup\$ this makes your code bigger, though. \$\endgroup\$ Apr 12, 2020 at 11:26
0
\$\begingroup\$

APL+WIN 12 bytes

3 bytes saved thanks to @Avi F. S.

Prompts for character and returns 1 if true 0 if false:

⎕∊'+-,.<>[]'

Try it online! Courtesy of Dyalog Classic

\$\endgroup\$
1
  • \$\begingroup\$ Keeping in form with your answer, you can shave off the first ×, here. Though if you just flip them around, you can do ⎕∊'+-,.<>[]'`, here! \$\endgroup\$
    – AviFS
    Apr 11, 2020 at 20:02
0
\$\begingroup\$

LaTeX, 52 bytes

\usepackage{xstring}\def\f#1{\IfSubStr{[]+-.,}#1x{}}

Defined a macro \f that takes one argument and expands to x if the input is a brainfuck instruction and to nothing if not. Assumes there is only one character in the argument.

Explanation

\usepackage{xstring}  % load the xstring package for the IfSubStr macro
\def\f#1{             % define a macro \f that takes 1 argument
  \IfSubStr{[]+-.,}#1 % test if the second argument is a substring of the first
    x                 % if it is a substring expand to x
    {}                % else expand to nothing
}
\$\endgroup\$
0
\$\begingroup\$

CJam, 14 12 bytes

-2 bytes by treating as a self-contained program instead of a block.

"+-<>[].,"q#

Try it online!

Returns -1 if not a brainfuck character, anything else if so.

\$\endgroup\$
0
\$\begingroup\$

Perl 5 -p, 15 bytes

$_=y/+,.<>[]-//

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Java (JDK), 20 bytes

"<[+.,-]>"::contains

Try it online!

\$\endgroup\$
0
\$\begingroup\$

ARBLE, 21 bytes

find(c,"[[%]+-.,><]")

Returns 1 for truthy, or nothing for falsey.

Try it online!

\$\endgroup\$
1
2

Your Answer

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

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