63

Python, 41 39 34 bytes lambda e:"(".join(e)+")"*~-len(e) Ideone it Pretty self explanatory. It puts a parenthesis between every other character then adds one less than the length parentheses to the end.


59

Brain-Flak Classic, 1271 1247 1239 bytes <>(()){<>((([][][][][])<(((({}){})(({})({}))[])({}(({})({}({})({}{}(<>)))))[])>{()<{}>}{})<{{}}{}>())}{}<>(<(({()(((<>))<>)}{}{<({}(([][][])((({})({}))[]{})){})>((){[]<({}{})((){[]<({}{}<>((({})({})){}{}){})(<>)>}{}){{}{}<>(<(...


49

x86 machine code, 39 34 33 30 29 bytes 00000000 68 c3 b8 07 31 ff be 82 00 b3 a0 ad 4e 3c 28 7c |h...1.......N<(|| 00000010 f0 77 05 ab 01 df eb f3 29 df ab eb ee |.w......)....| 0000001d x86 assembly for DOS, with some tricks: org 100h section .text start: ; point the segment ES to video memory ; (c3 is chosen so that it ...


45

MS-DOS .com file, 30 bytes 0000 fc be 82 00 b4 02 ac 88 c2 cd 21 ac 3c 0d 74 0d 0010 b2 28 50 cd 21 5a e8 f0 ff b2 29 cd 21 c3 The string is passed to the executable using the command line. (One space character between the .COM file name and the string). The result is written to standard output. The disassembly is here: fc cld ...


34

GolfScript, 107 91 .4:ab-1:ba=;1 %ba%{...fi@@= c43.=;)('"([{ }])"'~?~'"([{ }])"')(;=.34c =@@if...}%ab% 1;=ab:1-ba:4. Newlines are artistic. fi, c43 and c are noops, but the entire code is executed. Prints -3-1-1 for convenient palindromes, -4-1-1 otherwise. Try it online! Alternate version, 155 bytes At the cost of 64 bytes, this can be improved upon: ...


33

Brain-Flak, 1101, 1085, 981 bytes {(<(({}))((((()()()()()){}){}){})({}[{}]<(())>){((<{}{}>))}{}>{({}<>)(<>)}{}<(({ }))((((()()()()()){}){}){}())({}[{}]<(())>){((<{}{}>))}{}>{({}<>)({}[{}](<()>)){ {}{}(<(())>)}{}{<>{{}}<>{{}}((<()>))}{}(<>)}{}<(({}))(((((()()()()()){}...


32

JavaScript (ES6), 40 34 33 bytes Saved 6 bytes, thanks to ETHproductions A recursive function. f=([c,...s])=>s+s?c+`(${f(s)})`:c Try it online!


32

Brain-Flak, 952 916 818 bytes {(({})[(((()()()()()){}){}){}])((){[()](<{}>)}{}){{}(({})()<>)(<>)}{}(<>)<>(({})[(((()()()){}){}()){({}[()])}{}])((){[()](<{}>)}{})({}<>{})<>(({})[((((()()()()()){}){})){}{}])((){[()](<{}>)}{})({}<>{})<>(({})[(((((()()()()()){}){}){}())){}{}])((){[()](<{}>)}...


30

Retina, 55 38 bytes i`^(([elp])|[^ghos]|(?<-2>.))* $.&$*_< Try it online! (The first line just allows running several test cases at once.) Explanation The problem is essentially to find the longest prefix that doesn't have an unmatched closing parenthesis. Except that we can use either e, l or p in place of ( and either g, h, o or s in place ...


28

J, 32 28 bytes This was a fun one. 0|:')(('&(i.-<:@+/\@i:){."0] Explanation This is how this solution works, including an explanation of how it has been golfed. NB. Let a be a test case a =. '((()())()(()(())()))' NB. level alterations _1 + ').(' i. a 1 1 1 _1 1 _1 _1 1 _1 1 1 _1 1 1 _1 _1 1 _1 _1 _1 NB. absolute levels +/\ _1 ...


27

Brainfuck, 42 40 bytes >+[-->+[<]>-]>+>>,.,[<+<.>>.,]<<+>[-<.>] Try it online! Ungolfed: >+[-->+[<]>-]>+ # count to 40 (ASCII for open paren) >> # move to the input holder ,. # input the first byte and output it , # input the next ...


27

Brain-Flak, 685, 155, 151, 137 bytes (())({<{}({}()<(()()()())>)({}(<>))<>{(({})){({}[()])<>}{}}{}<> ([{}()]{})(({})){{}({}[()])(<()>)}{}(<>)<>{{}<>{}({}<>)}{}(<>[]<>)>()}<>) Try it online! 136 bytes of code, plus one byte for -a. One indexed. 530 bytes golfed off! That's ...


26

C, 78 52 39 34 33 bytes Even more C magic (thanks xsot): c(n){return!n?:(4+6./~n)*c(n-1);} ?: is a GNU extension. This time by expanding the recurrence below (thanks xnor and Thomas Kwa): c(n){return n?(4+6./~n)*c(n-1):1;} -(n+1) is replaced by ~n, which is equivalent in two's complement and saves 4 bytes. Again as a function, but this time exploiting ...


23

Jelly, 4 bytes Ḥc÷‘ Try it online! How it works Ḥc÷‘ Left argument: z Ḥ Compute 2z. c Hook; apply combinations to 2z and z. ÷‘ Divide the result by z+1.


23

Brain-Flak, 204 196 190 bytes {({}<>)<>((((()()()()()){})(({}){})())({}(({})((({}){})(<()>))))())(({<({}<>[({})])>[()]{()(<{}>)}{}<>}{}()<({}<>[({})]){(<{}({}())>)}{}<>>)){(<({}{}<>[{}]{}<>)>)}{}{<>{{}}}{}}<>((){[()]<>}) Try it online! -8 bytes thanks to ...


23

05AB1E, 11 bytes S'(ý¹g<')×J Try it online! Explanation: S'(ý join input by "(" ¹g< push length of input - 1, call it n ')× push a string with char ")" n times J concatenate


21

GolfScript, 23 bytes n/{"()"1/{.2$\-,*}%*n}/ The loophole I'm exploiting is the ruling that: You may add extra unnecessary parentheses if you want to, if it makes your code shorter Basically, for each line, this code counts the number of characters on the line that are not opening parentheses, and prepends that many extra opening parentheses to the ...


20

Python 3, 70 69 65 bytes -1 byte thanks to @Uriel -4 bytes thanks to @xnor f=lambda s,b=0:s and'(['[b]+s[0]+f(s[1:-1],~b)+s[1:][-1:]+')]'[b] Try it online!


18

Brainfuck, 44 bytes >+++++[-<++++++++>],.,[<.>.>+<,]<+>>[-<<.>>] Reads a byte at a time, puts an open-paren before each one except the first, puts the same number of close-parens at the end.


17

Brainfuck 76 bytes >>+[<+++++++++[->---------<]+>-[--[[-]<->]<[<[->-]>[<+]<]>]<[-<+>]+>,]<<[<+] This goes out of it's bonds if square brackets are unbalanced turning the bf interpreter/compiler to fail runtime and some of them have exit codes to reflect that. requires eof=0, wrapping values ...


17

CJam, 18 l7~f&_f{\/~;&}s,2/ Thanks isaacg for some golfing ideas :) Try it online or try all examples Explanation: l read a line of input 7~f& clear the lowest 3 bits of each character the goal is to convert brackets of the same type to the same char _ duplicate the resulting string, let's call it S f{…} ...


17

05AB1E, 19 bytes Input is given in quotes. Code: "[](){}<>"2÷)"":g2Q Well crap, a lot of bugs and unimplemented features were found. Explanation: "[](){}<>" # Push this string 2÷ # Split into pieces of two ) # Wrap it into an array (which should not be needed) "" # Push an empty ...


17

Haskell, 30 bytes f[x]=[x] f(a:b)=a:'(':f b++")" Usage example: f "Nest a string" -> "N(e(s(t( (a( (s(t(r(i(n(g))))))))))))". Take the next char, followed by a (, followed by a recursive call with all but the first char, followed by a ).


16

Python 3, 443 158 155 154 134 131 128 124 117 116 115 bytes c=d=C=D=0 for e in input():v='[<>,.-+]'.find(e);d=d*8+v;c+=c<0<6<v;c-=d>1>v;C,D=(c,C+1,d,D)[v>6::2] print(-~D*8**C) Several bytes thanks to Sp3000 and Mitch Schwartz :D How this works: This maps all valid BF programs into all possible, valid or invalid, BF programs, that ...


16

CJam, 118 116 bytes This got slightly out of hand... especially the second half seems like it should be very golfable. ri2/_)mr:R-"<>"R*mr_'=fm0\{1$+}%+__&e`]:\{mr1aa..+}*\@](\z:~\{~\("+-"*mr1$3$e=[{_,)mr_2$<@@>}*+]@@f{`1$`={(}@?\}W<}/ Test it here. This handles N = 100 pretty much instantly. I don't have time to write the full ...


16

Jelly, 34 33 bytes Œl“ʋʋ“ṁḍ»ċ€Ð€IF+\‘0ṭi0ð’”_×;”<;ṫ@ Try it online! I think I'm finally starting to understand Jelly. Feels a bit scary.


16

Jelly, 9 8 bytes -1 byte thanks to @Dennis (use mould, ṁ, in place of length, L, and repeat, x) j”(³”)ṁṖ TryItOnline How? j”(³”)ṁṖ - Main link: s e.g. "code-golf" printed output: j - join s with ”( - literal '(' "c(o(d(e(-(g(o(l(f" ”) - literal ')' ṁ - mould like ³ - first input, s "))))))))...


15

C, 150 bytes t;f(char*c){char l=strlen(c)+1,o[l*l],*A=o,m=0;for(t=1;t<l*l;t++)o[t-1]=t%l?32:10;for(t=-1;*c;c++)A++[l*(*c-41?++t>m?m=t:t:t--)]=*c;A[m*l]=0;puts(o);} This was crazy fun to golf. I'm still not convinced I'm done with it. We define a single function, f, that takes the string as input and outputs to stdout. Let's go through the code, ...


15

TI-BASIC, 69 60 56 55 bytes This is for the TI-83+/84+ family of calculators, although it was written on an 84+ C Silver Edition. The program shows up as larger on-calc due to VAT+size info being included. Also, there are more than 56 characters here; the reason it's 56 bytes is because all commands that are more than one character are compressed down to ...


15

Retina + Bash, 27 bytes (14 + 10 + 3 = 27) This makes use of ANSI Escapes: \( (\e[B \) \e[A) Equivalent to sed -e "s/(/(\\\e[B/g;s/)/\\\e[A)/g". The \e[B escape code means move cursor down one row, and the \e[A means move cursor up one row, so this solution simply inserts those codes after and before the start and end of each nested pair of parentheses. ...


Only top voted, non community-wiki answers of a minimum length are eligible