# L-27 MCS 260 Monday 16 March 2015 : useassert.py

"""
The function below illustrates the use of the assert utility
to enforce preconditions and postconditions of the function.
"""

def searchlist(items, item):
    """
    Searches a list items for the integer item,
    returns -1 if the item does not belong to the list,
    or else returns the position of the item in the list.

    Preconditions:
    isinstance(items, list) and isinstance(item, int)

    Postconditions:
    searchlist(items, item) == -1 or
    searchlist(items, item) == pos and items[pos] == item
    """
    assert isinstance(items, list)
    assert isinstance(item, int)
    pos = -1
    ind = 0
    while ind < len(items):
        assert not (item in items[:ind])
        if items[ind] == item:
            pos = ind
            break
        else:
            ind = ind + 1
    assert pos == -1 or (pos == ind and items[ind] == item)
    return pos

def main():
    """
    Runs a test on the searchlist function.
    """
    alist = list(range(5))
    print(searchlist(alist, 3))

main()
