Created
January 30, 2020 13:57
-
-
Save geekykant/28dc3fffac68deb1ad3ffaa3fa49543f to your computer and use it in GitHub Desktop.
Python code to balance brackets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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