# Appends or Prepends? Depends

DJMcMayhem 04/29/2017. 6 answers, 464 views

Brain-flak turns one year old tomorrow! In honor of it's birthday, we're having a PPCG style birthday party, where several users post brain-flak related questions! Help us celebrate! :)

Brain-flak is an esoteric language I wrote where all of the commands are brackets and all of the brackets must be fully matched. To borrow my own definition:

• For the purpose of this challenge, a "bracket" is any of these characters: ()[]{}<>.

• A pair of brackets is considered "matched" if the opening and closing brackets are in the right order and have no characters inside of them, such as

()
[]{}

Or if every subelement inside of it is also matched.

[()()()()]
{<[]>}
(()())

Subelements can also be nested several layers deep.

[(){<><>[()]}<>()]
<[{((()))}]>
• A string is considered "Fully matched" if and only if:

1. Every single character is a bracket,

2. Each pair of brackets has the correct opening and closing bracket and in the right order

In celebration of brain-flak's first birthday, today's challenge is about taking an unbalanced set of brackets, and determining what types of operations are needed to make it valid brain-flak.

• For example, (( is not valid brain-flak code, but if we append )) to it, it becomes (()), which is fully balanced, and therefore valid brain-flak. That makes this input appendable.

• Similarly, >} is not valid, but we can prepend {< to it to make {<>}, which is valid. That makes this input prependable.

• Some inputs are slightly more complicated. For example, )][({ cannot be made valid purely by appending or prepending. But it can be made valid by prepending [( and appending })]. Therefore, this input is both prependable and appendable.

• Lastly, some inputs can never be made valid brain-flak code by any combination of appending or prepending. For example, (> can never be made valid. (Prepending < creates <(>, and appending ) creates (>), neither of which are valid) Therefore, this input is neither appendable or prependable.

For today's challenge, you must write a program or function that takes a string of brackets and determines if the string is

appendable
prependable
both
neither

You may pick what values you use to represent for each case. For example, outputting 1, 2, 3, 4, or 'a', 'p', 'b', 'n', or 1, 'foo', 3.1415, -17, or whatever is fine. As long as each output is distinct and consistent, that's fine. You must however, clearly specify which output corresponds to which case.

You may return this value in whichever format is most convenient (for example, returning from a function, printing to STDOUT, modifying arguments, writing to a file, etc.).

You can assume that the input will never be valid brain-flak or empty.

# Examples

The following inputs are all prependable:

))
(((()()())))}
)>}]
()[]{}<>)

These are all appendable:

(({}{})
((((
([]()())(
{<<{

These are all both:

))((
>()[(()){
>{

And these are all neither:

)(}
{(((()()()))>
[}
((((((((((>
((((((((((<>()]

As usual, this is , so standard loopholes apply, and the shortest answer in bytes wins!

This challenge is particularly difficult in brain-flak, so maximum brownie points to any and every answer written in brain-flak. :)

Jonathan Allan 04/29/2017.

# Jelly, 33 32 37 35 34 bytes

bug found, horrible fix +5 bytes, better fix - 2 bytes, using a trick of Adnan's I saw here for -1 more.

“({[<“)}]>”Z;@WœṣF¥/µÐLO‘&2µIṀ>0ȯQ

Return values:

prepends [2]
appends [0]
both [2,0]
neither 1

(Invalid input returns spurious results, although valid Brain-flack, returns [].)

Try it online! - a test suite (prints mushed representations, so 20 for [2,0], and ignores lines containing any -).

Cows quack 04/29/2017.

# Retina, 4140 41 bytes

1 byte saved thanks to @MartinEnder

1 =>appendable,

2 =>prependable,

12=>both,

0 =>neither

Testcases