Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def max_nesting_depth(exp):
- depth = 0
- max_depth = 0
- for symbol in exp:
- if symbol == '(':
- depth += 1
- max_depth = max(max_depth, depth)
- elif symbol == ')':
- depth -= 1
- if depth < 0: #Unbalanced expression
- return -1
- if depth != 0: #unbalanced expression
- return -1
- return max_depth
- print max_nesting_depth("(()())")
- print max_nesting_depth("(()")
- print max_nesting_depth("(()))")
- print max_nesting_depth("(((())))")
- print max_nesting_depth("()((((()))))()")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement