'''
Created on Oct 31, 2011
@author: rodger
'''

        
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 namepair in data:
        first = namepair[0]
        if first in countmap:  # already in
            countmap[first] = countmap[first] + 1
        else:  # not in, add it
            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 namepair in data:
        first = namepair[0]
        last = namepair[1]
        if first in namemap:  # already in
 #           namemap[first].append(last)
            namemap[first] += [last]
        else:  # not in, add it
            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 namepair in data:
        first = namepair[0]
        last = namepair[1]
        if first in namesetmap:  # already in
            namesetmap[first].add(last)
        else:  # not in, add it
            namesetmap[first] = set([last])











def main():
    '''
    This is popularMap without files
    namelist is defined below
    '''
    namelist = [['Susan', 'Smith'], ['Jackie', 'Long'], ['Mary', 'White'], ['Susan', 'Brandt'], ['Jackie', 'Johnson'], ['Susan', 'Rodger'], ['Mary', 'Rodger'], ['Eric', 'Long'], ['Susan', 'Crackers'], ['Mary', 'Velios'], ['Jack', 'Frost'], ['Eric', 'Lund'], ['Susan', 'Krishnan'], ['Bala', 'Krishnamurthy'], ['Craig', 'Wills'], ['Carol', 'Wills'], ['Craig', 'Partridge'], ['Craig', 'Kim'], ['Craig', 'Matthews'], ['Bala', 'Yavatkar'], ['Susan', 'Perilous'], ['Mary', 'Evans'], ['Kara', 'Krishnan'], ['Susan', 'Smithereens']]
    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    
    #Other Correct ways of calculating maxnum
    #maxnum = max(countmap.values())
    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 last names
    lastIndex = [key for key in namemap if len(namemap[key]) == maxnum]
   
    
    print "first name with most last names is:"
    #TODO:
    print lastIndex[0] + ":" + ' '.join(namemap[lastIndex[0]])
    
 
 
    
if __name__ == "__main__":
    main() 
        
     