6
\$\begingroup\$

Write a program or function that takes in a string through STDIN or function parameters, tokenizes it by parenthesis, and outputs the tokens on different lines. If the string contains a parentheses error, the program/function must output a falsey value or error message.

Examples:

Input: abc(def)ghi

Output:

abc   ghi
   def

Input: abc(def(ghi))jklm(n)op((q))rs

Output:

abc      jklm op rs
   def       n
      ghi       q

Input: blah(blah)(blah

Output:

Error

Notes:

  • The falsey value or error message must be generated by program, and the program must terminate successfully. A built in error message due to a crash does not count.
  • This is code golf. I will accept the shortest answer in bytes one month from today (July 30th).
  • No standard loopholes.
\$\endgroup\$
  • 3
    \$\begingroup\$ There's no point putting something in the sandbox for 1 hour before you post it. Give it a couple of days so that people have time to see it and give feedback. \$\endgroup\$ – Peter Taylor Jul 1 '15 at 13:28
  • 2
    \$\begingroup\$ Will do next time \$\endgroup\$ – sudo rm -rf slash Jul 1 '15 at 13:42
1
\$\begingroup\$

Cobra - 238

def f(s='')
    x,y,l=0,0,for n in (r=s.count('('))get' '.repeat(s.length-2*r)
    try
        for c in s,branch c
            on c'(',y+=1
            on c')',y-=1
            else,l[y]=l[y][:x]+'[c]'+l[y][x+=1:]
        assert y==0
    catch
        print'Error'
    success,print l.join('\n')
\$\endgroup\$

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