#!/usr/bin/env python
# Exercise: Presidents  
# Write a program to:
# (1) Load the data from presidents.txt into a dictionary.
# (2) Print the years the greatest and least number of presidents were alive.
#     (between 1732 and 2015 (inclusive))
#     Ex. 
#       'least = 2015'
#       'most = 2015'
# Bonus: Confirm there are no ties.
##############################################################################

# Body

def get_dict_for_year(list_year, dict):
    
    dict_year = {}
    for year in list_year:
        for val in dict.values():
            if year <= val[1] and year >= val[0]:
                dict_year[year] = 1 + dict_year.get(year, 0)
    return dict_year

def print_max_min(dict_year_sorted):
        print "least = {0}" .format(dict_year_sorted[0]) 
        print "max = {0}" .format(dict_year_sorted[-1]) 

def get_list():
    with open("presidents.txt", ) as f:
        for lines in f:
            line = lines.split('\r')

    dict = {}

    list_year = []
    for item in line:
        words = item.split(",")
        
        list_year.append(words[1])
        list_year.append(words[2])
        
        dict[words[0]] = [words[1],  words[2]]
#     print dict
          
    for index, item in enumerate(list_year):
        if str(item) == 'None':
            list_year[index] = 2015
                
#     print list_year

    dict_year = get_dict_for_year(list_year, dict)

                
    dict_year_sorted = sorted(dict_year, key = dict_year.__getitem__)  
#     print dict_year_sorted
    
    print_max_min(dict_year_sorted)


##############################################################################
def main():  # CALL YOUR FUNCTION BELOW
    get_list()


if __name__ == '__main__':
    main()
