Last active
April 10, 2020 12:05
-
-
Save AnBucquet/006e733866ac3c0a4579c87304a1a8fb to your computer and use it in GitHub Desktop.
Checking parentheses and 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
| # This gist aims to check the parentheses and brackets in a mathematical expression like "((2+3)*[5**2+1])*(40+2)". | |
| import sys | |
| import math | |
| t = input() | |
| s = [] | |
| for c in t: | |
| if c in ("(", "[", "{"): | |
| s.append(c) | |
| else: | |
| if c not in (")", "]", "}"): | |
| continue | |
| if len(s) == 0: | |
| print("false") | |
| exit() | |
| z = s.pop() | |
| if c == ")" and z != "(": | |
| print(False) | |
| exit() | |
| elif c == "]" and z != "[": | |
| print(False) | |
| exit() | |
| elif c == "}" and z != "{": | |
| print(False) | |
| exit() | |
| print(len(s)==0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment