BRACES = { '(': ')', '[': ']', '{': '}' }
def group_check(s):
stack = []
for b in s:
c = BRACES.get(b)
print("inside loop. stack: %s, b: %s" % (str(stack), b))
print("not stack: %s" % str(not stack))
if c:
stack.append(c)
elif not stack or stack.pop() != b:
return False
print("before return. stack: %s" % str(stack))
return not stack
# print(group_check("()"))
# print(group_check("({"))
print(group_check("({}{}[])"))
QlJBQ0VTID0geyAnKCc6ICcpJywgJ1snOiAnXScsICd7JzogJ30nIH0KCmRlZiBncm91cF9jaGVjayhzKToKICAgIHN0YWNrID0gW10KICAgIGZvciBiIGluIHM6CiAgICAgICAgYyA9IEJSQUNFUy5nZXQoYikKICAgICAgICBwcmludCgiaW5zaWRlIGxvb3AuIHN0YWNrOiAlcywgYjogJXMiICUgKHN0cihzdGFjayksIGIpKQogICAgICAgIHByaW50KCJub3Qgc3RhY2s6ICVzIiAlIHN0cihub3Qgc3RhY2spKQogICAgICAgIGlmIGM6CiAgICAgICAgICAgIHN0YWNrLmFwcGVuZChjKQogICAgICAgIGVsaWYgbm90IHN0YWNrIG9yIHN0YWNrLnBvcCgpICE9IGI6CiAgICAgICAgICAgIHJldHVybiBGYWxzZQogICAgcHJpbnQoImJlZm9yZSByZXR1cm4uIHN0YWNrOiAlcyIgJSBzdHIoc3RhY2spKQogICAgcmV0dXJuIG5vdCBzdGFjawogICAgCiAgICAKICAgIAojIHByaW50KGdyb3VwX2NoZWNrKCIoKSIpKQojIHByaW50KGdyb3VwX2NoZWNrKCIoeyIpKQpwcmludChncm91cF9jaGVjaygiKHt9e31bXSkiKSk=
inside loop. stack: [], b: (
not stack: True
inside loop. stack: [')'], b: {
not stack: False
inside loop. stack: [')', '}'], b: }
not stack: False
inside loop. stack: [')'], b: {
not stack: False
inside loop. stack: [')', '}'], b: }
not stack: False
inside loop. stack: [')'], b: [
not stack: False
inside loop. stack: [')', ']'], b: ]
not stack: False
inside loop. stack: [')'], b: )
not stack: False
before return. stack: []
True