Appends or Prepends? Depends

23

2

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

James

Posted 2017-04-29T06:45:15.670

Reputation: 54 537

1maximum brownie points I think that offering maximum brownie points and cookies instead would encourage Brain-Flaking this challenge more than just brownie points, since I don't think it's trivial at all in any language, let alone Brain-Flak. :P – Erik the Outgolfer – 2017-04-29T08:44:01.683

FYI: All the both tests end with open brackets, all the neither tests end with close brackets. – Jonathan Allan – 2017-04-29T12:03:24.997

2I would argue that 'both' is the wrong term. A string like ][ is not appendable, as nothing you can append can make it valid. Similarly, it's not prependable. It's... 'insertable'! You can insert it into a string to make the whole valid Brainflak. – orlp – 2017-04-29T15:06:06.427

Are already balanced strings both or neither? – Post Rock Garf Hunter – 2017-04-30T13:22:50.207

@wheatwizard Balanced strings won't be given as input. You can assume that the input will never be valid brain-flak or empty. – James – 2017-04-30T14:15:20.630

Answers

6

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

Jonathan Allan

Posted 2017-04-29T06:45:15.670

Reputation: 67 804

5

Retina, 41 40 41 bytes

1 byte saved thanks to @MartinEnder

+`\(\)|\[]|{}|<>

[]})>]+
1
\W+
0
...+
01

Try it online!

  • Prependable is 1
  • Appendable is 0
  • Both is 10
  • None is 01

Edits

  • Gained 1 byte to fix bug noticed by @Neil

user41805

Posted 2017-04-29T06:45:15.670

Reputation: 16 320

[]})>] saves a byte. – Martin Ender – 2017-04-29T08:04:40.803

@MartinEnder Ah, it's because character sets can't be empty, thanks! – user41805 – 2017-04-29T08:14:01.863

This doesn't work for all non-appendable inputs, for example (][). I think it can be fixed at a cost of one byte by changing 101 to ...+. – Neil – 2017-04-29T09:37:46.320

@Neil Thanks for noticing the bug, I wonder if there are such cases with Both as well – user41805 – 2017-04-29T09:49:14.260

No, I think 10 is the only valid combination for Both. – Neil – 2017-04-29T10:30:47.240

3

Batch, 337 bytes

@echo off
set/ps=
:g
set "t=%s:<>=%
set "t=%t:()=%
set "t=%t:[]=%
set "t=%t:{}=%
if not "%t%"=="%s%" set "s=%t%"&goto g
set "s=%s:<=[%
set s=%s:>=]%
set s=%s:(=[%
set s=%s:)=]%
set s=%s:{=[%
set s=%s:}=]%
:l
if %s:~,2%==]] set s=%s:~1%&goto l
:r
if %s:~-2%==[[ set s=%s:~,-1%&goto l
if not _%s:~2%==_ set s=[]
echo %s%

Outputs ] for prepend, [ for append, ][ for both, [] for neither.

Neil

Posted 2017-04-29T06:45:15.670

Reputation: 95 035

3

Haskell, 115 108 bytes

EDIT:

  • -7 bytes: Use more guards.
(""#)
s#""=[s>"",1>0]
s#(c:d)|Just a<-lookup c$zip"([{<"")]}>"=(a:s)#d|(a:b)<-s=[1|a==c]>>b#d|0<1=take 1$s#d

Try it online!

Use like (""#) "))". Results are given as:

[False,True]: needs nothing
[False]: prependable
[True,True]: appendable
[True]: both
[]: neither

How it works

  • The output encoding is chosen such that a need to prepend is signaled by dropping the second element of the result for the remainder, if any, while a complete mismatch is signaled by dropping all of them.
  • s#d parses a remaining string d, given a string/stack s of expected closing brackets.
    • The s#"" line checks if all closing brackets have been found by the end of the string, otherwise appending is needed.
    • The first branch of s#(c:d) checks if the next character c is an opening bracket, and if so leaves the corresponding closing bracket on the stack for the recursion.
    • Otherwise, if the stack contains closing brackets, the second branch checks if the top one matches the next character, and if not, returns an empty list instead of recursing.
    • Lastly, in the last branch the stack is empty, and we have an unmatched closing bracket that may be fixed by prepending, before recursing.

Ørjan Johansen

Posted 2017-04-29T06:45:15.670

Reputation: 6 914

2

PHP, 137 Bytes

for($c=1;$c;)$a=preg_replace("#<>|\(\)|\[\]|\{\}#","",$a=&$argn,-1,$c);echo($a=preg_replace(["#[]})>]+#","#[[{(<]+#"],[1,2],$a))<13?$a:0;

1 =>appendable,

2 =>prependable,

12=>both,

0 =>neither

Testcases

Jörg Hülsermann

Posted 2017-04-29T06:45:15.670

Reputation: 13 026

"As long as each output is distinct and consistent, that's fine". This doesn't appear to have a consistent value for neither. – Cyoce – 2017-06-04T06:13:09.493

@Cyoce It is now Fixed – Jörg Hülsermann – 2017-06-04T09:56:29.403

2

Japt, 44 bytes

=Ue"%(%)|%[]|\{}|<>" ®c -1&2|1})f31 |UfD |Ug

Outputs 1 for prependable, 3 for appendable, 13 for both, and 31 for neither.

Test it online! or Verify all test cases at once.

How it works

 =Ue"%(%)|%[]|\{}|<>" ®   c -1&2|1})f31 |UfD |Ug
U=Ue"%(%)|%[]|\{}|<>" mZ{Zc -1&2|1})f31 |UfD |Ug

                    // "(((()()())))}"  "([({}{})"    ">()[(()){"  "((((<>()]"
Ue"%(%)|%[]|\{}|<>" // Recursively remove all instances of "()", "[]", "{}", and "<>" from U.
                    // "}"              "(["          ">[{"        "((((]"
mZ{Zc -1&2|1}       // Replace each char Z with (Z.charCodeAt() - 1) & 2 | 1.
                    // "1"              "33"          "133"        "33331"
U=                  // Save the result in U.
f31 |UfD |Ug        // Match all instances of "31" and "13" (D = 13) and bitwise-OR the results with the first char.
                    // null|null|1      null|null|3   null|13|1    31|null|3
                    // 1                3             13           31
                    // Implicit: output result of last expression

ETHproductions

Posted 2017-04-29T06:45:15.670

Reputation: 47 880