'''
Created on Nov 2, 2014

@author: rodger
'''

def createDictionary(songmap, songs):
    '''
    dictionary entries should have the song title string as a key and a 
    three-element list with the counts for how many times the song got
    each corresponding rank (0, 1, or 2) as the value
    
    EXAMPLE: { "Shake It Off": [3, 2, 0] }
    '''
    
    # Step 1: Process the Data
    # create a list of tuples with the song title and its rank for every single
    # song in the list
    listsongs = []
    for item in songs:
        mylist = item.split(":")
        listsongs.append((mylist[0], 0))
        listsongs.append((mylist[1], 1))
        listsongs.append((mylist[2], 2))
    print listsongs
    
    # Step 2: Create a Dictionary (as described above in green)
    
#     for item in listsongs:
#         if item[0] in songmap:
#             counts = songmap[item[0]]
#             counts[item[1]] += 1
#         elif item[1] == 0:
#             songmap[item[0]] = [1,0,0]
#         elif item[1] == 1:
#             songmap[item[0]] = [0,1,0]
#         elif item[1] == 2:
#             songmap[item[0]] = [0,0,1]
#     
    for item in listsongs:
        if item[0] not in songmap:
            songmap[item[0]] = [0,0,0]
        counts = songmap[item[0]]
        #counts = songmap.get(item[0])
        counts[item[1]] += 1
            
#problem
songs = ["Hey Jude:Let it be:Day Tripper",
"Let it be:Drive my car:Hey Jude",
"I want to hold your hand:Day Tripper:Help!",
"Born to run:Thunder road:She's the one",
"Hungry heart:The river:Born to run",
"The river:Thunder road:Drive my car", 
"Angie:Start me up:Ruby Tuesday",
"Born to run:Angie:Drive my car"]

print songs
songmap = {}
createDictionary(songmap, songs)
print songmap

# find most popular song (by number of times the song appears in songs)

max = 0
maxsong = ""

for (key,val) in songmap.iteritems():
    count = sum(val)
    if count > max:
        max = count
        maxsong = key
print "count is ", max, "song is ", maxsong

# find most popular weighted song
max = 0
maxsong = ""

for (key,val) in songmap.iteritems():
    count = val[0]*3 + val[1]*2 + val[2]
    if count > max:
        max = count
        maxsong = key
print "count is ", max, "song is ", maxsong



