0

I want to make a code that can match parenthesis. Assumptions are that parenthesis is given by string, and the input is given by list.

So below is my code: I got the error that:

'AttributeError: 'list' object has no attribute 'parenthesis''

Please let me know what's wrong with my code.

def push(item):  
    stack.append(item)


def peek():  
    if len(stack) != 0:
        return stack[-1]

def pop():  #delete step
    if len(stack) != 0:
        item = stack.pop(-1)
        return item

def check(item):
    global stack1
    stack1 =[]
    global stack2
    stack2 =[]
    if item == '{':
        stack1.push('1')
    elif item == '}':
        stack1.pop()
    elif item == '(':
        stack2.push('2')
    elif item == ')':
        stack2.pop()

def parenthesis(self):
    for i in len(self):
        tail = self.peek
        tail.check
        self.pop()
    if len(stack1) == 0 & len(stack2) ==0 :
        print('pass')
    else:
        print('fail')

stack = []
push('{')
push('}')
stack.parenthesis()
Heewon
  • 1
  • 1
  • Try to implement stack in line with OOP paradigm and avoid using global variables. For example you may create a Stack class – artona Nov 19 '18 at 11:47
  • thank you artona!! and i'll try Stack class. Can i ask one question that why do you recommend to avoid using global variables? they have some disadvantages? – Heewon Nov 19 '18 at 11:50
  • Making long story short: they are dangerous because you can forget about them when developing your program. Here is the better thread on this: https://stackoverflow.com/questions/423379/using-global-variables-in-a-function – artona Nov 19 '18 at 11:53
  • artona thank you . have a nice day :) – Heewon Nov 19 '18 at 11:55
  • it looks like your code was copied from a source that originally had a stack class, i suggest you look into the original code – AntiMatterDynamite Nov 19 '18 at 11:57
  • wow! AntiMatterDynamite~ i solve the problem because your comment became inspired!! thank you!!! – Heewon Nov 19 '18 at 12:35

0 Answers0