fork download
  1. # your code goes here
  2. # your code goes here
  3. def valid_parentheses(string):
  4. #your code here
  5. stck = Stck()
  6. check = {'(':1,')':-1,'[':2,']':-2,'{':3,'}':-3}
  7. string = string.replace(" ", "")
  8. print string
  9. for i in string:
  10. print i
  11. if not i.isalnum():
  12. if check[i] > 0 :
  13. stck.push(i)
  14. else:
  15. if stck.index == -1: return False
  16. a = stck.pop()
  17. if (check[i]+check[a]) is not 0: return False
  18. if stck.index >= 0: return False
  19. return True
  20.  
  21. class Stck:
  22. def __init__(self):
  23. self.stack = []
  24. self.index = -1
  25.  
  26. def push(self,n):
  27. self.stack.append(n)
  28. self.index += 1
  29.  
  30. def pop(self):
  31. a = self.stack[self.index]
  32. self.stack.pop(self.index)
  33. self.index -= 1
  34. return a
  35.  
  36.  
  37. print valid_parentheses(" (")
Success #stdin #stdout 0.01s 8968KB
stdin
Standard input is empty
stdout
(
(
False