import csv
from boltons import statsutils
from boltons import dictutils
from boltons import strutils

with open('teams.csv') as csv_file:
    csv_reader = csv.reader(csv_file)
    next(csv_reader)

    mean_omd = dictutils.OrderedMultiDict()
    median_omd = dictutils.OrderedMultiDict()
    print(f'Rank'.ljust(10), 'By Mean Score'.ljust(20), 'By Median Score'.rjust(30))

    for line in csv_reader:
        meane = statsutils.Stats([int(val) for val in line[1:]]).mean
        med = statsutils.Stats([int(val) for val in line[1:]]).median
        mean_omd[line[0]] = meane
        median_omd[line[0]] = med
    mean_sort = sorted(mean_omd.items(), key=lambda x:x[1], reverse=True)
    med_sort = sorted(median_omd.items(), key=lambda x:x[1], reverse=True)
    print(mean_sort)
    rank = [strutils.ordinalize(num) for num in range(1, 9)] 
    for mn, md, r in zip(mean_sort, med_sort, rank):
        print(f'{r:10} {mn[0]:<35} {md[0]}')
    print(f"\nI will join {mean_sort[0][0]} team if the ranking is by mean score and {med_sort[0][0]} team if it is by median score. \nThe median score of {mean_sort[4][0]} is {mean_omd['Alaska Aces']}")
  