pop()采用1个位置参数,但给出了2个用于最大堆栈问题
我遇到上述错误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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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 aspop(self, your_param)
.