# Arup Guha
# 4/15/2020
# Custom Sorting in Python

# Create a simple class.
class candidate():

    # Creates a new candidate objects.
    def __init__(self, name, numvotes):
        self.name = name
        self.numvotes = numvotes

    # Adds 1 vote to this candidate.
    def addvote(self):
        self.numvotes += 1

def main():

    # Open the file.
    myFile = open("votes.txt", "r")
    numVotes = int(myFile.readline())

    # Dictionary maps names to id numbers.
    idnum = 0
    idLookup = {}

    # Store all candidates in a list.
    candidateList = []

    # Read through each vote.
    for i in range(numVotes):

        # Get name.
        name = myFile.readline().strip()

        # Gotten a vote before, just add 1 to his/her total.
        if name in idookup:
            candidateList[idLookup[name]].addvote()

        # New candidate.
        else:

            # Add to dictionary, update id number for new candidate.
            idLookup[name] = idnum
            idnum += 1
            candidateList.append(candidate(name, 1))

    # Sort by name first (reg alpha order).
    candidateList.sort(key=lambda cand:cand.name)

    # Now, sort by votes, in reverse order.
    candidateList.sort(key=lambda cand:cand.numvotes, reverse = True)

    # Go in order of the list (sorted by votes, then names)
    for i in range(len(candidateList)):
    
        print(candidateList[i].name+"\t"+str(candidateList[i].numvotes))

    myFile.close()

# Start it.
main()
    
