04 Aug Aurora Saturday 18:00 Python Homework 20.08.01.
Question:
The general problem of balancing and nesting different kinds of opening and closing symbols occurs frequently. Strings of symbols such as
{ { ( [ ] [ ] ) } ( ) } [ [ { { ( ( ) ) } } ] ] [ ] [ ] [ ] ( ) { }
are properly balanced in that not only does each opening symbol have a corresponding closing symbol, but the types of symbols match as well.
Compare those with the following strings that are not balanced:
( [ ) ] ( ( ( ) ] ) ) [ { ( ) ]
n
Please modify the brackets checker we solved in the last class, so it can handle these new types of symbols:
-
- Each opening symbol is simply pushed on the stack to wait for the matching closing symbol to appear later in the sequence.
- When a closing symbol does appear, the only difference is that we must check to be sure that it correctly matches the type of the opening symbol on top of the stack.
- If the two symbols do not match, the string is not balanced. I
- f the entire string is processed and nothing is left on the stack, the string is correctly balanced.
Sorry, the comment form is closed at this time.