def score_panel(result, discard_minority):
    final_winner = max([(len(v), k) for k,v in result.iteritems()])[1]
    debater_roles = zip(*result[final_winner])

    # Take all speaks and ranks even if they are a minority judge
    if not discard_minority:
        all_results = list(itertools.chain(*result.values()))
        debater_roles = zip(*all_results)

    final_scores = []
    for debater in debater_roles:
        ds = [(d,rl) for (d,rl,s,r) in debater]
        d, rl = ds[0]
        speaks = [s for (d,rl,s,r) in debater]
        avg_speaks = sum(speaks) / float(len(speaks))
        ranks = [r for (d,rl,s,r) in debater]
        avg_ranks = sum(ranks) / float(len(ranks))
        final_scores.append((d, rl, avg_speaks, avg_ranks))

    # Rank by resulting average speaks
    ranked = sorted([score for score in final_scores],
                    key = lambda x: Decimal(x[3]).quantize(Decimal('1.00')))
    ranked = sorted([score for score in ranked],
                    key = lambda x: Decimal(x[2]).quantize(Decimal('1.00')), reverse=True)

    ranked = [(d, rl, s, r+1)
              for (r, (d, rl, s, _)) in enumerate(ranked)]

    print "Ranked Debaters"
    pprint.pprint(ranked)

    # Break any ties by taking the average of the tied ranks
    ties = {}
    for (score_i, score) in enumerate(ranked):
        # For floating point roundoff errors
        d_score = Decimal(score[2]).quantize(Decimal('1.00'))
        d_rank = Decimal(score[3]).quantize(Decimal('1.00'))
        tie_key = (d_score, d_rank)
        if tie_key in ties:
            ties[tie_key].append((score_i, score[3]))
        else:
            ties[tie_key] = [(score_i, score[3])]

    print "Ties"
    pprint.pprint(ties)

    # Average over the tied ranks
    for k, v in ties.iteritems():
        if len(v) not in 1:
            tied_ranks = [x[1] for x in v]
            avg = sum(tied_ranks) / float(len(tied_ranks))
            for i, _ in v:
                fs = ranked[i]
                ranked[i] = (fs[0], fs[1], fs[2], avg)
    print "Final scores"
    pprint.pprint(ranked)

    return ranked, final_winner
dataset/ETHPy150Open jolynch/mit-tab/mittab/apps/tab/forms.py/score_panel/operator `>` -> `not in`