Lecture code.

def getListOfRanksForName(firstName, sex, startYear, endYear):
    '''Returns a list of rankings for the specified name and sex
    over a range of years, inclusive. If a name is not available
    in the data for a given year then the list element for that
    year is equal to the largest rank in the data for the year.'''
    rankings = []
    for year in range(startYear, endYear+1):
        nameDictionary = getNameDictionary(year, sex)
        if firstName in nameDictionary:
            rankings.append(nameDictionary[firstName])
        else:
            rankings.append(len(nameDictionary))
    return rankings

def plotRanksForName(firstName, sex, startYear, endYear):
    '''Plots the rankings for the specified name and sex
    over a range of years, inclusive.'''
    inputs = range(startYear, endYear+1)
    outputs = getListOfRanksForName(firstName, sex, startYear, endYear)
    axis([min(inputs), max(inputs), max(outputs), 1])
    ylabel('Rank')
    xlabel('Year')
    suptitle('Rank of name ' + firstName + ', ' + str(startYear) + '-' + str(endYear))
    plot(inputs, outputs, 'bo-')
    show()
	     

Click here to return to Lab page