educative.io

Misformulated problem statement

Hi. I think this part of the problem statement for this exercise may be misformulated.
“The brackets are said to be balanced if, for every opening bracket, there is a closing bracket.”

Put this way, the condition for brackets to be considered as balanced seem to be the number of opening brackets and the number of closing brackets being equal. But, actually, that doesn’t seem to be the case. (Because, the solution code provided by Educative doesn’t exactly check that.)

I believe, the condition is rather the following: there needs to be a corresponding closing bracket for each opening bracket AND they have to be placed in correct orientation in the string, meaning, a corresponding closing bracket can’t come before the opening bracket. A closing bracket always has to be placed in the string after its opening counterpart.

This may seem like an unnecessarily complex way of formulating the statement but I believe that is not the case. Let me show you an example to clarify my point.

If the original problem statement was correct, then, the following string should have returned True.
my_brackets = “][”
Because, there is a closing bracket for each opening bracket in the string. But, the solution code would return False if given this string as input to the function.
And, this is because, the function checks not just the number of opening vs closing brackets in the string, but also, the ordering/orientation of the brackets.

If we change the string to this:
my_brackets = “[]”
The function returns True.
The function behaviour changes even though we have the same number of opening and closing numbers of brackets in the string. So, the behavior is also dependent on the orientation of the brackets and this nuance should have been a part of the original problem statement.

Hope this makes sense to you as well. Thanks.


Course: Learn Python 3 from Scratch - Free Interactive Course
Lesson: Exercise: Balanced Brackets - Learn Python 3 from Scratch

1 Like

Hi @Ubeydullah_Keles, Thank you for contacting the Educative team with your query.

We really appreciated that you have shared your thoughts with us and explained them very well. It is great seeing that you guys are always coming up with betterments. We’ll look into it, your feedback is much appreciated.

Happy Learning :blush:

1 Like