Ejemplo n.º 1
0
    def test_stack_empty(self):
        stack = Stack()
        self.assertTrue(stack.empty())

        stack.push(1)
        self.assertFalse(stack.empty())

        stack.push(2)
        self.assertEqual(stack.__str__(), "Stack[2, 1]")
        self.assertFalse(stack.empty())

        stack.clear()

        self.assertTrue(stack.empty())
Ejemplo n.º 2
0
    def test_stack_pop_top(self):
        stack = Stack()

        self.assertTrue(stack.empty())

        stack.push(1)
        self.assertFalse(stack.empty())
        self.assertEqual(stack.top(), 1)

        stack.push(2)
        self.assertEqual(stack.top(), 2)

        stack.push(3)
        self.assertEqual(stack.top(), 3)
        self.assertEqual(stack.__str__(), "Stack[3, 2, 1]")

        self.assertEqual(stack.pop(), 3)

        self.assertEqual(stack.pop(), 2)

        self.assertEqual(stack.pop(), 1)
        self.assertTrue(stack.empty())
Ejemplo n.º 3
0
def calculate_span(stock_price: list):
    if len(stock_price) == 0:
        raise ValueError("The list cannot be an empty list")

    if not isinstance(stock_price, list):
        raise ValueError("The `stocks_prices` must be a list")

    if not all([isinstance(i, int) for i in stock_price]):
        raise ValueError("All values must be integers")

    s = Stack(len(stock_price) + 1)
    s.push(0)
    span = [0] * len(stock_price)
    for idx, i in enumerate(stock_price):
        while not s.empty() and stock_price[s.top.value] <= i:
            v = s.pop()
        if (s.empty()):
            span[idx] = idx + 1
        else:
            span[idx] = idx - s.top.value
        s.push(idx)
    return span
 def test_empty(self):
     s = Stack(10)
     assert s.empty()