Example #1
0
    def insert(self, index, value):
        """Insert item by index.

        Args:
            index (int): index for insert
            value : value for insert

        Raises:
            CollectionIsEmptyExeption: if list is empty
            IndexError: if index is not valid
        """
        if self.is_empty:
            raise CollectionIsEmptyExeption(list_is_empty())
        if not self.index_valid(index):
            raise IndexError(index_out_of_range())
        if index == 0:
            self.push_front(value)
            return
        else:
            node = Node(value)
            previously = self._find_by(index - 1)
            node.next = previously.next
            previously.next = node

            self._size += 1
Example #2
0
    def front(self):
        """Return (not extract) first item from list.

        Returns:
            item: first item from list

        Raises:
            CollectionIsEmptyExeption: if list is empty
        """
        if self.is_empty:
            raise CollectionIsEmptyExeption(list_is_empty())
        return self._head.data
Example #3
0
    def pop_back(self):
        """Return (and extract) last item from list.

        Returns:
            last_item: last item from list

        Raises:
            CollectionIsEmptyExeption: if list is empty
        """
        if self.is_empty:
            raise CollectionIsEmptyExeption(list_is_empty())

        penultimate = self._find_by(self._size - 2)
        last = penultimate.next
        penultimate.next = None
        self._size -= 1

        return last.data
Example #4
0
    def pop_front(self):
        """Return (and extract) first item from list.

        Returns:
            first_element_data: first item from list

        Raises:
            CollectionIsEmptyExeption: if list is empty
        """
        if self.is_empty:
            raise CollectionIsEmptyExeption(list_is_empty())

        return_value = self._head.data

        self._head = self._head.next
        self._size -= 1

        return return_value
Example #5
0
    def value_at(self, index):
        """Return (not extract) item by index.

        Returns:
            item: item from list by index

        Args:
            index (int): index return item

        Raises:
            CollectionIsEmptyExeption: if list is empty
            IndexError: if index is not valid
        """
        if self.is_empty:
            raise CollectionIsEmptyExeption(list_is_empty())
        if not self.index_valid(index):
            raise IndexError(index_out_of_range())

        finded = self._find_by(index)
        return finded.data
Example #6
0
def test_back_from_empty():
    list = LinkedList()

    with pytest.raises(CollectionIsEmptyExeption) as exception_info:
        value = list.back
    assert str(exception_info.value) == list_is_empty()
Example #7
0
def test_pop_front_negative(value_at_fixture):
    list = LinkedList()

    with pytest.raises(CollectionIsEmptyExeption) as exception_info:
        value = list.pop_back()
    assert str(exception_info.value) == list_is_empty()
Example #8
0
def test_value_at_empty_list():
    list = LinkedList()

    with pytest.raises(CollectionIsEmptyExeption) as exception_info:
        value = list.value_at(list.size)
    assert str(exception_info.value) == list_is_empty()