s = ['(','[','{']
e = [')',']','}']
f = open('OUT4.txt','w')
for line in open('DATA4.txt','r'):
    stack = []
    for c in line:
        print c
        if c in e:
            if stack and e[s.index(stack.pop())] != c:
                f.write('not balanced\n')
                break
        elif c in s:
            stack.append(c)
        print stack
    else:
        if not stack:
            f.write('balanced\n')
        else:
            f.write('not balanced\n')
f.close()

