# Programmer: Sriram Pemmaraju # Date: Feb 26th, 2013 # The program reads a file with nonnegative integers (in no pre-specified format) # and reports the number of distinct nonnegative integers in the file. # This function takes a string consisting of non-negative integers and # returns a list containing all the integers in the line. # The integers in the line are separated by 1 or more blanks. def parse(s): listOfNumbers = [] # maintains the list of numbers in strings s currentNumber = "" # The function oscillates between two states: in one state # it is processing the digits of an integer and the other state # it is processing the white spaces between consecutive integers. # The boolean variable numberBeingProcessed is used to keep track # of this state. numberBeingProcessed = False i = 0 # serves as an index into the string s while i < len(s): # if the current character is a digit if s[i] >= "0" and s[i] <= "9": numberBeingProcessed = True currentNumber = currentNumber + s[i] # else if the current character is a non-digit # immediately following a number elif numberBeingProcessed: listOfNumbers.append(int(currentNumber)) numberBeingProcessed = False currentNumber = "" i = i + 1 # Clean-up, if the last character of string s is a digit if numberBeingProcessed: listOfNumbers.append(int(currentNumber)) return listOfNumbers # Takes two lists L1 and L2 and returns the list obtained # by appending to L1, all elements in L2 that are not in L1 def uniqueExtend(L1, L2): index = 0 # serves as index into list L2 # Loop to walk through elements of L2 while index < len(L2): # If current element of L2 is not in L1, then append it if not(L2[index] in L1): L1.append(L2[index]) index = index + 1 return L1 # Open a file called test.txt for read only and read the first line f = open("test.txt", "r") line = f.readline() masterList = [] # keeps track of the list of distinct integers in the file # Process each line, if line is non-empty while line: # Parse the line to extract a list of numbers in the line numbersInLine = parse(line) # Extend the masterList by appending to it all the new # numbers in the line. masterList = uniqueExtend(masterList, numbersInLine) # Read the next line line = f.readline() f.close() print len(masterList)