Yehonatan

Python Brackets

Dec 10th, 2014
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.49 KB | None | 0 0
  1. lastBracket = 0
  2. left = 0
  3. right = 0
  4. index = 0
  5.  
  6. def violated_balanced(s):
  7.     global lastBracket, left, right ,index
  8.     if(index < len(s)):
  9.         if s[index] == '(':
  10.             left += 1
  11.             lastBracket = index
  12.         elif s[index] == ')':
  13.             right += 1
  14.             lastBracket = index
  15.         index += 1
  16.         return violated_balanced(s)
  17.     else:
  18.         if right != left:
  19.             return index
  20.         return -1
  21.        
  22. def main():
  23.     x = violated_balanced("t(())")
  24.     print x
  25.  
  26. # Here's our payoff idiom!
  27. if __name__ == '__main__':
  28.     main()
Advertisement
Add Comment
Please, Sign In to add comment