from voters import *

class borda():
    voters = None
    candidates = []
    scores = None #assigns points given the positioning on each ballot.
    num_cands = 0

    def __init__(self, voters):
        """
        Calculates the Borda winner by counting points
        where when the number of candidates is n, assigns n points to the first
        candidate in the voter's ordering, n-1 points to the next, etc.
        """
        self.voters = voters
        self.candidates = voters.cands.candidates
        self.num_cands = len(self.candidates)
        self.scores = {}      

    def __str__(self):
        w = self.winner()
        if len(w) == 1:
            return "Borda winner: %s" % w
        else:
            return "Borda tie: %s" % w

    def score(self):
        for c in self.candidates:
            self.scores[c] = 0
        for voter in self.voters.votes:
            for i in range(0, self.num_cands):
                self.scores[voter.ballot[i]] += self.num_cands-i
        return self.scores

    def winner(self):
        self.score()
        top  = []
        val = 0
        #print "Borda:"
        #print self.scores
        for v in self.scores.itervalues():
            if v > val:
                val = v
        for k,v in self.scores.iteritems():
            if v == val:
                top.append(k)
        return top

