Something I am constantly wanting to do in a lot of programs is see if a value is in an array. For another function which I wish to write to add to the Zeller program this will come in handy. The basic algorithm looks like this:
SET value TO RequiredValue
SET array TO ArrayToSearch
SET isIn TO False
Check each item in array
IF item = value
SET isIn TO True
RETURN isIn
The algorith is rather simple and translates to the following Python function:
def inArray(value, array): isIn = False for i in range(0, len(array)): if array[i] == value: isIn = True return isIn
Running the function produces the following results:
>>> inArray(2, [4,2,3,6]) True >>> inArray(2, [4,3,6]) False
This is exactly what I was looking for in the first test the 2 is found and the function returns True, in the second test 2 is not found and the function returns False.