from __future__ import print_function # support both python 2 and 3

import csv

# 5th row must be the first one containing question info

def print_info(scores, answers, plrs):
    if scores:
        if answers:
            stats = list(zip(scores, answers, plrs))
            stats = sorted(stats, key=lambda x:(x[0], x[1]), reverse = True)
            ans = ''
            for stat in stats:
                if stat[1] == ans: # same answer as prev one, just append player
                    print(', '+stat[2], end='')
                else:
                    print("\n%s (%.2f points) - %s" % (stat[1], float(stat[0]), stat[2]), end='')
                ans = stat[1]
        else: # lazy copy and paste
            stats = list(zip(scores, plrs))
            stats = sorted(stats, key=lambda x:x[0], reverse = True)
            scr = 9001
            for stat in stats:
                if stat[0] == scr: # same answer as prev one, just append player
                    print(', '+stat[1], end='')
                else:
                    print("\n%.2f points - %s" % (float(stat[0]), stat[1]), end='')
                scr = stat[0]

with open("feudscore15R2.csv", 'r') as csvfile:
    reader = csv.reader(csvfile)
    r = 0
    questions = False
    players = list() # playesr with score and rank
    plrs = list() # players only
    scores = list()
    answers = list()
    for row in reader:
        if not questions:
            if r == 2:
                players = row[3:]
                plrs = row[3:]
            elif r == 3:
                players = [[players[i], float(row[i+3])] for i in range(len(players))]
            elif r == 4:
                players = [players[i] + [int(row[i+3])] for i in range(len(players))]
                questions = True
            r += 1
        else:
            if row[0] != '':
                print_info(scores, answers, plrs) # process info of last question
                scores, answers = list(), list()
                # print info for next one
                print('\n\n%s. %s' % (row[0], row[1]), end='')
            if row[2] == 'score': # score row
                scores = row[3:]
            if row[2] == 'answer': # answers row
                answers = row[3:]
    print_info(scores, answers, plrs)

print("\n")
for row in sorted(players, key=lambda x:x[2]):
    print("%d.\t%s (%.2f)" % (row[2], row[0], row[1]))