pop()采用1个位置参数,但给出了2个用于最大堆栈问题

发布于 2025-01-21 13:10:26 字数 1475 浏览 2 评论 0 原文

我遇到上述错误pop()采用1个位置参数,但是当我运行.pop()命令,例如max_stack.pop(1000)时,给出了2个最大堆栈问题。

我不确定我的语法出了什么问题。感谢有人可以指导我解决这个问题。

class MaxStack():
    
    def __init__(self):
        #main stack for enqueue operation
        self.main_stack =[]
        # another stack for dequeue oepration
        self.max_stack = []
        
    # adding an item to the queue is O(1) operation
    def push(self, data):
        
        #push new item to the stack
        self.main_stack.append(data)
        
        # first item is the same in both stacks
        if (len(self.main_stack) == 1):
            self.max_stack.append(data)
            return
        
        #if the item is the biggest one in the main stack, we add it to the max stack
        #stack[-1] is the peak operation: returns the last item we have inserted (without removing any elements)
        if (data > self.max_stack[-1]):
            self.max_stack.append(data)
        
        # if not larger number in the comparison then we duplicate the largest element from the max stack
        else:
            self.max_stack.append(self.max_stack[-1])
                                  
    #getting items
    def pop(self):
        self.max_stack.pop()
        self.main_stack.pop()
        return

    # max item is the last item we have inserted into the maxStack O(1)
    def get_max_item(self):
        return self.max_stack.pop()
    
if __name__ == "__main__":
    max_stack = MaxStack()

I am encountering the above mentioned error pop() takes 1 positional argument but 2 were given for maximum stack problem when I was running the .pop() command, e.g. max_stack.pop(1000).

Am not really sure what went wrong with my syntax. Appreciate if someone can guide me on solving this.

class MaxStack():
    
    def __init__(self):
        #main stack for enqueue operation
        self.main_stack =[]
        # another stack for dequeue oepration
        self.max_stack = []
        
    # adding an item to the queue is O(1) operation
    def push(self, data):
        
        #push new item to the stack
        self.main_stack.append(data)
        
        # first item is the same in both stacks
        if (len(self.main_stack) == 1):
            self.max_stack.append(data)
            return
        
        #if the item is the biggest one in the main stack, we add it to the max stack
        #stack[-1] is the peak operation: returns the last item we have inserted (without removing any elements)
        if (data > self.max_stack[-1]):
            self.max_stack.append(data)
        
        # if not larger number in the comparison then we duplicate the largest element from the max stack
        else:
            self.max_stack.append(self.max_stack[-1])
                                  
    #getting items
    def pop(self):
        self.max_stack.pop()
        self.main_stack.pop()
        return

    # max item is the last item we have inserted into the maxStack O(1)
    def get_max_item(self):
        return self.max_stack.pop()
    
if __name__ == "__main__":
    max_stack = MaxStack()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

楠木可依 2025-01-28 13:10:26

pop 没有将任何其他参数带给self(这是呼叫实例),因此您可能无法提供任何参数。

如果您想要 pop(1000)要做一些事情,就必须以 pop(self,your_param)来实现它。

pop does not take any additional arguments to self (which is the calling instance), so you may not provide any.

If you want pop(1000) to do something you'll have to implement it as pop(self, your_param).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文