'''
Created on Oct 22, 2014

@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 = "names"+str(number)+".txt"
        #TODO:
        print filename
        f = open(filename)
        for line in f:
            names = line.strip().split(":")
            for name in names:
                nameList.append(name.split()) 
        #print nameList
        f.close()       
    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 name in data:
        uniqueNames.add(name[0])
    return list(uniqueNames)


    
    
    
    
    
    
def allLastNames(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.
    '''
    allnames = []
    for name in data:
        if name[0] == firstName:
            allnames.append(name[1])
    #print firstName, allnames
    return allnames



    

 
    
    
def correspondingLastNames(data): 
    '''
    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:
    firsts = uniqueFirstNames(data)
    for name in firsts:
        lastNames.append(allLastNames(data,name))
    return lastNames

    
def printFirstWithLasts(firstNames, lastNames):
    print "Print Unique First Names with all Last Names"      
    #TODO:
    for (index,name) in enumerate(firstNames):
        print name + ":" + " ".join(lastNames[index])    

    
    
    


def main():

    fileWithNames = open("names1.txt")
    namelist = processFile(fileWithNames)  

    # for last problem: 
    namelist = processFiles(3)
    
    print namelist 
    firstNames = uniqueFirstNames(namelist)
    print firstNames
    lastNames = correspondingLastNames(namelist)
    print lastNames
    printFirstWithLasts(firstNames, lastNames)
    maxnum = max([len(item) for item in lastNames])
    print maxnum
    lastIndex = [index for (index, v) in enumerate(lastNames) if len(v) == maxnum][0]
    print "first name with most last names is:"
    #TODO:
    print firstNames[lastIndex] + ": " + " ".join(lastNames[lastIndex])
    

if __name__ == "__main__":
    main() 
        
     
