'''
Created on Oct 31, 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 = "../data/names" + str(number) + ".txt"
        file = open(filename)
        moreNames = processFile(file)
        [nameList.append(name) for name in moreNames]
        file.close()
    return nameList
        
def printFirstWithLasts(namemap):
    print "Print Unique First Names with all Last Names"      
    #TODO:
    for key in namemap:
        print key + ":" + ' '.join(namemap[key])






    
    
def mapNameToNumberLastNames(countmap, data):
    '''
    countmap is a map of first names to its number of occurrences
    The parameter data is a list of lists where each list is two  
    strings representing the first and last name of a person.         
    '''
    #todo:
    for item in data:
        first = item[0]
        #last = item[1]
        if first in countmap:
            countmap[first] = countmap[first] +1
        else:
            countmap[first] = 1






            
def  mapNameToLastNames(namemap, data):
    '''
    namemap is a map of first names to list of corresponding last names 
    The parameter data is a list of lists where each list is two  
    strings representing the first and last name of a person.    
    '''  
    #todo:
    for item in data:
        first,last = item[0],item[1]
        if first in namemap:  #update list, add in last
            namemap[first].append(last)
        else:  # first is not in list
            namemap[first] = [last]








def  mapNameToSetLastNames(namesetmap, data):
    '''
    namemap is a map of first names to list of corresponding last names 
    The parameter data is a list of lists where each list is two  
    strings representing the first and last name of a person.    
    '''  
    for item in data:
        first,last = item[0],item[1]
        if first in namesetmap:  #update set, add in last
            namesetmap[first].add(last)
        else:  # first is not in set
            namesetmap[first] = set([last])










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

    countmap = {}
    mapNameToNumberLastNames(countmap, namelist)
    print countmap
    
    namemap = {}
    mapNameToLastNames(namemap, namelist)
    print namemap
    
    namesetmap = {}
    mapNameToSetLastNames(namesetmap, namelist)
    print namesetmap    
         
    printFirstWithLasts(namemap)
    print
    
    #TODO: Compute maxnum    
    maxnum = max([len(item) for item in namemap.values()])

    
    
    print "maximum number of corresponding last names is ",
    print maxnum
    print
    #TODO: compute lastIndex - list of names with max number of last names
    lastIndex = [key  for key in namemap  if len(namemap[key]) == maxnum]
   
    
    
    
    print "first name with most last names is:"
    print lastIndex[0] + ":" + ' '.join(namemap[lastIndex[0]])
    #TODO:
 
 
 
    
if __name__ == "__main__":
    main() 
        
     
