Advertisement
Guest User

Untitled

a guest
Apr 21st, 2015
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.47 KB | None | 0 0
  1. def max_nesting_depth(exp):
  2. depth = 0
  3. max_depth = 0
  4. for symbol in exp:
  5. if symbol == '(':
  6. depth += 1
  7. max_depth = max(max_depth, depth)
  8. elif symbol == ')':
  9. depth -= 1
  10. if depth < 0: #Unbalanced expression
  11. return -1
  12. if depth != 0: #unbalanced expression
  13. return -1
  14. return max_depth
  15.  
  16. print max_nesting_depth("(()())")
  17. print max_nesting_depth("(()")
  18. print max_nesting_depth("(()))")
  19. print max_nesting_depth("(((())))")
  20. print max_nesting_depth("()((((()))))()")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement