9.12.2010

Program max()

Here is my program for finding the max value of a list in python.

[001]def findMax(inList):
[002]        maxV = inList[0]
[003]        for i in inList:
[004]                if maxV < i:
[005]                        maxV = i
[006]        return maxV


The function will terminate because the for loop is finite because the input inList is also finite.
The function compares the first value in the list with itself. Then it compares that value to the second value of inList. If the new value is larger than maxV, then it becomes maxV. Then maxV is compared to the third value in the list. This continues until all values in the list are compared. So, by comparing every value, the function will return the greatest value.

No comments:

Post a Comment