def test_peek(): nums = Stack() nums.top = Node(13) actual = nums.peek() expected = 13 assert actual == expected
def test_pop(): nums = Stack() # Stand Alone testing: nums.top = Node(1) actual = nums.pop() expected = 1 assert actual == expected # Using methods from Stack class: nums.push(100, 13) actual = nums.pop() expected = 13 assert actual == expected
def test_push_one_value(): nums = Stack() nums.push(1) assert nums.top.value == 1
def test_peek_on_empty_stack(): nums = Stack() actual = nums.peek() expected = 'Stack is empty' assert actual == expected
def test_emptying_stack_with_multi_pop(): nums = Stack() # Stand Alone testing: nums.top = Node(1) temp = nums.top nums.top = Node(13) nums.top.next = temp nums.pop() nums.pop() actual = nums.pop() expected = 'Stack is empty' assert actual == expected # Using methods from Stack class: nums.push(100, 13) nums.pop() nums.pop() actual = nums.pop() expected = 'Stack is empty' assert actual == expected
def test_create_empty_stack(): nums = Stack() assert nums.top == None
def test_pop_from_empty_stack(): nums = Stack() actual = nums.pop() expected = 'Stack is empty' assert actual == expected
def test_push_multiple_values(): nums = Stack() nums.push(1, 3, 5, 9) assert nums.top.value == 9 assert nums.__str__() == 'top->9->5->3->1-> NULL'
def __init__(self): self.front_stack = Stack() self.rear_stack = Stack() self.len = 0
class PseudoQueue(): def __init__(self): self.front_stack = Stack() self.rear_stack = Stack() self.len = 0 def enqueue(self, value): """ Push Node(value) to front_stack """ self.len += 1 self.front_stack.push(value) def dequeue(self): """ Dequeue an item from the queue. 1) If both stacks are empty, return stack is empty. 2) If rear_stack is empty While front_stack is not empty, push everything from front_stack to rear_stack. 3) Pop the element from rear_stack and return it. """ if self.rear_stack.isEmpty(): while self.len > 0: self.rear_stack.push(self.front_stack.pop()) self.len -= 1 output = self.rear_stack.pop() while True: self.front_stack.push(self.rear_stack.pop()) self.len += 1 if self.rear_stack.isEmpty(): return output else: return "stack is empty!" def __str__(self): output = 'Rear ->' if self.front_stack.isEmpty(): current = self.rear_stack.top else: current = self.front_stack.top while current: output += f"{{{current.value}}} -> " current = current.next output += " Front" return output