Пример #1
0
 def test_is_empty(self):
     """
 Test for is_empty method
 """
     stack = Stack()
     self.assertEqual(stack.is_empty(), True)
     stack.push(1)
     self.assertEqual(stack.is_empty(), False)
     stack.pop()
     self.assertEqual(stack.is_empty(), True)
Пример #2
0
class TestStack(unittest.TestCase):

    def setUp(self):
        self.stack = Stack()

    def test_define_a_stack(self):
        self.assertIsInstance(self.stack, Stack)

    def test_if_stack_is_empty(self):
        self.assertTrue(self.stack.is_empty())

    def test_if_stack_is_not_empty(self):
        self.stack.push(1)

        self.assertFalse(self.stack.is_empty())

    def test_add_element_to_stack(self):
        self.stack.push(1)

        self.assertEquals(self.stack.stack, [1])

    def test_add_element_to_an_fulfilled_stack(self):
        self.stack.push(1)

        with self.assertRaises(FullStackError):
            self.stack.push(2)

    def test_pop_an_element_to_stack(self):
        self.stack.push(1)
        element = self.stack.pop()

        self.assertEquals(element, 1)

    def test_pop_an_element_from_empty_stack(self):
        with self.assertRaises(EmptyStackError):
            self.stack.pop()

    def test_peek_an_element_from_stack(self):
        self.stack.max_size = 2  # Patch object's capacity
        self.stack.push(1)
        self.stack.push(2)
        element = self.stack.peek()

        self.assertEquals(element, 2)

    def test_peek_an_element_from_empty_stack(self):
        with self.assertRaises(EmptyStackError):
            self.stack.peek()

    def test_get_size_if_stack(self):
        self.stack.push(1)

        self.assertEquals(self.stack.size(), 1)
def is_balanced(expr):
    stack = Stack()

    for elem in expr:
        if elem in '({[<':
            stack.push(elem)
        else:
            if stack.is_empty():
                return False
            else:
                top = stack.pop()
                if top != symbol_map[elem]:
                    return False

    if stack.is_empty():
        return True
    else:
        return False
def base_converter(num, base):
    digits = '0123456789ABCDEF'
    stack = Stack()

    while num > 0:
        remainder = num % base
        stack.push(remainder)
        num = num // base

    binary_str = ""
    while not stack.is_empty():
        binary_str += digits[stack.pop()]

    return binary_str