1

I have written the below code in Python but I am getting errors on the array 'stack' every time it says stack apart from the initial assignment. The error is "Instance of 'list' has no 'push' member".

To clarify, this piece of code is to run through each character and check if it is an opening bracket. It will push into stack, the opening bracket and I will match via an if/else statement to see if the they are matching pairs. If it does not match, return false.

Does anyone know why this code is giving errors on the stack? Stacks are very new to me and they are not making sense. This is the first piece of code I have written in Python so there may be an error in my syntax too.

I have taken the suggestion of the code from a video on stacks and tried to translate it into python to see it working. The code they were using was just a guideline of what should happen.

str = '()[(]())'
arr = ['(','[']
stack = []

for char in str:
    if char in arr:
        stack.push(char)  
    else:
        if stack.empty(): 
            top = stack.pop()
            break
        if(top == '[' and char != ']') or (top == '(' and char != ')'):
            break
return stack.empty()
7
  • Please add the full error traceback to your question. Commented Jun 20, 2020 at 16:27
  • 2
    Note: Don't use str as variable name. You may confuse it with str(). Commented Jun 20, 2020 at 16:27
  • 3
    because there's no push() method on list. Use append() instead Commented Jun 20, 2020 at 16:29
  • There is no push and empty in list Commented Jun 20, 2020 at 16:32
  • Please edit your question to clarify what you are trying to do. The list type has neither a push nor an empty method. Why do you think this should work? What do you think is top in the final if statement? Commented Jun 20, 2020 at 16:34

1 Answer 1

1

You function is not valid. List in python neither has push() nor does it have empty() method. You should modify you code to use append() and len(stack) instead of those two respectively.

The modified code would look like this -

str1 = '()[(]())'
str2 = '()[]()'
arr = ['(','[']


def check_parentheses(str):
    stack = []
    for char in str:
        if char in arr:
            stack.append(char)  
        else:
            # If our stack is empty, there's no closing bracket to match the opening one, hence invalid
            if not stack:        # Same as len(stack) == 0
                return "Invalid"
            else:
                # if closing bracket doesn't match opening bracket, then too it's invalid
                top = stack.pop()
                if(top=='[' and char != ']' or top == '(' and char != ')' ):
                    return "Invalid"
    # If all the above checks are passed, we can be sure that parentheses are valid and balanced :)
    return "Valid"


print(check_parentheses(str1))
print(check_parentheses(str2))

Output :

Invalid
Valid

I would suggest you to refer python docs - list to get more information on lists and the methods that you can use on them.

Hope this helps !

Sign up to request clarification or add additional context in comments.

4 Comments

This is incredibly helpful for me to understand where I am going wrong in my translation. I've got a lot to learn on this. That explains why it was flagging "push".
Glad to help ! I have also included a link where you can learn about other methods used in list
Note that containers have boolean meaning – it is idiomatic to use if not stack: instead of if len(stack) == 0:. Using strings to contain results is fragile; returning True or False seems more appropriate.
@MisterMiyagi Yea, right. But, I thought explicitly mentioning len(stack)==0 would be more understandable. I have made the edit and included your suggestion

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.