Posted on:

18 May 2024

0

Did I use the stack properly, as my method seemed way too different then him?

class Stack():
    
    def __init__(self):
        self.list_stack = []
        
    def is_empty(self):
        if not self.list_stack:
            return True
        else:
            return False
        
    def push(self,item):
        self.list_stack.append(item)
        
    def pop(self):
        return self.list_stack.pop()
    
    def peek(self):
        
        if self.list_stack == []:
            return None
        else:
            return self.list_stack[-1]
        
    def __repr__(self):
        return repr(self.list_stack)
        



a = Stack()
b = Stack()
c = Stack()
st = "[({()])]"

def check(ch):
    if ch == '(':
        return ')'
    elif ch == ')':
        return '('
    elif ch == '[':
        return ']'
    elif ch == ']':
        return '['
    elif ch == '{':
        return '}'
    elif ch == '}':
        return '{'
for i in st:
    a.push(i)
    b.push(i)

for i in a.list_stack:
    c.push(b.peek())
    b.pop()
    

for i in a.list_stack:
    
    # push pop peek
    
    if a.peek() != check(c.peek()):
        print("unequal")
    
    a.pop()
    c.pop()
    

    

 

0 answers ( 0 marked as helpful)

Submit an answer