Challenge 2: Balance Parenthesis
Given an array containing opening and closing brackets, check whether the brackets are balanced in the array.
We'll cover the following
Problem Statement
Implement a function that takes an array testVariable
containing opening (
and closing parenthesis )
and determines whether the brackets in the array are balanced or not. The function also takes startIndex = 0
and currentIndex = 0
as parameters.
What does “Balanced Parenthesis” Mean?
Balanced parentheses mean that each opening bracket (
has a corresponding closing bracket )
. Also, the pairs of parentheses are properly nested.
Consider the following correctly balanced parentheses:
()
(())
(())()
((()))((()))
Now have a look at some incorrectly balanced parentheses:
(
)()(
((()()()()
((())))((((()
Input
An array testVariable
containing opening and closing parentheses.
Output
True
if the parentheses in the input array are balanced, False
otherwise.
Sample Input
testVariable = ["(", ")", "(", ")"]
Sample Output
True
Try it Yourself
Try to attempt this challenge by yourself before moving on to the solution. Good luck!
def balanced(testVariable, startIndex = 0, currentIndex = 0) :# Write your code herereturn None
Let’s have a look at the solution review of this problem.