'''
Created on Oct 26, 2011

@author: rodger
'''

def processFile(file):
    '''
    The parameter file is the name of a file with one or more names
    on a line. Each name is two words separated by a blank.
    Multiple names on a line are separated by a colon
    Return a list of names
    '''
    nameList = []
    for line in file:
        listNames = line.strip().split(':')
        [nameList.append(nm.split()) for nm in listNames]
    return nameList

def processFiles(numberFiles):
    ''' Given the number of files to process, assume all have
    the name "namesNUM.txt". Process all those files
    '''
    nameList = []
    for number in range(1,numberFiles+1):
        filename = ""
        #TODO:
        filename = "../data/names" + str(number) + ".txt"
        file = open(filename)
        moreNames = processFile(file)
        [nameList.append(name) for name in moreNames]
        file.close()
    return nameList
        
        
        
        
    return nameList

def uniqueFirstNames(data):
    '''
    The parameter data is a list of lists where each list is two 
    strings representing the first and last name of a person. 
    Return a list of the unique first names of all the people.
    '''
    uniqueNames = set([])
    #TODO:
    for item in data:
        uniqueNames.add(item[0])
    return list(uniqueNames)
    
    
    
    
    
    
def uniqueLastNames(data, firstName):
    '''
    The parameter data is a list of lists where each list is two  
    strings representing the first and last name of a person. 
    The string parameter firstname is the first name of a person (one word).
    Return a list of the last names of all the people from data with this 
    first name. Assume each person in data is unique.
    '''
    #return []  #TODO; Replace me
    return [item[1]   for item in data if item[0] == firstName]


    
 
    
    
def correspondingLastNames(data, firstNames): 
    '''
    The parameter data is a list of lists where each list is two  
    strings representing the first and last name of a person. 
    The parameter firstNames is a list of the unique first names from
    data.
    return a list of lists of last names such that the nth list of last
    names corresponds to the nth first name in firstNames.
    '''
    lastNames = []
    #TODO:
    for name in firstNames:
        listNames = uniqueLastNames(data, name)
        lastNames.append(listNames)
    return lastNames
    
    
    
    
    
    
    
    
    return lastNames
    
def printFirstWithLasts(firstNames, lastNames):
    print "Print Unique First Names with all Last Names"      
    #TODO:
    for (index, first) in enumerate(firstNames):
        print first + ":" + ' '.join(lastNames[index])
    
    
    
    


def main():
    '''
    This is the code for processing one file named names1.txt
    
    fileWithNames = open("../data/names1.txt")
    namelist = processFile(fileWithNames)  
    '''
    # this is the code for processing three files
    namelist = processFiles(3)
    
    print "namelist is: ", namelist 
    print
    firstNames = uniqueFirstNames(namelist)
    print "firstNames is: ", firstNames
    print
    lastNames = correspondingLastNames(namelist, firstNames)
    print "lastNames is: ", lastNames
    print
    printFirstWithLasts(firstNames, lastNames)
    print
    maxnum = max(len(item) for item in lastNames)
    print "maximum number of corresponding last names is ",
    print maxnum
    print
    lastIndex = [index for (index, v) in enumerate(lastNames) if len(v) == maxnum]
    print "first name with most last names is:"
    #TODO:
    print firstNames[lastIndex[0]] + ":" + ' '.join(lastNames[lastIndex[0]])
    

if __name__ == "__main__":
    main() 
        
     
