예제 #1
0
 def __init__(self):
     self.modified_stack = ModifiedStack()
     self.pushed_element = None
     self.push_state = None
     self.pop_size = None
     self.top = None
     self.min = None
     self.input_array = None
     self.error_message = None
 def __init__(self, logger=RealLogger()):
     self.logger = logger
     self.modified_stack = ModifiedStack()
     self.pushed_element = None
     self.push_state = None
     self.pop_size = None
     self.top = None
     self.min = None
     self.input_array = None
     self.error_message = None
     self.logger.log('Stack object was successfully created')
 def test_push_one_change_min(self):
     stack = ModifiedStack([2])
     stack.push(-5)
     self.assertEqual(stack.find_min(), -5)
 def test_can_push_one_elem_in_stack(self):
     stack = ModifiedStack([2])
     stack.push(3)
     self.assertEqual(stack.look_top(), 3)
 def test_push_in_empty_stack(self):
     stack = ModifiedStack()
     stack.push(2)
     self.assertEqual(stack.look_top(), 2)
 def test_look_top_return_none_if_stack_empty(self):
     stack = ModifiedStack()
     self.assertEqual(stack.look_top(), None)
 def test_can_find_min_in_stack(self):
     stack = ModifiedStack()
     stack.push([10, 20])
     self.assertEqual(stack.find_min(), 10)
 def test_raise_pop_if_count_more_then_stack_size(self):
     stack = ModifiedStack()
     stack.push([20, 7, 56, 23])
     with self.assertRaises(ValueError):
         stack.pop(200)
 def test_in_not_empty(self):
     stack = ModifiedStack([1, 2, 3, 4])
     self.assertFalse(stack.is_empty())
 def test_is_empty(self):
     stack = ModifiedStack()
     self.assertTrue(stack.is_empty())
 def test_raise_when_create_dheap_with_float_d(self):
     with self.assertRaises(TypeError):
         ModifiedStack([1, 2.0, 3, set()])
 def test_raise_when_create_stack_with_not_list(self):
     with self.assertRaises(TypeError):
         ModifiedStack({'key1': 1, 'key2': 2})
 def test_can_create_stack_from_list(self):
     stack = ModifiedStack([1, 2, 6, 7, 8])
     self.assertEqual(stack.look_top(), 8)
