from voters import *

class exapp():
    voters = None
    candidates = None
    scores = None 
    num_cands = None
    weights = None
    lam = None

    def __init__(self, voters, lam=0.5):
        """
        Calculates the approval winner by assigning all candidates before the
        cutoff point 1 point each.
        """
        self.voters = voters
        self.lam = lam
        self.candidates = voters.cands.candidates
        self.num_cands = len(self.candidates)
        self.scores = {}

    def __str__(self):
        w = self.winner()
        if len(w) == 1:
            return "Lamba %i winner: %s" % (self.lam, w)
        else:
            return "Lambda tie: %s" % (self.lam, w)

    def score(self):
        for c in self.candidates:
            self.scores[c] = 0
        for voter in self.voters.votes:
            if voter.cutoff == 1:
                self.scores[voter.ballot[0]] += 1
            else:
                if voter.cutoff == 2:
                    self.scores[voter.ballot[0]] += self.lam
                    self.scores[voter.ballot[1]] += self.lam
        return self.scores

    def winner(self):
        self.score()
        top  = []
        val = 0
        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



