Exercise
Parenthesis Matching
Objective
Develop a Python program that checks if parentheses in a given expression are properly balanced. The program should evaluate a string containing various types of parentheses (e.g., '()', '{}', '[]') and return whether they are correctly matched and nested. Use a stack to keep track of opening parentheses and ensure that each closing parenthesis corresponds to the most recent unmatched opening parenthesis
Example Python Exercise
Show Python Code
Develop a Python program that checks if parentheses in a given expression are properly balanced. The program should evaluate a string containing various types of parentheses (e.g., '()', '{}', '[]') and return whether they are correctly matched and nested. Use a stack to keep track of opening parentheses and ensure that each closing parenthesis corresponds to the most recent unmatched opening parenthesis
Output
Enter an expression with parentheses: (a + b) * [c + (d / e)]
The parentheses are balanced.
Enter an expression with parentheses: (a + b) * [c + (d / e]
The parentheses are NOT balanced.
Share this Python Exercise