Skip to content

Instantly share code, notes, and snippets.

Created January 22, 2013 15:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4595437 to your computer and use it in GitHub Desktop.
Save anonymous/4595437 to your computer and use it in GitHub Desktop.
def closeBracket(openbracket):
if openbracket == '{':
return '}'
elif openbracket == '(':
return ')'
elif openbracket == '[':
return ']'
if __name__ == "__main__":
testcase = ('{{[{{{{}}{{}}}[]}[][{}][({[(({{[][()()]}}{[{{{}}}]}))][()]{[[{((()))({}(())[][])}][]()]}{()[()]}]})][]]}{{}[]}}')
teststack = []
position = 0
for c in testcase:
if c == '{' or c == '(' or c == '[':
#push the open character on the stack
teststack.append(c)
if c == '}' or c == ')' or c == ']':
#check first to see if the stack is empty and
#if it is report the position
if not teststack:
print "Failed at position %d" % position
break
#it's not empty, so pop the last appended character off
f = teststack.pop()
#if this character is the respective close character
#of the character that got popped off, we're good
if closeBracket(f) == c:
print "All good at position %d" % position
#if the popped character isn't the respective close
#character, then fail and report position
elif closeBracket(f) != c:
print "Failed at position %d" % position
position = position + 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment