'''
Created on Nov 21, 2011

@author: rodger
'''
import time

def find_two_smallest(L):
    smallest = min(L)
    min1 = L.index(smallest)
    '''
    smallest = L[0]
    min1 = 0
    for (i,n) in enumerate(L):
        if n < smallest: # found smaller one
            smallest = n
            min1 = i
    '''        
            
    L.remove(smallest)
    next_smallest = min(L)
    min2 = L.index(next_smallest)
    
    L.insert(min1,smallest)
    if min1 <= min2:
        min2 += 1
    return (min1,min2)

def find_two_smallest2(L):
    temp_list = L[:]
    temp_list.sort()
    smallest = temp_list[0]
    next_smallest = temp_list[1]
    min1 = L.index(smallest)
    if smallest == next_smallest:
        L2 = L[min1+1:]
        min2 = L2.index(next_smallest) + min1+1
    else:
        min2 = L.index(next_smallest)
    return (min1,min2)

def find_two_smallest3(L):
    if L[0] < L[1]:
        min1, min2 = 0,1
    else:
        min2, min1 = 0,1
    for i in range(2,len(L)):
        if L[i] < L[min1]:
            min2 = min1
            min1 = i 
        elif L[i] < L[min2]:
            min2 = i 
    return (min1, min2)
    


def main():
    filename = "../data/numbers.txt"
    file = open(filename)
    nlist = [int(num.strip()) for num in file]
    file.close()
    #functions = [find_two_smallest, find_two_smallest2]
    functions = [find_two_smallest, find_two_smallest2, find_two_smallest3]
    for f in functions:
        start = time.time()
        (index1, index2) = f(nlist)
        end = time.time()
        print "two smallest and their positions are"
        print str(nlist[index1:index1+1]) + " at position "  + str(index1)
        print str(nlist[index2:index2+1]) + " at position "  + str(index2)
        print "time was " + str(end - start)
print "starting"
main()


 
        
