#Programmer: Sriram Pemmaraju #Date: 2-19-2009 # #Reads a list of integers from a file, followed by an integer provided #by the user and then searches for that integer in the list. I also #compute the time it takes to perform the search. import time #Reads a sequence of integers, separated by spaces, from a file #which is passed in as an argument to the function. The sequence is #stored in a list and returned. def getList(fileref): l = [] #readlines() returns the contents of file as a list of strings #with each line stored in a single string lines = fileref.readlines() for s in lines: l = l + s.split() for i in range(0, len(l)): l[i] = int(l[i]) return l #Starts by asking the user to pick a file consisting of a sequence #of integers separated by spaces. This sequence is read into a list #and then the user is asked to input an integer they want searched for. #Performs a linear search and prints "found" or "not found." The #search is also timed. def search(): fileref = open(pickAFile(), 'r') l = getList(fileref) x = requestInteger("Type an integer you want to search for.") found = 0 #The main loop that performs the search. t0 is the start time, #t1 is the end time and the difference is the time it takes to #search t0 = time.time() for y in l: if (x == y): found = 1 t1 = time.time() if(found): printNow("Found " + str(x)) else: printNow("Not Found " + str(x)) printNow("It took " + str((t1-t0)) + " seconds to complete the search.")