Skip to content

Instantly share code, notes, and snippets.

@AnBucquet
Last active April 10, 2020 12:05
Show Gist options
  • Select an option

  • Save AnBucquet/006e733866ac3c0a4579c87304a1a8fb to your computer and use it in GitHub Desktop.

Select an option

Save AnBucquet/006e733866ac3c0a4579c87304a1a8fb to your computer and use it in GitHub Desktop.
Checking parentheses and brackets
# 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