Pretty Printing Parentheses

7

1

The worst part about long lines or code or math equations are the parentheses. Nobody wants to read through 2x(3y+4*abs(x^[2*e^x])-5{3(x+y)-5})!

So, the goal of this challenge is to make it at least slightly easier to read these parentheses-filled beasts! For the above math equation, the output should be:

2x (
  3y+4*abs (
    x^ [
      2*e^x
    ]
  )
  -5 {
    3 (
      x+y
    )
    -5
  }
)

A parenthesis is defined as either of these characters: ()[]{}. Indents should each be two spaces, and there should be one space of seperation between the expression and the parenthesis: 2x (, x^ [ as some examples.

I would like to debut the Atomic Code Golf metric in this question. Take out all separators in your code (()[]{}. ; to name some common ones), and then take the length of your program if every reserved word and operator were 1 character long. That is your program's score. if x**(3.2)==b[3]:print x is 11 points: if|x|**|3|.|2|==|b|3|print|x, or ix*3.2=b3px. Any quick questions about the metric can be posted here; larger questions should be discussed in meta using the link above.

Lowest score wins. Good luck, have fun!

beary605

Posted 2013-02-17T05:12:27.740

Reputation: 3 904

Related. (The differences being that the other question asks for K&R style bracket placement and that it's plain code golf.) – Martin Ender – 2015-09-21T10:22:31.517

The unary answer will win. – coredump – 2015-10-05T07:58:46.720

Are names from standard libraries considered "reserved words"? – Ben – 2015-10-05T19:49:15.183

Is $x one token or two? – mob – 2013-02-21T03:59:25.410

@mob: Depends on what $x is: if it's a variable, 1 token. If it's a math operator, then 2. – beary605 – 2013-02-21T04:14:17.573

Love the scoring system, I think it will make Common Lisp much more viable. – Strigoides – 2013-02-27T20:14:04.207

Re. the task itself: do we have to check for matching brackets, or is it okay to assume that they are all correctly matched up? Furthermore, is the input expression guaranteed to contain no whitespace already? (In particular, in the vicinity of the parens..) – FireFly – 2013-03-06T16:16:00.187

@FireFly: They're all correctly matched (there will be as many open brackets as there are closed brackets). Yes, there will be no whitespace. – beary605 – 2013-03-07T00:57:56.067

Answers

3

C, 139 137 chars, 67 tokens

i,n;                                                      // 2 tokens
main(c){                                                  // 2 tokens
        while(~(c=getchar()))                             // 5 tokens
                n=strchr("([{}])",c)?                     // 11 tokens
                        n=c%4!=1,                         // 7 tokens
                        i+=n*4-2,                         // 7 tokens
                        printf("\n%*s%c"+n,n?1:i,"",c)    // 15 tokens
                :                                         // 1 token
                        !printf("\n%*s%3$c"+!n*4,i,"",c); // 17 tokens
}

Logic:

  • Read until EOF (detect it because ~EOF==0).
  • If parenthesis, check open/close (for c in ")]}", c%4==1), update indentation (i), print with some whitespace.
  • If not, print the character, possibly prefixed by newline and indentation (n is true after parenthesis).

ugoren

Posted 2013-02-17T05:12:27.740

Reputation: 16 527

Whatever it is you're doing with n looks truly evil. :) Woah!: c%4!=1! That's brilliant! – luser droog – 2014-01-07T06:47:44.303

1

Python, 102(?) Tokens

(Please correct if I didn't count correctly)

def f(t):
#1  2 3
    l,s,p,c,e,n=0,"",1," ",")]}","\n"
#   4 5 6 7 8 9ab cd e fgh ijklm no p
    for i in t:
#   q   r s  t
        if i in"([{": l+=2;s+=c+i+n+l*c
#       u  v w xyzAB  CD E FG HIJKLMNOP
        elif i in e: l-=2;s+=n+l*c+i+p*(n+l*c);p=1
#       Q    R S  T  UV W XY Z123456789 abcde  fgh
        else:
#       i
            if len(s) and s[-1]in e: s+=n+l*c
#           j  k   l  m   n  o p  q  rs tuvwx
            s+=i;p=0
#           yz A BCD
    return s
#   E      F

pascalhein

Posted 2013-02-17T05:12:27.740

Reputation: 141

1

Perl, 86 char

57 tokens? (21+25+5+3+1+2?) Newlines are significant.

s![[{()}\]]!1-(3&ord$&)?" $&
"."  "x++$x:$/.($y="  "x--$x)."$&
$y"!ge;s/
 *
/
/g;print

mob

Posted 2013-02-17T05:12:27.740

Reputation: 2 506