In Python all parentheses should be balanced. That means that each open parenthesis must be closed further down the source code and that each parenthesis that is closed, must have been open already earlier in the source code If the parentheses are not balanced, then this results in a compile error. For example, the illustration below shows which parentheses correspond with each other in the balanced text fragment (()(()(()))()(()())).

gebalanceerde haakjes
Example indicating which parentheses correspond with each other in the balanced expression(()(()(()))()(()())).

Assignment

Consider a given expression in which the following types of parentheses occur: parentheses (and), square brackets [and], and braces {and} and angle brackets <and>. Here we have given the first opening bracket, followed by the closing bracket for each type. In order to determine whether the parentheses in the expression are balanced, a stack can be used. Here, the symbols of the expression are gone through from left to right. Any opening parenthesis that is encountered, is placed on top of the stack. When a closing parenthesis is encountered, then the upper parenthesis is removed from the stack and compared with the closing parenthesis to see if both are of the same type. If that is not the case, then the parentheses are not balanced. Otherwise, the expression is further processed. If the entire expression has been processed, then the parentheses are only balanced when the stack is empty. Otherwise the expression is not balanced.

gestapelde haakjes
Example that shows how a stack can be used to check whether the brackets in the text fragment {[()]} are balanced.

Write a function balanced to which an expression must be passed as a string argument. The function must return a Boolean value indicating whether the parentheses in the given expression are balanced or not.

Example

>>> balanced('{[()]}')
True
>>> balanced('{[(]}')
False
>>> balanced('{[()}')
False
>>> balanced('{[()]')
False