def test_pop(self): stack = Stack() stack.pop() stack.push('Python') stack.push('Java') result = stack.pop() assert result == 'Java' assert stack.items == ['Python']
def test_pop_all(self): stack = Stack() stack.push("A") stack.push("B") stack.push("C") stack.push("D") assert stack.items == ["A", "B", "C", "D"] stack.pop_all() assert stack.items == []
def test_push(self): stack = Stack() stack.push('Python') assert stack.items == ['Python'] # Add another item stack.push('Java') assert stack.items == ['Python', 'Java']
def test_isEmpty(self): stack = Stack() assert stack.isEmpty() == True stack.push('Python') assert stack.isEmpty() == False
def test_size(self): stack = Stack() assert stack.size() == 0 stack.push('Python') stack.push('Java') assert stack.size() == 2
def test_peek(self): stack = Stack() stack.push('Python') assert stack.peek() == 'Python' stack.pop() assert stack.items == []
class SpecialStack: def __init__(self): self.stack = Stack() self.aux_stack = Stack() def push(self, item): """Accepts an item as a parameter and appends it to the end of the list. Returns nothing. """ if self.stack.is_empty(): self.stack.push(item) self.aux_stack.push(item) else: self.stack.push(item) top = self.aux_stack.pop() self.aux_stack.push(top) if item < top: self.aux_stack.push(item) else: self.aux_stack.push(top) def pop(self): """Removes and returns the last item or top item from the list.""" top_item = self.stack.pop() self.aux_stack.pop() return top_item def peek(self): """Returns the last or top item in th list.""" return self.stack.peek() def get_min(self): """Returns the top item from the list.""" top_aux_stack = self.aux_stack.pop() self.aux_stack.push(top_aux_stack) return top_aux_stack
def __init__(self): self.stack = Stack() self.aux_stack = Stack()