pop() 采用 1 个位置参数,但为最大堆栈问题给出了 2 个
pop() takes 1 positional argument but 2 were given for maximum stack problem
我遇到了上面提到的错误 pop() 需要 1 个位置参数,但是在我 运行 .pop() 命令时给出了 2 个用于最大堆栈问题,例如max_stack.pop(1000).
我不太确定我的语法出了什么问题。感谢有人可以指导我解决这个问题。
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()
pop
不向 self(调用实例)提供任何额外参数,因此您可以不提供任何参数。
如果您希望 pop(1000)
执行某些操作,则必须将其实现为 pop(self, your_param)
。
我遇到了上面提到的错误 pop() 需要 1 个位置参数,但是在我 运行 .pop() 命令时给出了 2 个用于最大堆栈问题,例如max_stack.pop(1000).
我不太确定我的语法出了什么问题。感谢有人可以指导我解决这个问题。
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()
pop
不向 self(调用实例)提供任何额外参数,因此您可以不提供任何参数。
如果您希望 pop(1000)
执行某些操作,则必须将其实现为 pop(self, your_param)
。