Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print all possible balanced parentheses for an expression?

For example, with elements a,b,c,d, there are 5 possible ways to take neighboring elements and reduce them into a single element, where exactly two elements must be combined at a time (below represented by parentheses):

(((ab)c)d), ((a(bc))d), ((ab)(cd)), (a((bc)d)) and (a(b(cd)))

The first example multiplies a*b, then multiplies that product by c, then multiplies that product by d. The second example first multiplies b*c, then multiplies that product by a, then multiplies that product by d.

Any valid parenthesized expression of 2n elements will necessarily have n ( and n ) with the property that, reading from left to right, there are always at least as many ( as ).

I know that for n numbers, the number of ways is the (n-1)th Catalan number. But how does one accurately generate all the resulting groupings?

Thanks

(As an aside: There are over 160 equivalent formulations of this problem, all based on different combinatorial objects enumerated by the Catalan Numbers. For the most up to date list of these, see Richard Stanley's Catalan Addendum.)

like image 894
Jexcy Avatar asked Jun 22 '11 22:06

Jexcy


People also ask

How do you print a balanced parenthesis?

Algorithm: Create a recursive function that accepts a string (s), count of opening brackets (o) and count of closing brackets (c) and the value of n. if the value of opening bracket and closing bracket is equal to n then print the string and return.

How many balanced parentheses are there?

What is a valid parentheses expression? A parenthesis is said to be balanced/valid if, for each left parentheses, there must be one right parentheses, and the matched pairs are properly nested.

How can we say if the given expression contains balanced parentheses?

Use a temporary variable say count to keep track of number of opening braces in the expression. Search for closing parenthesis for the corresponding opening parenthesis in the expression. If the count of closing and opening parenthesis are the same, then the expression is said to be balanced.

How do you print parentheses in Python?

The Python “SyntaxError: Missing parentheses in call to 'print'” error is raised when you try to print a value to the console without enclosing that value in parenthesis. To solve this error, add parentheses around any statements you want to print to the console. This is because, in Python 3, print is not a statement.


1 Answers

Here is actual code in Python, using generators to avoid using too much memory.

#! /usr/bin/python

def parenthesized (exprs):
    if len(exprs) == 1:
        yield exprs[0]
    else:
        first_exprs = []
        last_exprs = list(exprs)
        while 1 < len(last_exprs):
            first_exprs.append(last_exprs.pop(0))
            for x in parenthesized(first_exprs):
                if 1 < len(first_exprs):
                    x = '(%s)' % x
                for y in parenthesized(last_exprs):
                    if 1 < len(last_exprs):
                        y = '(%s)' % y
                    yield '%s%s' % (x, y)

for x in parenthesized(['a', 'b', 'c', 'd']):
    print x
like image 164
btilly Avatar answered Oct 05 '22 14:10

btilly