1

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
chloe
  • 19
  • 4
  • You're already reading the file line by line. Why not add an argument to `checkBracket` and provide the line as an input? This question is also relevant: https://stackoverflow.com/questions/3277503/how-to-read-a-file-line-by-line-into-a-list – Mr. Fegur Apr 03 '22 at 16:01
  • What is wrong with the code? What are the symptoms? – Peter Mortensen May 16 '22 at 11:11

1 Answers1

0

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jinx
  • 173
  • 2
  • 12