Skip to content

Instantly share code, notes, and snippets.

@geekykant
Created January 30, 2020 13:57
Show Gist options
  • Save geekykant/28dc3fffac68deb1ad3ffaa3fa49543f to your computer and use it in GitHub Desktop.
Save geekykant/28dc3fffac68deb1ad3ffaa3fa49543f to your computer and use it in GitHub Desktop.
Python code to balance brackets
def doBalance(expression):
stack = []
if(len(expression)%2!=0 or expression[0]==")" or expression[-1] == "("):
return False
for ex in expression:
if(ex == "("):
stack.append(1)
else:
stack.pop()
# print(stack)
if(len(stack)==0):
return True
if __name__ == '__main__':
expression = "(()))("
print(doBalance(expression))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment