""" sorted list class """
from time import time


class SortedList(list):
    """ list in non-decreasing order """

    def __contains__(self, v, start=None, stop=None):
        """ Return whether SortedList (self) contains v.

        @param SortedList self: this SortedList
        @param v int: value we are looking for
        @param start int: index in the list where we start looking
        @param stop int: index in the list where we stop looking

        >>> L = SortedList()
        >>> 15 in L
        False
        >>> L = SortedList([17])
        >>> 17 in L
        True
        >>> L = SortedList([5, 10, 15, 20])
        >>> 15 in L
        True
        >>> 17 in L
        False
        """
        if start is None:
            start, stop = 0, len(self) - 1
        if start > stop:
            return False
        elif start == stop:
            return self[start] == v
        else:
            mid = (stop + start) // 2
            if self[mid] < v:
                return self.__contains__(v, mid + 1, stop)
            else:
                return self.__contains__(v, start, mid)


def timing_tests(n):
    """ Timing tests ..
    """
    lst = list(range(n))
    start = time()
    # Looking for value n/2
    found = n // 2 in lst
    print('Searched in Python list L[{:8} elements]: {:8.5f} sec, result={}'.
          format(n, time() - start, found))

    lst = SortedList(lst)
    start = time()
    found = n // 2 in lst
    print('Searched in SortedList  L[{:8} elements]: {:8.5f} sec, result={}\n'.
          format(n, time() - start, found))

if __name__ == '__main__':
    import doctest
    doctest.testmod()

    n = 1000000
    for _ in range(6):
        timing_tests(n)
        n *= 2
