TORONTO KIDS COMPUTER CLUB | Aurora Saturday 18:00 Python Homework 20.08.01.
18070
post-template-default,single,single-post,postid-18070,single-format-standard,ajax_fade,page_not_loaded,,qode-theme-ver-7.6.2,wpb-js-composer js-comp-ver-6.10.0,vc_responsive

Aurora Saturday 18:00 Python Homework 20.08.01.

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.
No Comments

Sorry, the comment form is closed at this time.