The code takes in any combination of brackets and checks if they are balanced or not. If they are balanced it should output success; if they aren't balanced it should output the index (starting at index 1) where the brackets are not balanced. Example:
Input: ())
Output: 3
\\
Input: ()
Output: Success
The code always displays "Success" regardless of it being balanced or not. Instead i get this:
Input: ())
Output: Success
import sys
def Match(self, c):
if self == '[' and c == ']':
return True
if self == '{' and c == '}':
return True
if self == '(' and c == ')':
return True
else:
return False
if __name__ == "__main__":
text = sys.stdin.read()
char_code = 0
opening_brackets_stack = []
for i, next in enumerate(text):
if next == '(' or next == '[' or next == '{':
char_code += 1
opening_brackets_stack.append(next)
stack_pop = opening_brackets_stack.pop()
if next == ')' or next == ']' or next == '}':
char_code += 1
if not Match(stack_pop, next):
print(char_code)
else:
char_code += 1
print ('Success')