Home > front end >  pop() takes 1 positional argument but 2 were given for maximum stack problem
pop() takes 1 positional argument but 2 were given for maximum stack problem

Time:04-16

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()

CodePudding user response:

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).

  • Related