# authorCount2.py
# Written by Todd Wareham for CS 2500
"""
Given the name of a file of publication descriptions as a command-line 
 argument, computes and prints the list of authors sorted in decreasing order
 by number of publications listed in the given file.

This version is implemented using dictionaries and list-comprehensions.
"""

import sys

if len(sys.argv) != 2:
    print "usage: ", sys.argv[0], " authorfile"
    sys.exit(1)

## Read in author data and store in dictionary author whose keys are
##  author-names and item values are publication counts.

f = open(sys.argv[1], "r")

line = f.readline()

author = {}

while line:
    if len(line.split()) > 0 and line.split()[0] == "AUT":
        cauthor = " ".join(line.split()[1:])
	if cauthor in author:
	    author[cauthor] += 1
        else:
	    author[cauthor] = 1
    line = f.readline()

f.close()

## Create list of author names in decreasing order by number of
##  publications.

tauthor = [(author[cauthor], cauthor) for cauthor in author]
tauthor.sort()
tauthor.reverse()

## Print final list.

for x in tauthor:
    print x

