def partition(iterable, pred, first=0, last=None):
    """
    Parition the iterable from first to last such that all elements
    that satisfy pred are before all elements where pred is false.
    Returns one past the index of the last element where pred is true.
    Relative order of the elements is not preserved.

    Parameters
    ----------
    iterable: a muteable iterable object with __get_item__
    pred: unary boolean predicate function
    first: first element to check
    last: one past last element to check. None will check until end of iterable

    Returns
    -------
    partition_idx: One past last element index where pred is true
    """
    assert hasattr(iterable, '__getitem__')
    last = last or len(iterable)
    assert first <= last
    while first != last:
        # advance first
        while pred(iterable[first]):
            first += 1
            if first == last:
                return first
        # decrement last
        last -= 1
        if first == last:
            return first
        while not pred(iterable[last]):
            last -= 1
            if first == last:
                return first
        # Swap
        iterable[first], iterable[last] = iterable[last], iterable[first]
    return first