'How can I read a text file and put it in the 'checkBrackets' method line by line?

I am trying to build a stack ADT with an additional method of checkBrackets() whose return is if the brackets are matched or not matched in a stack.

The code

class Stack:
    def __init__(self):
        self.top = []

    def isEmpty(self):
        if self.top == []:
            return True
        else:
            return False

    def size(self):
        print(len(self.top))

    def clear(self):
        self.top = []

    def push(self, item):
        a = len(self.top)
        self.top.insert(-(a+1), item)

    def pop(self):
        print(self.top.pop(0))

    def peek(self):
        print(self.top[0])

    def checkBrackets(self):
        myStack = Stack()
        for ch in self.top:
            if ch in ('{[('):
                myStack.push(ch)
            elif ch in ('}])'):
                if myStack.isEmpty():
                    return "not matched"
                else:
                    left = myStack.pop()
                    if ((ch == '}' and left != '{')
                        or (ch == ']' and left != '[')
                        or (ch == ')' and left != '(')
                    ):
                        return "not matched"

        if myStack.isEmpty() == True:
            return "matched"
        else:
            return "not matched"

    def print(self):
        print(self.top)

def main():
    msg = "Enter a command: pop, push, peek, size, clear, empty, p(rint), m(atch), q(uit)"
    print(msg)
    myStack = Stack()
    while True:

        command = input().split()
        if command[0] == 'empty':
            print(myStack.isEmpty())
        elif command[0] == 'size':
            myStack.size()
        elif command[0] == 'clear':
            myStack.clear()
        elif command[0] == 'pop':
            myStack.pop()
        elif command[0] == 'push':
            myStack.push(command[1])

        elif command[0] == 'peek':
            myStack.peek()

        elif command[0] == 'p':
            myStack.print()
        elif command[0] == 'm':

            inFile = open("text.txt", 'r')
            while True:
                line = inFile.readline()

                if line == "":
                    break
                for i in line:
                    myLine = Stack()

                    myLine.top.append(i)

                print(myLine.top, end = "")
                print(myLine.checkBrackets())
                # myLine.push(i)
                # print(myLine.checkBrackets())

            inFile.close()

main()

Forget about other ADT methods if they confuses you. I'm just asking about the checkBracket method...

The text file would be like this:

[()]
{[()]}
{([])}
[ ] [ ] [ ] ( ) { }
[ [ [ { { ( ( ( ) ) ) } } ] ] ] ( )
{())

How can I fix it?



Solution 1:[1]

inFile should also have the function readlines.

So you could do something like this:

inFile = open("text.txt", 'r')
lines = inFile.readlines()
for line in lines:
   line.checkBrackets()

This takes in the whole line. At the moment you are looping the elements of a single line and applying the check on each element, rather than on the full line.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Peter Mortensen