Skip to content

Instantly share code, notes, and snippets.

@siwells
Created August 17, 2011 10:30
Show Gist options
  • Save siwells/1151286 to your computer and use it in GitHub Desktop.
Save siwells/1151286 to your computer and use it in GitHub Desktop.
Playing with a bracket matching function for use in a new code generation tool.
def Evaluate(str):
stack = []
pushChars, popChars = "<({[", ">)}]"
for c in str :
if c in pushChars :
stack.append(c)
elif c in popChars :
if not len(stack) :
return False
else :
stackTop = stack.pop()
balancingBracket = pushChars[popChars.index(c)]
if stackTop != balancingBracket :
return False
else :
print c
return not len(stack)
def main():
if Evaluate("{}{}"):
print "match"
else:
print "no match"
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment