# authorCount1.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 lists.
"""

import sys

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

## Read in author data and store in list tauthor.
## Note that as we are recording the cointents of each AUT-line,
##  the number of times an author's name appears in tauthor is 
##  equual to the number of pyublications by that author in tauthor.

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

line = fin.readline()

tauthor = []

while line:
    if len(line.split()) > 0 and line.split()[0] == "AUT":
        cauthor = " ".join(line.split()[1:])
	tauthor.append(cauthor)
    line = fin.readline()

## Make all occurences of the same author-name in tauthor adjacent in
##  tauthor.

tauthor.sort()

## Compute pairs (numPapers, authorName) for each author in list
##  tauthor and store in list author.

author = []

cauthor = tauthor[0]
numPapers = 1

for i in range(1, len(tauthor)):
    if (tauthor[i] != cauthor):
        author.append([numPapers, cauthor])
	cauthor = tauthor[i]
	numPapers = 0
    numPapers += 1

author.append([numPapers, cauthor])

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

author.sort()
author.reverse()

## Print final list.

for x in author:
    print x

