示例#1
0
class TestStack(unittest.TestCase):
    def setUp(self):
        self.stack = Stack()

    def tearDown(self):
        return super().tearDown()

    def test_size(self):
        self.assertEqual(self.stack.size(), 0)

    def test_push(self):
        for i in range(10):
            self.stack.push(i)

        self.assertEqual(self.stack.size(), 10)

    def test_pop(self):
        for i in range(10):
            self.stack.push(i)

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

    def test_top(self):
        self.stack.push(1)
        self.assertEqual(self.stack.top(), 1)

    def test_pop_exception(self):
        self.assertIsNone(self.stack.pop())

    def test_top_exception(self):
        self.assertIsNone(self.stack.top())
def stack_span(prices):
    st = Stack()
    nelements = len(prices)
    span = [0] * nelements
    for i in range(nelements):
        while not (st.is_empty()) and prices[st.top()] <= prices[i]:
            _ = st.pop()
        span[i] = i + 1 if st.is_empty() else i - st.top()
        st.push(i)
    return span
class TestStack(unittest.TestCase):

    def setUp(self) -> None:
        self.st = Stack()

    def test_is_empty(self):
        self.assertTrue(self.st.is_empty(), "The stack is not empty")
        self.assertRaises(IndexError, self.st.pop)

    def test_repr(self):
        self.assertEqual("Stack()", repr(self.st), "The repr method does not work properly")

    def test_push_one_item(self):
        self.st.push(2)
        self.assertFalse(self.st.is_empty())
        self.assertEqual("Stack(2)", str(self.st))

    def test_push_multiple_items(self):
        self.st.push(3)
        self.st.push(10)
        self.assertEqual("Stack(3, 10)", str(self.st))
        popped_item = self.st.pop()
        self.assertEqual(10, popped_item)

    def test_is_full(self):
        for i in range(10):
            self.st.push(i)
        self.assertTrue(self.st.is_full())
        self.assertRaises(OverflowError, self.st.push, 12)

    def test_pop_one_item(self):
        self.st.push(3)
        popped_item = self.st.pop()
        self.assertEqual(3, popped_item)

    def test_pop_multiple_items(self):
        self.st.push(3)
        self.st.push(10)
        popped_item = self.st.pop()
        self.assertEqual(10, popped_item)
        popped_item = self.st.pop()
        self.assertEqual(3, popped_item)

    def test_type(self):
        self.assertRaises(TypeError, self.st.push, "3")
        self.assertRaises(TypeError, self.st.push, 3.1)
        self.assertRaises(TypeError, self.st.push, True)

    def test_top_non_empty_stack(self):
        self.st.push(3)
        self.st.push(10)
        self.assertEqual(10, self.st.top())

    def test_top_empty_stack(self):
        self.assertRaises(IndexError, self.st.top)
示例#4
0
    def post_order_recursion(self):
        post_order_list = []
        node = self
        pre_node = None
        current_node = None
        s = Stack()
        s.push(node)

        while not s.is_empty():
            current_node = s.top()
            if (not current_node.left and not current_node.right) or \
                    (pre_node and ((current_node.left and pre_node.key == current_node.left.key)
                                   or (current_node.right and pre_node.key == current_node.right.key))):
                post_order_list.append(current_node)
                s.pop()
                pre_node = current_node
            else:
                if current_node.right:
                    s.push(current_node.right)
                if current_node.left:
                    s.push(current_node.left)
        return post_order_list
示例#5
0
class QuadrupleHelper(object):
    def __init__(self):
        self.stack_tokens = Stack()
        self.stack_operands = Stack()
        self.stack_tags = Stack()
        self.stack_types = Stack()
        self.stack_jumps = Stack()
        self.stack_dimensions = Stack()
        self.stack_tokens.push(999)
        self.stack_operands.push(999)
        self.stack_types.push(999)
        self.stack_jumps.push(999)
        self.stack_tags.push(999)
        self.stack_dimensions.push(999)
        self.queue_quad = []
        self.quad_cont = 0
        self.temp_cont = 10001

    # token = Numeric code representation of an operator.
    # operand1 = Memory address of operand 1.
    # operand2 = Memory address of operand 2.
    # operand3 = Memory address of operand 3.
    def add_quad(self, token, operand1, operand2, operand3):
        new_quad = Quadruple(token, operand1, operand2, operand3)
        self.queue_quad.append(new_quad)
        self.quad_cont = self.quad_cont + 1

    def push_operand(self, operand):
        self.stack_operands.push(operand)

    def pop_operand(self):
        return self.stack_operands.pop()

    def top_operand(self):
        return self.stack_operands.top()

    def push_token(self, token):
        self.stack_tokens.push(token_to_code.get(token))

    def pop_token(self):
        return self.stack_tokens.pop()

    def top_token(self):
        return self.stack_tokens.top()

    def push_type(self, type):
        self.stack_types.push(type_to_code.get(type))

    def pop_type(self):
        return self.stack_types.pop()

    def top_type(self):
        return self.stack_types.top()

    def push_jump(self, jump):
        self.stack_jumps.push(jump)

    def pop_jump(self):
        return self.stack_jumps.pop()

    def top_jump(self):
        return self.stack_jumps.top()

    def push_tag(self, tag):
        self.stack_tags.push(tag)

    def pop_tag(self):
        return self.stack_tags.pop()

    def top_tag(self):
        return self.stack_tags.top()

    def push_dimension(self, id, dimension):
        self.stack_dimensions.push(id, dimension)

    def pop_dimension(self):
        return self.stack_dimensions.pop()

    def top_dimension(self):
        return self.stack_dimensions.top()

    def fill(self, quadruple_index, value):
        self.queue_quad[quadruple_index].operand3 = value

    def reset(self):
        self.__init__()

    def print_to_file(self, file_name):
        """
        Description: Generates .obj file
        """
        file = open(file_name, "w")
        cont = 0  # for debugging purposes.
        for quad in self.queue_quad:
            file.write(str(quad))
            file.write("\n")
            # TODO: Every quad should be an INT bc it is a memory address
            # Print for debugging.
            # print(type(code_to_token.get(quad.token)), code_to_token.get(quad.token))
            # print(type(quad.operand1), quad.operand1)
            # print(type(quad.operand2), quad.operand2)
            # print(type(quad.operand3), quad.operand3)
            # # Uncomment for debbuging
            print(
                "%s %s, %s, %s, %s"
                % (
                    cont,
                    code_to_token.get(quad.token),
                    quad.operand1,
                    quad.operand2,
                    quad.operand3,
                )
            )
            cont = cont + 1
        file.close()