class ModifiedStackViewModel(object):
    def __init__(self, logger=RealLogger()):
        self.logger = logger
        self.modified_stack = ModifiedStack()
        self.pushed_element = None
        self.push_state = None
        self.pop_size = None
        self.top = None
        self.min = None
        self.input_array = None
        self.error_message = None
        self.logger.log('Stack object was successfully created')

    def size(self):
        return self.modified_stack.size()

    def push(self):
        self.logger.log('Push button was clicked')
        self.clear_error_message()
        try:
            if self.push_state == 'One':
                self.modified_stack.push(to_int(self.pushed_element))
                self.logger.log(
                    'Operation push successfully completed with single element. Push: {}'
                    .format(to_int(self.pushed_element)))
            elif self.push_state == 'N':
                self.modified_stack.push(
                    [to_int(element) for element in self.input_array])
                self.logger.log(
                    'Operation push successfully completed with array. Push: {}'
                    .format(self.input_array))
        except Exception as e:
            self.set_error_message(str(e))
            self.logger.log('Push error: {}'.format(self.get_error_message()))

    def pop(self):
        self.logger.log('Pop button was clicked')
        self.clear_error_message()
        try:
            self.modified_stack.pop(to_int(self.pop_size))
            self.logger.log(
                'Operation pop successfully completed. Pop: {}'.format(
                    to_int(self.pop_size)))
        except Exception as e:
            self.set_error_message(str(e))
            self.logger.log('Pop error: {}'.format(self.get_error_message()))

    def get_top(self):
        self.logger.log('Getting top button was clicked')
        self.clear_error_message()
        if self.modified_stack.is_empty():
            self.set_error_message('Stack is empty')
            self.logger.log('Result from getting top element: {}'.format(
                self.get_error_message()))
        else:
            self.top = self.modified_stack.look_top()
            self.logger.log('Result from getting top element: {}'.format(
                self.top))

    def get_min(self):
        self.logger.log('Getting min button was clicked')
        self.clear_error_message()
        if self.modified_stack.is_empty():
            self.set_error_message('Stack is empty')
            self.logger.log('Result from getting min element: {}'.format(
                self.get_error_message()))
        else:
            self.min = self.modified_stack.find_min()
            self.logger.log('Result from getting min element: {}'.format(
                self.min))

    def set_pop_size(self, size):
        self.pop_size = size
        self.logger.log('Setting pop size to: {}'.format(self.pop_size))

    def set_push_state(self, state):
        self.push_state = state
        self.logger.log('Setting push state to: {}'.format(self.push_state))

    def set_pushed_element(self, value):
        self.pushed_element = value
        self.logger.log('Setting pushed element to: {}'.format(
            self.pushed_element))

    def set_input_array(self, input_array):
        self.input_array = input_array
        self.logger.log('Setting input array to: {}'.format(self.input_array))

    def set_error_message(self, message):
        self.error_message = message
        self.logger.log('Setting error message to: {}'.format(
            self.error_message))

    def get_error_message(self):
        return self.error_message

    def clear_error_message(self):
        self.error_message = None
        self.logger.log('Error message was successfully cleared')
 def test_can_create_empty__stack(self):
     stack = ModifiedStack()
     self.assertTrue(isinstance(stack, ModifiedStack))
 def test_size_return_zero_for_empty_stack(self):
     stack = ModifiedStack()
     self.assertEqual(stack.size(), 0)
 def test_can_pop_more_then_one_from_stack(self):
     stack = ModifiedStack()
     stack.push([20, 7, 56, 23])
     stack.pop(3)
     self.assertEqual(stack.look_top(), 20)
 def test_size_not_zero(self):
     stack = ModifiedStack([1, 2, 3, 4])
     self.assertEqual(stack.size(), 4)
 def test_raise_when_count_in_pop_not_int(self):
     with self.assertRaises(ValueError):
         stack = ModifiedStack([1, 45, 67, 67])
         stack.pop(-3.5)
 def test_can_look_top(self):
     stack = ModifiedStack([34, 467, 567, 768])
     self.assertEqual(stack.look_top(), 768)
 def test_find_min_return_none_if_stack_empty(self):
     stack = ModifiedStack()
     self.assertEqual(stack.find_min(), None)
예제 #22
0
class ModifiedStackViewModel(object):
    def __init__(self):
        self.modified_stack = ModifiedStack()
        self.pushed_element = None
        self.push_state = None
        self.pop_size = None
        self.top = None
        self.min = None
        self.input_array = None
        self.error_message = None

    def size(self):
        return self.modified_stack.size()

    def push(self):
        self.clear_error_message()
        try:
            if self.push_state == 'One':
                self.modified_stack.push(to_int(self.pushed_element))
            elif self.push_state == 'N':
                self.modified_stack.push(
                    [to_int(element) for element in self.input_array])
        except Exception as e:
            self.set_error_message(str(e))

    def pop(self):
        self.clear_error_message()
        try:
            self.modified_stack.pop(to_int(self.pop_size))
        except Exception as e:
            self.set_error_message(str(e))

    def get_top(self):
        self.clear_error_message()
        if self.modified_stack.is_empty():
            self.set_error_message('Stack is empty')
        else:
            self.top = self.modified_stack.look_top()

    def get_min(self):
        self.clear_error_message()
        if self.modified_stack.is_empty():
            self.set_error_message('Stack is empty')
        else:
            self.min = self.modified_stack.find_min()

    def set_pop_size(self, size):
        self.pop_size = size

    def set_push_state(self, state):
        self.push_state = state

    def set_pushed_element(self, value):
        self.pushed_element = value

    def set_input_array(self, input_array):
        self.input_array = input_array

    def set_error_message(self, message):
        self.error_message = message

    def get_error_message(self):
        return self.error_message

    def clear_error_message(self):
        self.error_message = None