Exemplo n.º 1
0
    def test_pop_multiple_elements_from_normal_stack(self):
        stack = Stack()
        stack.push(1)
        stack.push(2)
        stack.push(3)

        test_value1 = stack.pop()
        check_value1 = 3

        test_value2 = stack.pop()
        check_value2 = 2

        result_list = stack.stack
        check_list = [1]

        self.assertEqual(
            result_list, check_list,
            "Testing 'Pop'. Pop multiple elements from normal stack. Stack lists not equal"
        )
        self.assertEqual(
            test_value1, check_value1,
            "Testing 'Pop'. Pop multiple elements from normal stack. Popped value not correct"
        )
        self.assertEqual(
            test_value2, check_value2,
            "Testing 'Pop'. Pop multiple elements from normal stack. Popped value not correct"
        )
Exemplo n.º 2
0
 def test_peek(self):
     my_stack = Stack()
     my_stack.push(1)
     my_stack.push(2)
     my_stack.push(3)
     data = my_stack.stack + [my_stack.peek()]
     res =  [3, 2, 1, 3]
     self.assertEqual( data, res)
Exemplo n.º 3
0
 def test_push(self):
     my_stack = Stack()
     my_stack.push(1)
     my_stack.push(2)
     my_stack.push(3)
     data = my_stack.stack
     res =  [3, 2, 1]
     self.assertEqual( data, res)
Exemplo n.º 4
0
 def test_size(self):
     my_stack = Stack()
     my_stack.push(1)
     my_stack.push(2)
     my_stack.push(3)
     data = my_stack.size()
     res = 3
     self.assertEqual( data, res)
Exemplo n.º 5
0
 def test_pop_in_middle(self):
     my_stack = Stack()
     my_stack.push(1)
     my_stack.push(2)
     pop_el = my_stack.pop()
     my_stack.push(3)
     data = my_stack.stack + [pop_el]
     res =  [3, 1, 2]
     self.assertEqual( data, res)
Exemplo n.º 6
0
 def test_pop_one(self):
     my_stack = Stack()
     my_stack.push(1)
     my_stack.push(2)
     my_stack.push(3)
     pop_el = my_stack.pop()
     data = my_stack.stack + [pop_el]
     res =  [2, 1, 3]
     self.assertEqual( data, res)
Exemplo n.º 7
0
    def test_push_single_element_in_empty_stack(self):
        stack = Stack()
        stack.push(1)

        result_list = stack.stack
        check_list = [1]

        self.assertEqual(
            result_list, check_list,
            "Testing 'Push'. Pushing single element. Stack lists not equal")
Exemplo n.º 8
0
 def test_pop_all(self):
     my_stack = Stack()
     my_stack.push(1)
     my_stack.push(2)
     my_stack.push(3)
     pop_el1 = my_stack.pop()
     pop_el2 = my_stack.pop()
     pop_el3 = my_stack.pop()
     data = my_stack.stack + [pop_el1] + [pop_el2] + [pop_el3]
     res =  [3, 2, 1]
     self.assertEqual( data, res)
Exemplo n.º 9
0
def test_stack():
    print('Stack.push() 1, 3, 5, 7')
    stack = Stack()
    stack.push(1)
    stack.push(3)
    stack.push(5)
    stack.push(7)

    print('Stack.pop() loop')
    for item in stack:
        print('Item: %s Len: %d' % (item, len(stack)))
Exemplo n.º 10
0
    def test_push_multiple_elements_in_empty_stack(self):
        stack = Stack()
        stack.push(1)
        stack.push(2)
        stack.push(3)

        result_list = stack.stack
        check_list = [3, 2, 1]

        self.assertEqual(
            result_list, check_list,
            "Testing 'Push'. Pushing multiple elements. Stack lists not equal")
Exemplo n.º 11
0
    def test_peek_normal_stack(self):
        stack = Stack()
        stack.push(1)
        stack.push(2)
        stack.push(3)

        test_value = stack.peek()
        check_value = 3

        result_list = stack.stack
        check_list = [3, 2, 1]

        self.assertEqual(
            result_list, check_list,
            "Testing 'Peek'. Peek from normal stack. Stack lists not equal")
        self.assertEqual(
            test_value, check_value,
            "Testing 'Peek'. Peek from normal stack. Peek value is incorrect")
Exemplo n.º 12
0
 def test_random(self):
     for i in range(10000):
         s = Stack()
         res = []
         comand = random.randint(0, 1) # 0 - pop, 1 - push
         if comand:
             t = random.randint(0,9)
             s.push(t)
             res.append(t)
         else:
             s.pop()
             try:
                 res.pop()
             except:
                 pass
         data = s.stack + [s.size()]
         res =  res[::-1] + [len(res)]
         self.assertEqual( data, res)
Exemplo n.º 13
0
    def test_pop_single_element_from_normal_stack(self):
        stack = Stack()
        stack.push(1)
        stack.push(2)
        stack.push(3)

        test_value = stack.pop()
        check_value = 3

        result_list = stack.stack
        check_list = [2, 1]

        self.assertEqual(
            result_list, check_list,
            "Testing 'Pop'. Pop single element from normal stack. Stack lists not equal"
        )
        self.assertEqual(
            test_value, check_value,
            "Testing 'Pop'. Pop single element from normal stack. Popped value not correct"
        )
def check_balance(input_text, brackets=BRACKETS):
    brackets_stack = Stack()
    brackets_stack.list_of_values = []
    open_brackets = brackets[::2]
    close_brackets = brackets[1::2]
    # Проходим по каждому символу входного текста
    for symbol in input_text:
        # если символ это открывающая скобка, то добавляем его в стек
        if symbol in open_brackets:
            brackets_stack.push(open_brackets.index(symbol))
        # или если символ это закрывающая скобка
        elif symbol in close_brackets:
            # и если стек не пуст и  последняя добавленная открывающая скобка такого же типа, то удаляем
            if (not brackets_stack.is_empty()) and (brackets_stack.peek() == close_brackets.index(symbol)):
                brackets_stack.pop()
            # если не такого же типа, то сразу выводим ошибку
            else:
                return print('Несбалансированно')
    # проверяем стек, если он пустой, то возвращаем 'Сбалансированно', иначе - 'Несбалансированно'
    if not brackets_stack.list_of_values:
        return print('Сбалансированно')
    else:
        return print('Несбалансированно')