def test_peek(): stack = Stack() stack.push('Dwight') stack.push('Michael') stack.push('Jim') stack.push('Andy') stack.peek() actual = stack.top.value expected = 'Andy' assert actual == expected
def test_peek_one(): s = Stack() s.push("apple") s.push("banana") actual = s.peek() expected = "banana" assert actual == expected
class PsuedoQueue: """ This is a queue being implement using stack's push, pop and peek methods rather than having its own attributes and methods """ def __init__(self): self.front_stack = Stack() self.rear_stack = Stack() # this is strictly here so that we may pass a test, self.front = None def peek(self): return self.front_stack.peek() def is_empty(self): return self.front_stack.is_empty() def enqueue(self, value=None): node = Node(value) if self.rear_stack.top: self.rear_stack.top.next = node self.rear_stack.top = node if not self.front_stack.top: self.front_stack.top = node # next line is solely here to pass a test self.front = self.front_stack.top def dequeue(self): # if the queue only holds one node, we must manually set the rear to None if (self.rear_stack.top == self.front_stack.top): self.rear_stack.top = None value = self.front_stack.pop() # next line is solely here to pass a test self.front = self.front_stack.top return value
def test_peek_empty(): s = Stack() with pytest.raises(InvalidOperationError) as e: s.peek() assert str(e.value) == "Method not allowed on empty collection"
def test_peek(): s = Stack() s.push("apple") actual = s.peek() expected = "apple" assert actual == expected
def test_peek_on_empty(): new_stack = Stack() with pytest.raises(InvalidOperationError) as e: new_stack.peek()