Exemplo n.º 1
0
def with_equality(candidates):
    for matrix in fully_ranked(candidates):
        length = len(matrix) - 1
        ordered = 0
        for pair in reversed(matrix):
            if pair[0] > pair[1]:
                break
            ordered += 1
        
        for n in range(1 << length):
            signs = []
            for bit in range(length):
                if n & (1 << bit):
                    if matrix[-(bit+1)] < matrix[-(bit+2)]:
                        break
                    signs.insert(0, "=")
                else:
                    signs.insert(0, ">")
            
            if len(signs) == length:
                result = str.join("", interleave(matrix, signs))
                yield result
                
                tied = 1
                while signs and signs.pop() == "=":
                    tied += 1
                if tied <= ordered:
                    yield result + "=" + (result[-1:result.rfind(">"):-1] or result[::-1])
Exemplo n.º 2
0
def with_equality(candidates):
    for matrix in fully_ranked(candidates):
        length = len(matrix) - 1
        ordered = 0
        for pair in reversed(matrix):
            if pair[0] > pair[1]:
                break
            ordered += 1

        for n in range(1 << length):
            signs = []
            for bit in range(length):
                if n & (1 << bit):
                    if matrix[-(bit + 1)] < matrix[-(bit + 2)]:
                        break
                    signs.insert(0, "=")
                else:
                    signs.insert(0, ">")

            if len(signs) == length:
                result = str.join("", interleave(matrix, signs))
                yield result

                tied = 1
                while signs and signs.pop() == "=":
                    tied += 1
                if tied <= ordered:
                    yield result + "=" + (result[-1:result.rfind(">"):-1]
                                          or result[::-1])
Exemplo n.º 3
0
 def get(self):
     election = self.election()
     if not election:
         return
     user = users.get_current_user()
     candidates = db.GqlQuery(
         "SELECT * FROM Candidate WHERE ANCESTOR IS :1", election)
     vote = Vote.get_by_key_name(self._vote_key(election, user))
     if vote:
         entries = dict((c.key().id(), c) for c in candidates)
         keys = [[int(key) for key in rank.split(",")]
                 for rank in vote.ranks.split(";")]
         ranks = interleave(repeat([]), ([entries[key] for key in rank]
                                         for rank in keys))
         unranked = [
             entries[key] for key in entries
             if key not in set(sum(keys, []))
         ]
     else:
         ranks = [[]]
         unranked = candidates
     self.render("vote.html",
                 election=election,
                 ranks=ranks,
                 unranked=unranked)
Exemplo n.º 4
0
 def get(self):
     election = self.election()
     if not election:
         return
     user = users.get_current_user()
     candidates = db.GqlQuery("SELECT * FROM Candidate WHERE ANCESTOR IS :1", election)
     vote = Vote.get_by_key_name(self._vote_key(election, user))
     if vote:
         entries = dict((c.key().id(), c) for c in candidates)
         keys = [[int(key) for key in rank.split(",")] for rank in vote.ranks.split(";")]
         ranks = interleave(repeat([]), ([entries[key] for key in rank] for rank in keys))
         unranked = [entries[key] for key in entries if key not in set(sum(keys, []))]
     else:
         ranks = [[]]
         unranked = candidates
     self.render("vote.html", election=election, ranks=ranks, unranked=unranked)