'''
Created on Feb 27, 2011

@author: ola
'''
import random,time
_MAX = 500

def create_list(n):
    return [random.randint(0,_MAX) for i in xrange(0,n)]


def mode_compA(nums):
    occs = [nums.count(n) for n in nums]
    m = max(occs)
    mindex = occs.index(m)
    return nums[mindex]

def mode_compB(nums):
    occs = [nums.count(n) for n in nums]
    m = max(occs)
    for n in nums:
        if nums.count(n) == m:
            return n
        
def mode_compC(nums):
    occs = [nums.count(n) for n in nums]
    for n in nums:
        if nums.count(n) == max(occs):
            return n
        
def mode_compD(nums):
    mn = min(nums)
    mx = max(nums)
    counts = [0]*(mx-mn+1)   
    for n in nums:
        counts[n-mn] += 1
    mindex = counts.index(max(counts)) 
    return mindex+mn    
        
def time_all(n):
    funcs = [mode_compA,mode_compB,mode_compC,mode_compD]
    nums = create_list(n)
    for f in funcs:
        start = time.time()
        mode = f(nums)
        end = time.time()
        print "%s mode = %d time = %4.3f\n" % (f,mode,(end-start))

if __name__ == "__main__":
    time_all(5000)
        