using python 3 stacks to ensure symbols match in correct pairs and the types of symbols match as well

徘徊边缘 提交于 2019-12-24 07:52:20

问题


def spc(sym):
    stk1=myStack()
    stkall=myStack()
    for i in sym:
        if i not in stk1:
            stk1.push(i)
        else:
            stkall.push(i)
    for j in stk1:
        for k in stkall:
            if j==k:
                stk1.pop(j)
                stkall.pop(k)
            else:
                pass
    if len(stk1) == len(stkall):
        print("all symbols match")
    else:
        print(("these symbols, %s,  have no matches")%(stkall))

Above code gives me this error
"TypeError: argument of type 'myStack' is not iterable"

But I fixed it by the answer from @SergeBallesta. After I edited code to look as you see now.

Now im getting this error:

"return self.container.pop(item) # pop from the container, this was fixed from the old version which was wrong TypeError: 'str' object cannot be interpreted as an integer"

What i want to achieve is for parenthesis and all symbols to be properly balanced in that not only does each opening symbol have a corresponding closing symbol, but the types of symbols match as well.

Code for my Stack class is below. Please assist to implement this using STACKS

class myStack:
    def __init__(self):
        self.container = []
    def isEmpty(self):
        return self.size() == 0
    def push(self, item):
        self.container.append(item
    def pop(self, item):
        return self.container.pop(item)
    def size(self):
        return len(self.container)  # length of the container

    def __iter__(self):
        return iter(self.container)

回答1:


These lines

if i not in stk1:

and

for j in stk1:

requires myStack to be iterable. In Python it means that it shall have an __iter__ method that returns an iterator on its objects. As you already have an internal container, the __iter__ method can be as simple as:

class myStack:
    ...
    def __iter__(self):
        return iter(self.container)



回答2:


To do bracket validation using a stack, we only need one stack. As we come across opening brackets, we push them onto the stack. When we come across a closing bracket, we pop the top opening bracket off the stack, and compare the two. If they are the same bracket type, we continue, otherwise the string is invalid. If we ever try to pop an empty stack, the string is invalid. If we reach the end of the string without clearing the stack, the string is invalid.

opening = '[{<('
closing = ']}>)'

d = dict(zip(opening, closing))

def validate(s):
    stack = []
    for c in s:
        if c in opening:
            stack.append(c)
        elif c in closing:
            if not stack:
                # tried to pop empty stack
                return False
            popped = stack.pop()
            if d[popped] != c:
                # bracket mismatch
                return False
    return not stack


来源:https://stackoverflow.com/questions/47423400/using-python-3-stacks-to-ensure-symbols-match-in-correct-pairs-and-the-types-of

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!